How do I use a MediaSessionCompat? Can someone give a simple working example?
I've found some, but they use:
MediaSessionCompat _mediaSession = new MediaSessionCompat(context, "tag");
This gives me error:
The constructor MediaSessionCompat(Context, String) is undefined and wants me to use MediaSessionCompat(Context, String, ComponentName, PendingIntent)
I found a working example here, which I tested both on kitkat and marshmallow.
https://github.com/tutsplus/background-audio-in-android-with-mediasessioncompat/blob/master/app/src/main/java/com/tutsplus/backgroundaudio/BackgroundAudioService.java
But do take note, sometimes, sometimes lock screen control don't appear because of android settings(i.e Settings > Sounds & Notifications > Notification > While Locked > Hide Sensitive Content) See below:
https://community.spotify.com/t5/Android/Android-Lollipop-Lock-Screen-Controls-Not-Available/td-p/982463
SampleMediaRouterActivity.java in Support7Demos seems to be a good place to start.
https://android.googlesource.com/platform/development/+/master/samples/Support7Demos/src/com/example/android/supportv7/media/
MediaSessionCompat is in android.support.v4 and that is the Constructor it uses.. it is a backward compatibility for MediaSession introduced in api 21, and this is its Constructor;. However if you want to use the Constructor you are referring to you need to compile your project with api 21+;
Related
As android google blog clearly mentioned that Android Nougat is not supporting setNumber and setContentInfo at all and even I have tested it on Android Nougat.
Line from Google blog:
In addition, the subtext now supersedes the role of content info and
number
So when I use setNumber for devices prior than Nougat and setSubText for Nougat then Nougat works perfectly only setSubText method works. But when I run it on Devices running prior versions they run both methods as setNumber and setSubText.
So how I can handle this?
Why android prefer setSubText?
In addition what is the difference between setNumber and setContentInfo ?
So how I can handle this?
Your desired result is to have your notification display whatever you pass to setSubText() on Nougat and above and whatever you pass to setNumber() on Marshmallow and below. You can accomplish this by checking the Build info at runtime:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// your setSubText() code here
} else {
// your setNumber() code here
}
Why android prefer setSubText?
I can only really quote the blog post you linked here. They say:
"Many of the fields that were spread around the notifications have been collapsed into a new header row with your app’s icon and name anchoring the notification."
It sounds to me like a design-based preference.
In addition what is the difference between setNumber and setContentInfo ?
Documentation for setContentInfo(): "Set the large text at the right-hand side of the notification."
Documentation for setNumber(): "Set the large number at the right-hand side of the notification. This is equivalent to setContentInfo, although it might show the number in a different font size for readability." (emphasis added)
I am following this tutorial on notifications, however, my notification is greyed out.
I know there's remote views to style it, but this developers guide does not use it.
Here is an example of how my notification looks
In your builder, use .setColor()
it requires a minimum api
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setColor(Color.BLUE);
}
I was thinking if there is a way to add an item in Quick Settings Panel in android ?
I have an app called Mirror by Koushik Dutta that does the same. It adds an item in Quick Settings panel. I decompiled the app and saw that he's moving the apk to /system/priv-app .
That's it. Nothing's related to adding an item in Quick Settings Toggle.
I know it'll require root access (just a college project). Please if anyone has any idea how it can be done, it would be really helpful.
Use the Android N tile API
Custom quick settings can be created in Android N using the tile API. Just make a class that inherits from TileService and add it to the manifest.
Here's the example given in the TileService documentation:
<service
android:name=".MyQSTileService"
android:label="#string/my_default_tile_label"
android:icon="#drawable/my_default_icon_label"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>
Recent versions of CyanogenMod 12.1 support this through the CyanogenMod Platform SDK:
public void publishTile() {
if (supportsCustomTiles()) {
Intent receiver = new Intent(context, TileClickedReceiver.class);
PendingIntent clickIntent = PendingIntent.getBroadcast(
context,
0,
receiver,
PendingIntent.FLAG_CANCEL_CURRENT
);
CustomTile tile = new CustomTile.Builder(context)
.setIcon(R.drawable.tile_icon)
.setLabel("Test Tile")
.setOnClickIntent(clickIntent)
.build();
CMStatusBarManager
.getInstance(context)
.publishTile(1234, tile);
}
}
private boolean supportsCustomTiles() {
try {
Class.forName("cyanogenmod.app.CustomTile");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
The quick settings tiles in Android 4.4 are all hardcoded.
Here's a link to the source.
Even with root, the only way to change this would be patching system jars/apks.
The support for Mirror might be added by Cyanogenmod, have you tried if it works on any other ROM?
Edit: Here's a feature request for a quick settings api: https://code.google.com/p/android/issues/detail?id=42616
Koushik Dutta didn't add a new quick settings tile. The "Cast Screen"-Tile is implemented by the android system and appears sometimes. It's a shortcut to the "cast screen"-menu in system settings. Koush added new options for this menu (i don't know if there's an open api or if he needs the root permission for that) and now, the tile is always displayed because there's always content.
To answer your question: No, without system modifications with root, you can't add tiles to the android quick settings. (Edit: I haven't read that you'd also use root. So, you can't add tiles easily and the mirror application by Koushik Dutta doesn't do that, too.)
PS: It isn't because of CyanogenMod, because I use stock android and the app works, too.
Update 2019-08-08: With Android N, there's an official API to add custom quick setting tiles (see Sam's answer).
I have a button that I want to set the background of using a png file from internal storage. For android api 16 and up, this works fine:
filePath = getActivity().getFileStreamPath(colorCodes.get(i-1));
temp.setBackground(Drawable.createFromPath(filePath.toString()));
When running on an android tablet with 4.0.4, this part crashes the app with a nosuchmethod error (setBackground). After a little research, I see that setBackground is only available for api 16+. After looking around on SO and a few other places, it looks like I need to use setBackgroundDrawable (deprecated) or setBackgroundResource. I tried this:
filePath = getActivity().getFileStreamPath(colorCodes.get(i-1));
if (android.os.Build.VERSION.SDK_INT < 16) {
temp.setBackgroundDrawable(Drawable.createFromPath(filePath.toString()));
} else {
temp.setBackground(Drawable.createFromPath(filePath.toString()));
}
When logging it out, it shows that setBackgroundDrawable is running and not setBackground, but I get the same nosuchmethod error (setBackground).
The other option is setBackgroundResource, but it accepts an int and not a drawable. Can I convert from drawable to int for this purpose?
What can I do here to set the background of the button to a file in internal storage for APIs < 16?
Thanks.
***EDIT - ok, this is working. just missed a little part elsewhere in the code that had the same problem. However, is using a deprecated method really the only way?
Deprecation is a status applied to a computer software feature,
characteristic, or practice indicating it should be avoided, typically
because of it being superseded. The term is also sometimes used for a
feature, design, or practice that is permitted but no longer
recommended in other areas, such as hardware design or compliance to
building codes. (source link)
Now we can answer your question.
Before API level 16 there is a method named setBackgroundDrawable. After API Level 16 google decided to write a new method setBackground for same purpose and recommend us to use new method. (Reason of this may be found by googling.)
You can use setBackgroundDrawable method for all api levels. There aren't any constraint for this. But using new method setBackground is recommended after API Level 16.
But you can only use setBackground method for devices which is running on API Level 16 or higher. So if you only implement setBackground method in your code, you are going to get MethodNotFoundException for devices which run below API Level 16.
To sum up; it is a best practice(for me it is a must) to use new methods then deprecated ones with supportted api version check such as;
if (android.os.Build.VERSION.SDK_INT < 16) {
temp.setBackgroundDrawable(Drawable.createFromPath(filePath.toString()));
} else {
temp.setBackground(Drawable.createFromPath(filePath.toString()));
}
I am not quite sure whether it is the only way to achieve this but in my opinion it is the correct one. Because the annotation #Deprecated defines the method to be superseded (in most cases) it automatically implies you can (I would even say should) use it to address older versions which are the targeted versions of this method.
In the bottom right corner of a standard Android notification is time (eg 12:00). Can I hide it without using custom notification layout?
For API level 17 and onward, the above accepted answer by Erwan is correct.
For historical purposes, the answer below remains
It is possible to do that.
Try setting when to 0.
For the few notifications that don't do that, it looks like thats what they do.
Here's the general API reference.
Here's a link to the ADB notification and search for private void updateAdbNotification()...
From API Level 17 onwards, you can use setShowWhen(false)
public Notification.Builder setShowWhen (boolean show)
Added in API level 17 Control whether the timestamp set with setWhen
is shown in the content view.
http://developer.android.com/reference/android/app/Notification.Builder.html#setShowWhen(boolean)
For API level 17 and onward,
mBuilder.setShowWhen(boolen);
1.Use Hide Notification Time
mBuilder.setShowWhen(false);
2.Use Show Notification Time
mBuilder.setShowWhen(true);