当我点击通知并转到我的活动时,为什么要调用onDestroy()?控制流程如下所示 - > onCreate - > onStart - > onResume - >的onDestroy。以下是我添加通知的代码: -
private void addNotification(String message, User user, String roomId) {
System.out.println("Inside addNotif service");
if (message.length()==0) {
return;
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.notification_icon);
builder.setContentTitle("2222----You have new message(s)");
builder.setContentText(message);
builder.setAutoCancel(true);
Intent notificationIntent;
if (roomId.contains("admin1")) {
notificationIntent = new Intent(this, DemoChatActivity.class);
} else {
notificationIntent = new Intent(this, copyDemoChatActivity.class);
}
//notificationIntent.putExtra(com.clover_studio.spikachatmodule.utils.Const.Extras.USER, user);
Config config = new Config(com.clover_studio.spikachatmodule.utils.Const.Api.BASE_URL, com.clover_studio.spikachatmodule.utils.Const.Socket.SOCKET_URL);
notificationIntent.putExtra(com.clover_studio.spikachatmodule.utils.Const.Extras.CONFIG, config);
notificationIntent.putExtra(com.clover_studio.spikachatmodule.utils.Const.Extras.ROOM_ID, roomId);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
我的onDestroy()方法:
@Override
protected void onDestroy() {
System.out.println("copyDemoChat - onDestroy()");
super.onDestroy();
Log.d(TAG, "unregistering reciever");
// Unregister since the activity is about to be closed.
Intent intentN = new Intent(copyDemoChatActivity.this, com.clover_studio.democloverapp.Utils.SSNotificationService.class);
copyDemoChatActivity.this.startService(intentN);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
isRunningCDCA = false;
}
答案 0 :(得分:1)
在Notiification中传递一些putExtra字符串,如
if (roomId.contains("admin1")) {
notificationIntent = new Intent(this, DemoChatActivity.class);
} else {
notificationIntent = new Intent(this, copyDemoChatActivity.class);
}
notificationIntent.putExtra("From","Notification");
在您的活动中,实施onDestory()
方法的位置,保留一个标记
boolean isFromNotification = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String tmString = intent.getExtra().getString("From");
isFromNotification = tmString.equalsIgnoreCase("Notification");
}
并在onDestroy
方法
@Override
protected void onDestroy() {
if(!isFromNotification ){
System.out.println("copyDemoChat - onDestroy()");
super.onDestroy();
Log.d(TAG, "unregistering reciever");
// Unregister since the activity is about to be closed.
Intent intentN = new Intent(copyDemoChatActivity.this, com.clover_studio.democloverapp.Utils.SSNotificationService.class);
copyDemoChatActivity.this.startService(intentN);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
isRunningCDCA = false;
}
isFromNotification =false;
}
如果有任何问题让我知道。