如何上传图片,即使应用程序在Android中死亡
我打电话给IntentService
上传图片api,在后台运行,并将图片上传到服务器。如何上传图片,即使应用程序在Android中死亡
IntentService,onHandleEvent
方法被调用并在后台运行,我所了解的是IntentService
会执行任务并调用stopSelf()
方法。
在我的应用程序上传时,当我杀了我的应用程序,上传被终止,IntentService
停止完成上传任务。
我怎样才能让我的IntentService
即使该应用程序被终止运行?
编辑1:我试着用粘性的服务,当我杀死服务重新启动应用程序,并传递给onStartCommand
方法意图数据为空
你可以试试这个下面的代码。首先你需要在清单文件中添加服务的性质
<service
android:name=".service.Service"
android:enabled="true"
android:icon="@drawable/ic_launcher"
android:isolatedProcess="true">
</service>
而且也是你的服务添加START_STICKY
的。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
如在下面的步骤中提到保持运行所有的时间
1)在服务onStartCommand方法的返回START_STICKY的服务,您可以创建一个服务。
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
2)使用startService(则将MyService),使其始终保持活跃,无论绑定的客户端的数量在后台启动该服务。
Intent intent = new Intent(this, PowerMeterService.class);
startService(intent);
3)创建活页夹。
public class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
4)定义一个服务连接。
private ServiceConnection m_serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
m_service = ((MyService.MyBinder)service).getService();
}
public void onServiceDisconnected(ComponentName className) {
m_service = null;
}
};
5)使用bindService绑定到服务。
Intent intent = new Intent(this, MyService.class);
bindService(intent, m_serviceConnection, BIND_AUTO_CREATE);
开始粘滞,它会发送意图数据服务一旦我杀了应用程序?它将如何重新启动服务? – Praneeth
使用粘滞服务 –