IntentService未启动onBoot

时间:2016-02-23 15:11:21

标签: android android-service alarmmanager intentservice android-intentservice

我正在开发一款可将您的本地日期与云同步的应用。所以我需要在O.S.时自动检查。启动我的本地数据以获取要上传到云的新相机文件。并且每10分钟进行一次本地数据检查,以获取新文件并将其上传到云端。

我遇到了服务方面的问题,因为它只在我打开应用程序时启动,之后我可以关闭这个应用程序并保持在后台(好吧!)。但是我需要在操作系统时启动服务也开始了。

我在Manifest.xml中定义了:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service android:name=".receiver.UploadDeleteService"   android:exported="false"/>

    <receiver
        android:name=".receiver.AlarmReceiver"   android:process=":remote"  >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

我还使用Interactor(来自MVP架构)定义警报管理器和时间段:

public class ServiceInteractorImpl implements ServiceInteractor {

private Context context;

public ServiceInteractorImpl(Context context){
    this.context = context;
}

@Override
public void launchService() {

    // Construct an intent that will execute the AlarmReceiver
    Intent intent =  new Intent(context, AlarmReceiver.class);
    // Create a PendingIntent to be triggered when the alarm goes off
    final PendingIntent pIntent = PendingIntent.getBroadcast(context, AlarmReceiver.REQUEST_CODE,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    long firstMillis = System.currentTimeMillis(); // alarm is set right away
    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    // First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
    // Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 30);
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
            60*1000, pIntent);

}

}

我的AlarmReceiver是:

public class AlarmReceiver  extends   BroadcastReceiver {
    public static final int REQUEST_CODE = 12345;

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, UploadDeleteService.class);
         context.startService(i);
    }

}

我的服务:

public class UploadDeleteService extends IntentService implements ApiConnector.GetObjectListener{

private RemoteInteractor remoteInteractor;
private LocalInteractor localInteractor;
private LocalDBInteractor localDBInteractor;
List<String> pathsToUpload;
private String uploading;

public UploadDeleteService(String name) {
    super(name);
}

public UploadDeleteService() {
    super("UpdateDeleteService");
}

@Override
protected void onHandleIntent(Intent intent) {
    localInteractor = new LocalInteractorImpl(getApplicationContext());
    localDBInteractor = new LocalDBInteractorImpl(getApplicationContext());
    remoteInteractor = new RemoteInteractorImpl(getApplicationContext());
    uploading = "";
    remoteInteractor.checkNews(this);
}

}

希望有助于理解我的麻烦......非常感谢!

1 个答案:

答案 0 :(得分:0)

好的,首先感谢您的所有答案。我采取了混合并得到了解决方案:

在我的接收器中,我在WakefulBroadcastReceiver中添加了下一行,以便在手机再次启动时设置AlarmManger:

 @Override
    public void onReceive(Context context, Intent intent) {
        if (intent!=null &&  intent.getAction()!=null && intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Intent i =  new Intent(context, AlarmReceiver.class);

            final PendingIntent pIntent = PendingIntent.getBroadcast(context, AlarmReceiver.REQUEST_CODE,
                    i, PendingIntent.FLAG_UPDATE_CURRENT);
            long firstMillis = System.currentTimeMillis(); // alarm is set right away
            AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
            alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
                    30 * 1000, pIntent); 
        }else{ 
            Intent i = new Intent(context, UploadDeleteService.class);
            context.startService(i);
            startWakefulService(context, i);  

        }
    }

我在服务中添加了:

@覆盖     protected void onHandleIntent(Intent intent){

    remoteInteractor.checkNews(this);
    AlarmReceiver.completeWakefulIntent(intent);//Very important to link with your WakefulBroadcastReceiver
}