gpt4 book ai didi

java - 安卓通知

转载 作者:行者123 更新时间:2023-11-30 01:13:03 28 4
gpt4 key购买 nike

好吧,我正在尝试使用通知,但这段代码不起作用。我已经在 4.4 和 5.0 上测试过了。我不明白,怎么了。

public void onClick(View view) {
Context context = getApplicationContext();

Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

Resources res = context.getResources();
Notification.Builder builder = new Notification.Builder(context);

builder.setContentIntent(contentIntent)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Title")
.setContentText("Text");

Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFY_ID, notification);
}

我将不胜感激。

最佳答案

这可能是因为 Google 在每个 Android 版本中对其通知 API 进行了大量更改。所以你使用的 API 不兼容多 Android 版本。但是 Google 发布了 appcompat-v7\appcompat-v4 来解决这个问题。

试试下面的代码:

public void send(View v) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Context context = getApplicationContext();
Intent notificationIntent = new Intent(context, TestNotificationActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Notification notification = builder
.setContentIntent(contentIntent)
.setContentTitle("this is title")
.setContentText("this is content")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
manager.notify(NOTIFY_ID, notification);
}

记得导入android.support.v7.app.NotificationCompat。

关于java - 安卓通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38236576/

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