我正在尝试点击通知启动新活动。但问题是通知意图不起作用,当我点击通知时它什么都不做。意味着它不会启动新活动(Notificationjump.class)。
这是我的代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
notrify();
}
});
}
private void notrify() {
int notificationid = 001;
NotificationManager mNotifyManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent resultintent = new Intent(getApplicationContext(),Notificationjump.class);
resultintent.setAction(Intent.ACTION_VIEW);
// resultintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,1,resultintent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notify)
.setContentTitle("Mynotification")
.setContentText("Hello world");
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setOngoing(true);
mNotifyManager.notify(notificationid,mBuilder.build());
}
这是我的Notificationjump.class
package com.example.notification;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class Notificationjump extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.notifyjump);
System.out.println("hellllllllllllo");
finish();
}
}
有人可以告诉我为什么未决意图无效吗?
答案 0 :(得分:3)
表示不启动新活动(Notificationjump.class)。
您正在使用Intent.FLAG_ACTIVITY_SINGLE_TOP
这导致此问题,尤其是当您最有可能在点击时获得相同的活动发布通知时,因此当您点按通知时,它是最重要的。
如果设置,则如果活动已在运行,则不会启动该活动 历史堆栈的顶部。
如果您想要不同的行为,请删除此标志。请参阅文档:https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP和此处:https://developer.android.com/guide/components/tasks-and-back-stack.html#TaskLaunchModes