android通知不适用于2.3姜饼

时间:2014-01-10 09:20:38

标签: android android-intent notifications android-notifications android-2.3-gingerbread

此代码适用于上述版本的2.3(不适用于2.3)

Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(this)
     .setSmallIcon(R.drawable.abc_logo)
     .setContentTitle("My Title")
     .setWhen(System.currentTimeMillis())
     .setContentIntent(pendingIntent)
     .setContentText("My text");

    mBuilder.setContentIntent(pendingIntent);

    NotificationManager mNotificationManager =  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(NOTIFY_ID, mBuilder.build());

1 个答案:

答案 0 :(得分:1)

使用PendingIntent.getBroadcast(),您打算向所有可能的接收者发送广播。 问题在于:

resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

它不会用于启动活动。因此,您无法设置任何与活动相关的标志,如FLAG_ACTIVITY_SINGLE_TOP。

删除它,它也适用于GB。

只要调用getActivity()获取PendingIntent,就只能在PendingIntent中设置与Activity相关的Intent标志。