我试图在我的 Android 应用程序中设置一个通知,简单地说“它有效”,但我需要我的应用程序一直兼容 API 1。不过,我真的很困惑如何做到这一点。有些旧教程已被弃用,有些新教程不支持旧的 API 级别。根据这个SO https://stackoverflow.com/questions/6391870/how-exactly-to-use-notification-builder问题,我应该使用通知兼容生成器 http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html。我正在使用一个示例,但我不完全理解代码。
从这段代码中可以看出:
Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
YOUR_PI_REQ_CODE, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.some_img)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
.setTicker(res.getString(R.string.your_ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.your_notif_title))
.setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();
nm.notify(YOUR_NOTIF_ID, n);
我在下面看到红线:ctx
, YOUR_PI_REQ_CODE
, and YOUR_NOTIF_ID
The ctx
变量旨在成为一个 Android 上下文——通常是一个 Activity(或者实际上是一个扩展 Activity 的类)。
你应该做一些研究PendingIntent
类来理解YOUR_PI_REQ_CODE
但您需要确定放在这里的内容;它是your待处理的意图请求代码。
您还应该研究NotificationManager
notify()
方法来确定您要使用的内容your通知ID。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)