我正在处理应用程序,我需要在重新启动时设置警报以在特定时间间隔内启动服务。
我已创建所需的Boot Complete Receiver类,在清单文件中添加了uses-permission和定义的接收器。我已经在每台设备上的许多真实和模拟设备上进行了测试,接收器的工作情况除了使用Android 5.1.1的海信设备外
我已经尝试过这些解决方案。
broadcastreceiver-not-receiving-boot-completed
boot-completed-not-working-android
android-boot-completed-not-received-when-application-is-closed
我还删除了android:installLocation="auto"
并在某个帖子中添加了android:directBootAware="true" android:enabled="true" android:exported="true"
。
请在下面找到Receiver类和清单代码。
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Set instance Alarm on boot
Calendar calendar = Calendar.getInstance();
Intent messageCheckerIntent = new Intent(context, MessageCheckerService.class);
PendingIntent pendingIntent =
PendingIntent.getService(context, REQ_MESSAGE_CHECK,
messageCheckerIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Cancel if any existing alarm for given pendingIntent (MessageCheckerService)
alarmManager.cancel(pendingIntent);
// Alarm is reset every time when this service is called,
// still keep alarm repeating so if one call fails does not affect following calls
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
ALARM_INTERVAL, pendingIntent);
}
}
清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.project.myapp">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup">
<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask">
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<service android:name=".GPSTrack" />
<service
android:name=".MessageCheckerService"
android:enabled="true" />
<receiver
android:name=".BootReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
</manifest>