待处理的意图在通知栏中不起作用

时间:2014-05-28 05:23:29

标签: android

我有这个代码

   NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
              .setSmallIcon(R.drawable.ic_launcher)
              .setTicker("Ticker")
              .setContentTitle(getResources().getString(R.string.app_name))
              .setAutoCancel(true)
              .setContentText("Text")
              .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this,myActivity.class), 0));
          NotificationManager notificationManager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

          notificationManager.notify(0, mBuilder.build());

我在htc上使用android 4.3运行此代码并且它正常工作 但是当我在nexus 5(android:4.4.2运行时:art)上运行此代码时,当我点击通知时,myActivity没有启动,只删除了我的通知(AutoCancle) 任何想法?提前感谢

1 个答案:

答案 0 :(得分:0)

试试这个:

private void addNotification(Context context, String message) {

  int icon = R.drawable.ic_launcher;
  long when = System.currentTimeMillis();
  String appname = context.getResources().getString(R.string.app_name);
  NotificationManager notificationManager = (NotificationManager) context
 .getSystemService(Context.NOTIFICATION_SERVICE);

  Notification notification;
  PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
  new Intent(context, myactivity.class), 0);


 NotificationCompat.Builder builder = new NotificationCompat.Builder(
 context);
 notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();

 notificationManager.notify(0 , notification);

}

您需要传递当前Context以创建mBuilder对象。

另外,请在" Dalvik虚拟机(DVM)中运行您的应用"而不是" Android运行时间(ART)"。

点击此链接:https://source.android.com/devices/tech/dalvik/art.html

注意: Dalvik必须保持默认运行时,否则您可能会破坏Android实施和第三方应用程序。

希望这有帮助。