我正在使用IntentService将dataData保存到SQLDatabase&将数据上传到Parse。如果我保持应用程序运行,IntentService工作正常。如果我最小化它(通过按下主页按钮)它也会执行。
但是,如果我按导航按钮并破坏应用程序(应用程序屏幕缩小并将其滑落的东西);然后IntentService停止运行(它甚至不调用onDestory)。
我想要的是让服务执行所有任务;和破坏本身。因此,START_STICKY(Intent)不是必需的,因为我不希望它在后台连续运行。
启动IntentService
Intent intent_publishService = new Intent(getApplicationContext(), PublishService.class);
Bundle basket_storyData = new Bundle();
basket_storyData.putAll( packStoryData() );
intent_publishService.putExtra(KEY_BASKET_STORY_DATA, basket_storyData);
intent_publishService.putParcelableArrayListExtra(KEY_BASKET_IMAGE_DATA, (ArrayList<? extends Parcelable>) final_image_data);
startService(intent_publishService);
PublishService.class (摘录)
@Override
protected void onHandleIntent(Intent intent)
{
Log.i(TAG, "ONHANDLEINTENT" );
context = getApplicationContext();
final String KEY_BASKET_IMAGE_DATA = "key_basket_image_data";
final String KEY_BASKET_STORY_DATA = "key_basket_story_data";
final String KEY_BASKET_EXTRA = "key_basket_extra";
ArrayList<ImagesData> _iDataSave = new ArrayList<ImagesData>();
_iDataSave.addAll( (Collection<? extends ImagesData>) intent.getParcelableArrayListExtra(KEY_BASKET_IMAGE_DATA) );
Log.i(TAG, "_iDataSize:" + Integer.toString(_iDataSave.size()) );
//Get Bundle Values
Bundle storyBundle = intent.getBundleExtra(KEY_BASKET_STORY_DATA);
publishStory(storyBundle, _iDataSave);
}
编辑1:清单声明
<service android:name="com.example.create.main.PublishService" />
解决
答案 0 :(得分:1)
在这种情况下,您需要START_STICKY来向Android表明您希望在应用程序被杀后继续尝试工作。
当用户从应用选择器中滑出时,将无条件地终止应用进程。您无法阻止这种情况发生 - 它旨在让用户控制当时正在运行的内容。
答案 1 :(得分:1)
以下是需要做的事情:
1)更改IntentService&gt;&gt;服务
2)将所有代码移至onStartCommand
3)使用单独的线程,以便一切都在UI-Thread
之外完成4)在Manifest中将其声明为一个单独的进程
PublishService.class (摘录)
@Override
public int onStartCommand(final Intent intent, int flags, int startId)
{
Log.i(TAG, "ONSTARTCOMMAND" );
Intent notificationIntent = new Intent(this, MyFriendList_Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("WhatsApp Notification");
builder.setContentText("You have a new message");
builder.setSmallIcon(R.drawable.ic_launcher);
//builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setNumber(100);
// Sets the progress indicator to a max value, the current completion
// percentage, and "determinate" state
builder.setProgress(0, 0, true);
Notification notificationFinal = builder.getNotification();
startForeground(ONGOING_NOTIFICATION_ID, notificationFinal );
Runnable r = new Runnable()
{
public void run()
{
context = getApplicationContext();
final String KEY_BASKET_IMAGE_DATA = "key_basket_image_data";
final String KEY_BASKET_STORY_DATA = "key_basket_story_data";
final String KEY_BASKET_EXTRA = "key_basket_extra";
ArrayList<ImagesData> _iDataSave = new ArrayList<ImagesData>();
_iDataSave.addAll( (Collection<? extends ImagesData>) intent.getParcelableArrayListExtra(KEY_BASKET_IMAGE_DATA) );
Log.i(TAG, "_iDataSize:" + Integer.toString(_iDataSave.size()) );
//Get Bundle Values
Bundle storyBundle = intent.getBundleExtra(KEY_BASKET_STORY_DATA);
publishStory(storyBundle, _iDataSave);
}
};
Thread t = new Thread(r);
t.start();
//return START_STICKY;
//return START_REDELIVER_INTENT;
//return START_CONTINUATION_MASK;
return START_NOT_STICKY;
}
明确宣言
<service
android:name="com.example.create.main.PublishService"
android:process=".publishservice" />
<强>关强>
startForeground()+单独的Process +在单独的Thread上运行
---也许所有3都不是必需的?
编辑1:另外;你可以跳过所有前景的东西;并返回START_CONTINUATION_MASK;但不推荐这样做,因为每个设备都有不同的处理返回类型的方式