我似乎无法启动和运行这些:
public class BackgroundSyncService extends IntentService {
public BackgroundSyncService() {
super("SchedulingService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i("asd-service", "lol");
BackgroundSyncBroadcastReceiver.completeWakefulIntent(intent);
}
}
第二课:
public class BackgroundSyncBroadcastReceiver extends WakefulBroadcastReceiver {
private AlarmManager alarmManager;
private PendingIntent alarmIntent;
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, BackgroundSyncService.class);
startWakefulService(context, service);
}
public void setAlarm(Context context){
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, BackgroundSyncBroadcastReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
0, 5000, alarmIntent);
ComponentName receiver = new ComponentName(context, BackgroundSyncBroadcastReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
}
第三课:
public class BackgroundSyncBootReceiver extends BroadcastReceiver {
BackgroundSyncBroadcastReceiver service = new BackgroundSyncBroadcastReceiver();
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
service.setAlarm(context);
}
}
}
的Manifest.xml:
<receiver android:name=".BackgroundSyncBroadcastReceiver"/>
<receiver android:name=".BackgroundSyncBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".BackgroundSyncService"/>
答案 0 :(得分:0)
在您的应用程序被用户手动启动一次之前,您的BOOT_COMPLETED接收器将不会被激活。这是一项安全功能,可防止应用在用户不知情的情况下运行。
答案 1 :(得分:0)
所以,是的,我忘了从活动开始......
BackgroundSyncBroadcastReceiver service = new BackgroundSyncBroadcastReceiver();
service.setAlarm(this);
愚蠢的错误。