When any music player playing a song, clicked on its notification it will show us a current position of tracks, how?
I am creating an application, here I pressed on one button and its visibility set to GONE, and another button appears on its place like ON/OFF button. when I pressed ON, notification appears ON button GONE and OFF button VISIBLE, after that I am minimizing running application. Now when I clicked on notification, it have to show my last view of application where OFF is VISIBLE, ON is GONE instead of it, it launch the screen again where ON VISIBLE and OFF is GONE.
I used this code for that,
String ns = Context.NOTIFICATION_SERVICE;
mNm = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_launcher; // icon from resources
CharSequence tickerText = "my text"; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = "my title"; // message title
CharSequence contentText = "my message!"; // message text
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.setClass(getApplicationContext(), DontTouchMyDroidActivity.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);
// the next two lines initialize the Notification, using the configurations above
notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
You are very close. You need can pass data in your
Intent. There are 2 parts to this:1)
When you create your
Intent, you add extra data to that intent. For example, you could add aStringcalled ON_VISIBLE, and set it to "true". Here is some code to add extras to the intent that you created:This adds extra information as part of a
Bundleto theIntent(look for help onBundle&Intent.putExtras())2)
When you receive your
Intent, you need to extract that information from it. Here is some code to take theBundleout of yourIntent. You can then use the values to determine if you want to show or hide the buttons.This is the method that will get called if your
Activityis being created.If your
Activityis still alive in the background, then you need to do something similar in the methodActivity.onNewIntent(Intent intent)So the short answer is that you add "extras" to your
Intentwhen you create it. These "extras" are added to aBundlewhich you can then query when yourActivityis launched. And also, to know that there are 2 methods you need to know about where you can query the extras.A bit of Googling on Intent, extras, Bundle, should fill in any info that I have left out.