java - 除非启动应用程序,否则手机重启后 IntentService 不会自动启动

标签 java android broadcastreceiver android-intentservice

我试图在特定时间发送通知,这部分工作正常,但在我重新启动手机后,除非打开应用程序然后启动服务,否则服务不会启动。我尝试了网上的多种解决方案,但仍然无法解决问题。

Activity :

private void startNotificationAlarm() {

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, ReminderBroadcastReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), intent, 0);
    Objects.requireNonNull(alarmManager).setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

广播接收器:

public class ReminderBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent serviceIntent = new Intent(context, BootService.class);
        intent.setAction("<package>.Receiver");
        context.startService(serviceIntent);

    } else {

        scheduleNotification(context, intent);
    }
}

private void scheduleNotification(Context context, Intent intent) {

    Notification notification = new NotificationCompat.Builder(context, BaseApp.CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_calendar_alert)
            .setContentTitle(title)
            .setContentText(message)
            .setColor(color)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_REMINDER)
            .setAutoCancel(true)
            .setOnlyAlertOnce(false)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Objects.requireNonNull(notificationManager).notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notification);
}

IntentService:

public class BootService extends IntentService {

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

@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        try {

            Thread.sleep(5000);
            startNotification();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
  }
}

list

<receiver
        android:name=".Receiver.ReminderBroadcastReceiver" android:enabled="true" android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

    <service android:name=".Service.BootService"/>

最佳答案

您必须在 list 中提供以下权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

此外,您还必须将 android:exported 设置为 true 根据 Android 文档:

android:exported

Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.

示例代码

<receiver android:name=".sample.BootReceiver"
          android:enabled="true"
          android:exported="true">
     <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
     </intent-filter>
</receiver>

关于java - 除非启动应用程序,否则手机重启后 IntentService 不会自动启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57638656/

相关文章:

java - 删除节点

android - 私有(private)调度TouchEvent?

android - 使用 EXTRA_PLUGGED 获取 USB 电缆插入 IN/OUT 事件不起作用

android - Processing 可以处理多点触控吗?

android - BroadcastReceiver BATTERY_LOW 在显示低电量警告时崩溃

android - Android BOOT_COMPLETED 和类别 HOME 之间的区别

java - 如何在 Android 中以编程方式获取我自己的电话号码?

java - 如何使用 EAR 中提供的依赖项取代 Glassfish 内置库?

Java 字符串到 Int

java - 如何在约束布局中以编程方式移动 View (按钮)?