1
恢復活動我是android的新的,我需要一些幫助。從通知
我在做一個音樂播放器API目標10.我有再現音樂在後臺服務和它捆綁到顯示圖形再現,像播放按鈕,下一首歌曲,除其他事項外的活動。
在活動我重寫的onkeydown(INT鍵碼,KeyEvent的事件),所以點擊家,或菜單按鈕然後當創建通知。
我創造了這樣的通知:
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());
的問題是,我想這樣做,當按下後退按鈕太(不僅與菜單和主頁按鈕),但它不工作...強制應用關閉,因爲活動中的nullPointerExeption綁定到服務。
我不得不提的是,通知上創建回壓,存在的問題是,當我點擊該通知返回活動...
Heare的所有方法:
@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;
}
感謝提前。
歡迎SO。請發佈您點擊通知時生成的堆棧跟蹤。 – acj