以前,我使用IntentService
通过AlaramManager触发后台服务。现在将IntentService
更改为JobIntentService
,以触发后台进程从服务器获取数据。在某些JobIntentService
未触发的设备中,仅在打开应用程序后才会触发。
设置AlaramManager
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi;
AlarmManager am;
pi = PendingIntent.getBroadcast(getActivity(), requestCode, new Intent(getActivity(), PendingCampaignReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
BroadCastReceiver类
public class PendingCampaignReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PendingCampaign.enqueueWork(context, intent);
}
}
<receiver
android:name=".broadcast.receiver.PendingCampaignReceiver"
android:enabled="true"
android:process=":remote" />
JobIntentService类
public class PendingCampaign extends JobIntentService {
public static void enqueueWork(Context context, Intent intent) {
enqueueWork(context, PendingCampaign.class, PENDING_CAMPAIGN_JOB_ID, intent);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
//am doing my API calling here
}
}
<service
android:name=".services.PendingCampaign"
android:permission="android.permission.BIND_JOB_SERVICE" />
我的编码中是否有任何问题。帮我解决问题。