gpt4 book ai didi

android - 在 Android 中处理 API 10 上的通知

转载 作者:搜寻专家 更新时间:2023-11-01 08:58:47 25 4
gpt4 key购买 nike

我放入 onCreate() 的代码如下:

if(Build.VERSION.SDK_INT >= 11)
notifApiGT10();
else
notifApiLT10();

在哪里,

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void notifApiGT10() {
// TODO Auto-generated method stub
Notification.Builder builder ;
NotificationManager notifier;
builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setOngoing(true).setContentTitle("my Title").setContentText("my displayed text");

notifier = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

if(Build.VERSION.SDK_INT>=11 && Build.VERSION.SDK_INT<=15)
notifier.notify(1, builder.getNotification());
else if (Build.VERSION.SDK_INT > 15)
notifier.notify(1, builder.build());
}

private void notifApiLT10()
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentText("is actually in use")
.setContentTitle("my Title")
.setOngoing(true)
.setTicker("my ticker")
.setSmallIcon(R.drawable.ic_launcher);
Notification notif = builder.getNotification();
NotificationManager mN = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mN.notify(1, notif);

}

以上代码不适用于 API 10。我的设备正在运行 Gingerbread,但无法正常运行。我想知道为什么……有专家吗?

解决方案:

Notification notification = new Notification(R.drawable.ic_launcher, "my ticker", System.currentTimeMillis());
notification.flags = Notification.FLAG_ONGOING_EVENT;
Context context = getApplicationContext();
Intent i = new Intent(this,Main.class);
PendingIntent pd = PendingIntent.getActivity(this, 1, null, Intent.FLAG_ACTIVITY_NEW_TASK);


notification.setLatestEventInfo(context, "my title", "my text", pendingintent);
NotificationManager mN = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mN.notify(1,notification);

上面的解决方案解决了我的问题,即使您不想在点击时运行任何东西,您也应该将 pendingintent 放在 setLatestEventInfo 方法中,但是正如您所注意到的,在 pd 中,在第三个字段中,我已将 intent 设置为 null

最佳答案

您是否尝试过使用 NotificationCompat.Builder.build()? NotificationCompat.Builder.getNotification()已弃用。

仅供引用,上次我使用 NotificationCompat.Builder 时,setNumber() 无法正常工作,我最终直接在旧设备上构建 Notification。

编辑:尝试在旧设备上直接使用 Notification 类,因为 NotificationCompatImplBase (API <= 10) 无论设置了多少,都只使用构建器中的 4 个字段。

关于android - 在 Android 中处理 API 10 上的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16963522/

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