android - 在应用程序关闭后启动带有警报的服务

标签 android android-intent alarmmanager android-broadcast intentservice

我正在制作一个简单的应用程序,其中包含一项每天多次弹出通知的服务。我正在尝试用闹钟来做这个。如果我不关闭应用程序,一切都会完美无缺。但是,如果我在接收器被触发时关闭应用程序,我会收到“应用程序已停止工作”或其他消息。

我在想是不是因为没有应用程序的 Activity 或其他什么东西,服务就无法工作?有什么见解吗?

这是我的代码:

接收者:

public class AlarmReceiver extends BroadcastReceiver {
    public static final int REQUEST_CODE = 12345;
    public static final String ACTION = "com.ddimitrovd.hap4eapp4e.alarm";

    // Triggered by the Alarm periodically (starts the service to run task)
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MainIntentService.class);
        i.putExtra("foo", "bar");
        context.startService(i);
    }
}

这是服务:

public class MainIntentService extends IntentService {

    int noteID = 1;
    String chanelID;

    public MainIntentService() {
        super("MainIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        chanelID = getString(R.string.channel_ID);
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();

        // Do the task here
        createNotificationChannel();
        popNotif();
    }

    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    }

    private void popNotif() {
        // Create an explicit intent for an Activity in your app
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, chanelID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("My notification")
                .setContentText("Hello World!")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                // Set the intent that will fire when the user taps the notification
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        notificationManager.notify(noteID, mBuilder.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) {
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(chanelID, 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);
        }
    }
}

我确定警报会触发接收器。

谢谢!

最佳答案

我想我找到了问题所在。我估计我的 IntentService 不能执行复杂,因为 BroadcastReceiver 进程在它可以执行之前就被杀死了。更多信息 here . 我所做的只是在接收器中弹出我的通知,但我想更好的解决方案是启动另一个线程或将其异步。

关于android - 在应用程序关闭后启动带有警报的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50556471/

相关文章:

android - 在 android 的 Alarm Manager 中使用 intent extras 传递值

java - Android Studio 中的 Intent 问题

java - fragment Activity 不膨胀

android - 如何保护后台服务/警报在自定义操作系统(如 oppo - coloros、vivo-funtouch os、Xiomi-MIUI os)中被杀死?

android - 如何为抽屉导航中的每个滑动选项卡制作 fragment

java - 从 Activity 2 到 Activity 1 进行通信并更新列表

android - Android 警报在过去执行了多远(以及为什么!)?

Android subview 高度与 ListView 项目中的父项不匹配

java - 锁屏小部件不工作并且不显示任何内容

android - 广告叠加层需要在 intent extras 中使用 useClientJar 标志。