java - Android IntentService无法启动通知

标签 java android

这是我的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/

相关文章:

java - 抽屉导航中的可折叠和不可折叠菜单

java - 如何查找我的 Spring Boot 应用程序每秒收到多少个请求?

android - 使用 gradle 将 android 库发布到 nexus

java - Arduino 和 Android UDP 数据报

关于获取上下文的 Android ActivityInstrumentationTestCase2 NullPointerException

java - Android线程有时不启动

Node.js 中 Java 的 RSA/ECB/OAEPWithSHA-256AndMGF1Padding 等效项

java - 如何让程序从java终端接受一行不同的输入类型?

java - Jruby 堆栈跟踪

android - 使用 Air android 视频播放重新打开设备屏幕后出现黑屏