JobIntentService在某些设备中无法正常启动?

时间:2018-10-26 09:56:20

标签: android android-intentservice jobintentservice

以前,我使用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" />

我的编码中是否有任何问题。帮我解决问题。

0 个答案:

没有答案