gpt4 book ai didi

java - Android IntentService无法启动通知

转载 作者:行者123 更新时间:2023-12-01 18:39:06 25 4
gpt4 key购买 nike

这是我的full code

启动通知在 Service 中运行良好,但是当我将服务更改为 IntentService 时,通知不显示,这是我的服务:

public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}

Context ctx;
String channelId;

@Override
protected void onHandleIntent(Intent intent) {
ctx = getApplicationContext();
channelId = getPackageName() + " Channel";
createNotificationChannel();
startForeground(1, getNotification());
}

Notification getNotification() {
return new NotificationCompat.Builder(ctx, channelId)
.setContentTitle("Title")
.setContentText("Content")
.setSmallIcon(R.drawable.ic_android_black_24dp)
.build();
}

private void createNotificationChannel() {

// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = channelId + " Name";
String description = channelId + " Desc";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channelId, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}

我使用以下代码启动服务:

startService(new Intent(this, MyIntentService.class))

最佳答案

我找到了解决办法

IntentService无法使用startforeground,因为IntentService会在onHandleIntent之后停止,因此无法长时间前台通知(参见StartForeground for IntentService)

如果我想使用startForeground,请使用Service而不是IntentSerivce

如果我想在IntentService中使用通知,请使用NotificationManager#notify,请参阅以下代码:

public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}

Context ctx;
String channelId;

@Override
protected void onHandleIntent(Intent intent) {
initNotification();
}

private void initNotification() {
ctx = getApplicationContext();
channelId = getPackageName() + " Channel";
createNotificationChannel();
// startForeground(1, getNotification());
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1, getNotification());
}

Notification getNotification() {
return new NotificationCompat.Builder(ctx, channelId)
.setContentTitle("Title")
.setContentText("Content")
.setSmallIcon(R.drawable.ic_android_black_24dp)
.build();
}

private void createNotificationChannel() {

// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = channelId + " Name";
String description = channelId + " Desc";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channelId, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}

关于java - Android IntentService无法启动通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59979274/

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