我每隔5分钟安排一次报警管理器,它对我来说运行正常,但是当我重新启动设备时,警报管理器仅在每隔5分钟触发警报管理器后第一次触发。
在MainActivity中:
int DATA_FETCHER_RC = 123;
AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, DATA_FETCHER_RC, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int interval = 5 * 60 * 1000;
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
int minofday = calendar.get(Calendar.MINUTE);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minofday + 1);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND,0);
mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
在Receiver.Class中:
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alaram Started", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml如下:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".Receiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
答案 0 :(得分:0)
使用以下代码创建启动接收器:
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context pContext, Intent intent) {
// Do your work related to alarm manager
}
}
在您的清单中,注册此广播接收器:
<receiver
android:name="com.yourapp.BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
不要忘记在AndroidManifest.xml中添加权限:
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
答案 1 :(得分:0)
首先,您需要在清单中添加启动接收器和服务:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".Receiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:enabled="true"
android:process=":service"
android:exported="false"/>
实施服务和接收者:
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
BroadcastReceiver receiver = new Receiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
registerReceiver(receiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
}
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// receive
}
}