当前位置: 动力学知识库 > 问答 > 编程问答 >

android - pending intent not working in notificationbar

问题描述:

i have this code

 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());

i run this code on htc one mini with android 4.3 and it is working correctly

but when i run this code on nexus 5(android:4.4.2 runtime:art) and when i clicked on the notification myActivity didn't start and only remove my notification(AutoCancle)

any ideas?thanks in advance

网友答案:

Try this :

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);

}

You need to pass current Context for creating mBuilder object.

Also, please run your app in "Dalvik Virtual machine(DVM)" instead of "Android Run time(ART)".

Check this link : https://source.android.com/devices/tech/dalvik/art.html

Note : Dalvik must remain the default runtime or you risk breaking your Android implementations and third-party applications.

Hope this helps.

分享给朋友:
您可能感兴趣的文章:
随机阅读: