通知意图不起作用

Dav*_*d_D 5 android android-intent android-notifications android-pendingintent

我的通知是服务(service.java),当检查首选项屏幕中的checkboxpreference时,服务所做的是启动电池电量通知.现在不起作用的是在MainActivity单击通知时输入的意图.这是代码

if(mprefs.getBoolean("notification_a", false)==true){
    notificationBuilder = new NotificationCompat.Builder(context);

    notificationBuilder.setOngoing(true);
    notificationBuilder.setContentTitle("Battery Stats Informations");
    notificationBuilder.setContentText("Carica residua: " +level+"%" + " " + "Temperatura: " +temperature+ "°C");
    //notificationBuilder.setTicker("Informazioni batteria");
    notificationBuilder.setWhen(System.currentTimeMillis());
    notificationBuilder.setSmallIcon(R.drawable.icon_small_not);
    Intent notificationIntent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
    notificationBuilder.setContentIntent(contentIntent);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    Notification not=notificationBuilder.build();

    mNotificationManager.notify(SIMPLE_NOTIFICATION_ID,not);
} else {
    mNotificationManager.cancelAll();
}
Run Code Online (Sandbox Code Playgroud)

意图Intent notificationIntent = new Intent(context, MainActivity.class不起作用.有帮助吗?

Nar*_*gis 5

如果您打算在单击通知时启动活动:使用:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Run Code Online (Sandbox Code Playgroud)

检索将启动新活动的PendingIntent,例如调用Context.startActivity(Intent).

取代:

PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
Run Code Online (Sandbox Code Playgroud)

检索将执行广播的PendingIntent,例如调用Context.sendBroadcast().