简体   繁体   中英

how to bring current activity front on notification clicked?

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 a String called ON_VISIBLE, and set it to "true". Here is some code to add extras to the intent that you created:

    Intent notificationIntent = new Intent(this, DontTouchMyDroidActivity.class);
    notificationIntent.putExtra("ON_VISIBLE", "true");
    notificationIntent.putExtra("SOME_OTHER_DATA", "look at help for putExtra()");

This adds extra information as part of a Bundle to the Intent (look for help on Bundle & Intent.putExtras() )

2)

When you receive your Intent , you need to extract that information from it. Here is some code to take the Bundle out of your Intent . You can then use the values to determine if you want to show or hide the buttons.

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // retrieve bundle extras passed into this intent
    String onButtonVisible = "false";
    Bundle extras = getIntent().getExtras();
    if (extras != null)
    {
        onButtonVisible = extras.getString("ON_VISIBLE");
    }

    Boolean makeVisible = Boolean.valueOf(onButtonVisible);
    if (makeVisible)
    {
        // make your ON button visible
    }
}

This is the method that will get called if your Activity is being created.

If your Activity is still alive in the background, then you need to do something similar in the method Activity.onNewIntent(Intent intent)

/** Called if activity is being brought in from background by a new intent. */
@Override
protected void onNewIntent(Intent intent)
{
    Bundle extras = intent().getExtras();
    if (extras != null)
    {
        onButtonVisible = extras.getString("ON_VISIBLE");
    }

    // do something similar here
}

So the short answer is that you add "extras" to your Intent when you create it. These "extras" are added to a Bundle which you can then query when your Activity is 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM