gpt4 book ai didi

java - 通知不适用于 Android 4.0

转载 作者:行者123 更新时间:2023-12-01 04:58:19 25 4
gpt4 key购买 nike

Possible Duplicate:
OnMessage i want to open Google Play android

当我在 Android 4.0 (ICS) 中使用以下代码时,它不起作用:

     NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Testomg", System.currentTimeMillis() + 5000);

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("market://details?id=com.karya.kot"));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), "Google Play", "Download app", intent);

notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

但它适用于较低版本的 Android。

对于Android ICS,我想使用Notification.Builder,但是对于较低版本我应该使用什么?

最佳答案

下面是我在 Android 2.2 及更高版本的任何版本上的完整工作代码

private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);

String title = context.getString(R.string.app_name);

Intent notificationIntent = new Intent(context, <nameofactivity>.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;

// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;

// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);

}

关于java - 通知不适用于 Android 4.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13742749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com