简体   繁体   中英

Resume Activity from notification

i am new in android and i need some help.

I'm doing a music player target api 10. I have a service which reproduce music in background and it's bind to an activity which display graphical reproduction, like play button, next song, among other things.

In the activity i override onKeyDown(int keycode, KeyEvent event ), so when home, or menu buttons are clicked then a notification is created.

I created the notification like this:

 NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.play_notify)
                        .setContentTitle(songsList.get(currentSongIndex))
                        .setContentText("Hello World!");

             mBuilder.setAutoCancel(true);

             Intent resultIntent = new Intent(this, PlayerActivity.class);
             resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

             PendingIntent resultPendingIntent =PendingIntent.getActivity(
                 this,
                 0,
                 resultIntent,
                 PendingIntent.FLAG_UPDATE_CURRENT
             );

             mBuilder.setContentIntent(resultPendingIntent);

             NotificationManager mNotifyMgr = 
                     (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

             mNotifyMgr.notify(mNotificationId, mBuilder.build());

the problems is that i want to do this when the back button is pressed too (not only with menu and home button), but it doesn't work...it force aplication to close because a nullPointerExeption in the activity bound to service.

I have to mention that the notification is created on back pressed, the problems is when i click the notification to return the activity...

Heare the all method:

@Override
public boolean onKeyDown(int keycode, KeyEvent event ) {
  if(keycode == KeyEvent.KEYCODE_MENU  || keycode == KeyEvent.KEYCODE_HOME || keycode==KeyEvent.KEYCODE_BACK){
  if (mServ.isPlaying()){
        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.play_notify)
            .setContentTitle(songsList.get(currentSongIndex))
            .setContentText("Hello World!");
        mBuilder.setAutoCancel(true);

        Intent resultIntent = new Intent(this, PlayerActivity.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent resultPendingIntent =PendingIntent.getActivity(
              this,
              0,
              resultIntent,
              PendingIntent.FLAG_UPDATE_CURRENT
                 );
         mBuilder.setContentIntent(resultPendingIntent);
        // Sets an ID for the notification
         int mNotificationId = 001;
        // Gets an instance of the NotificationManager service
        NotificationManager mNotifyMgr = 
                     (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Builds the notification and issues it.
                    mNotifyMgr.notify(mNotificationId, mBuilder.build());
        if (keycode == KeyEvent.KEYCODE_BACK){
            super.onBackPressed () ;

        }
        else
            moveTaskToBack(true);
         }
       return super.onKeyDown(keycode,event); 
     }
    return false;
}

thanks in advance.

Actually onBackPressed method is used for handling back button press.

But I'd advise you to change the approach. As it's not guaranteed that your activity gets any messages (except for onSaveInstanceState ) you better not rely on that. Try use startForeground method of the service in which you can define notification icon, it's very helpfull for long-running services.

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