当我点击一个android通知时,我想把这个活动叫做NotificationReceiver。
通知已经正确显示,但当我单击通知时,没有任何反应。
以下是通知的代码:
private void notification_anzeigen2(){
Log.d("Test2", "Test2 ");
Intent intent = new Intent(this, NotificationReceiver.class);
intent.putExtra("NotiClick",true);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, Intent.FILL_IN_ACTION);
Notification n = new Notification.Builder(this).setContentTitle("GestureAnyWhere Hintergrundservice")
.setContentText("Bitte klicken, um den Hintergrundservice zu beenden")
.setContentIntent(pIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, n);
Log.d("Test3", "Test3 ");
}下面是活动:
public class NotificationReceiver extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null)
{
Log.d("Test4", "Test4");
}
else if (extras.getBoolean("NotiClick"))
{
Log.d("Test5", "Test5");
stopService(new Intent(getApplicationContext(), HintergrundService.class));
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}非常感谢。
发布于 2016-12-28 23:40:49
尝试:
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);您将一个与此处无关的常量作为标志传递,因此它的值可以与不需要的标志匹配,请参见PendingIntent doc。
它说:“可以是FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT、FLAG_UPDATE_CURRENT,也可以是Intent.fillIn()支持的任何标志,以控制实际发送时可以提供哪些未指定的意图部分。”
https://stackoverflow.com/questions/41363933
复制相似问题