当应用程序从另一个应用程序运行时,Android重新启

时间:2016-04-07 13:36:11

标签: android broadcastreceiver intentservice

我使用IntentService对服务器执行API请求。应用程序启动时,IntentService始终启动,执行同步并向我的BroadcastReceiver发送广播。我也可以从其他应用程序发送照片。当我这样做时,我想重新启动我的IntentService即使它没有完成它的工作。我试过打电话

stopService(new Intent(this, SyncApiService.class));
startService(new Intent(this, SyncApiService.class));

但是,正如我所理解的,这种方法不起作用,因为onReceive方法调用了两次。有没有办法重启IntentService?

我在onCreate的{​​{1}}中调用此方法:

Activity

我想在private void requestForMemmoryPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_STORAGE); } else { SyncApiService.startActionFullSync(this, AuthPreferences.getAuthToken(this), AuthPreferences.getDeviceToken(this), false); } } 方法中重启服务:

onNewIntent

方法@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); readDataFromIntentFilter(intent); SyncApiService.stop(this); requestForAccessNetworkStatePermission(); } 内部调用requestForAccessNetworkStatePermission。这是我的服务:

requestForMemoryPermission

所以我希望在public class SyncApiService extends IntentService { public SyncApiServive() { super("SyncApiService"); } public static void stop(Context context) { Intent intent = new Intent(context, SyncApiService.class); context.stopService(intent); } /** * Starting of full synchronization process * @param context * @param authToken * @param deviceId * @param neddImport */ public static void startActionFullSync(Context context, String authToken, String deviceId, boolean neddImport) { Intent intent = new Intent(context, SyncApiService.class); intent.setAction(ACTION_FULL_SYNC); intent.putExtra(EXTRA_AUTH_TOKEN, authToken); intent.putExtra(EXTRA_DEVICE_ID, deviceId); intent.putExtra(EXTRA_NEED_IMPORT, neddImport); context.startService(intent); } @Override protected void onHandleIntent(Intent intent) { if (intent != null) { final String action = intent.getAction(); switch (action) { case ACTION_FULL_SYNC: String authToken = intent.getStringExtra(EXTRA_AUTH_TOKEN); String deviceId = intent.getStringExtra(EXTRA_DEVICE_ID); boolean needImport = intent.getBooleanExtra(EXTRA_NEED_IMPORT, false); handleActionFullSync(authToken, deviceId, needImport, action); break; } } } @Override public void onDestroy() { super.onDestroy(); Log.d(API_TAG, "!!!onDestroy!!!"); NotificationHelper.cancel(getApplicationContext(), NotificationHelper.SYNC_NOTIFICATION); } private void handleActionFullSync(String authToken, String deviceId, boolean needImport, String action) { // TODO: full sync process //NotificationHelper.showSyncNotification(getApplicationContext(), R.string.sync_notification, R.string.cloud_sync, R.string.cloud_sync); //startCloudSync(authToken, deviceId); NotificationHelper.showSyncNotification(getApplicationContext(), R.string.sync_notification, R.string.photo_sync, R.string.photo_sync); startPhotoSync(authToken, deviceId, needImport); } private void startPhotoSync(String authToken, String deviceId, boolean needImport) { getOldPhotoSync(authToken, deviceId); if (needImport) uploadNewPhotoSync(authToken, deviceId); else { Log.i(API_TAG, "!!!NO IMPORT!!!"); NotificationHelper.cancel(getApplicationContext(), NotificationHelper.SYNC_NOTIFICATION); Intent intent = new Intent(AppActivity.SYNC_FILTER); intent.putExtra(EXTRA_NEED_IMPORT, needImport); LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent); } } 调用

时停止此操作

0 个答案:

没有答案