我开始一个IntentService
以下方法:IntentService的onCreate()调用,但onHandleIntent()不
private void startMyService(Data data) {
Intent intent = new Intent(this, MyService.class);
intent.putExtra(KEY_DATA, data.toJson());
startService(intent);
}
的Data
类:
public class Data extends ArrayList<MyObject> {
public Data() {
super();
}
public Data(Collection<MyObject> myObjects) {
super(myObjects);
}
public String toJson() {
return new Gson().toJson(this);
}
public static Data fromJson(String jsonString) {
return new Gson().fromJson(jsonString, Data.class);
}
}
的IntentService
的相关部分:
public class MyService extends IntentService {
private Data data;
public MyService() {
super("myServiceName");
}
@Override
public void onCreate() {
super.onCreate();
// this gets called properly
Log.d("myTag", "Service onCreate()");
}
@Override
protected void onHandleIntent(Intent intent) {
// this is not called in case of the bigger dataset
Log.d("myTag", "Service onHandleIntent()");
String dataJson = intent.getStringExtra(KEY_DATA);
data = Data.fromJson(dataJson);
// doing stuff with data
}
}
我有2个测试场景:
- 数据保持2000个对象
- 数据保持4000个对象
随着2000和目的,Service
运行完美无缺。
对于4000个对象,调用Service
的onCreate()
方法,就是这样... onHandleIntent()
未被调用。一段时间后,该应用只是抛出一个ANR。
我已经与上onHandleIntent()
第一线既Log.d()
电话和断点测试,它是不是在所有的时候Data
持有4000个对象调用。
TransactionTooLargeException
是不是抛出。
我没有得到Exception
,根本不知道有什么问题。
这种行为的原因是什么?
尝试在sqlite中保存json,以便您可以通过调用intentService类中的方法直接访问它 – Manifest
可能是[SO帖子](http://stackoverflow.com/a/14379016/5015207)会有帮助:如果Binder事务失败,似乎不会总是有TransactionTooLargeException – 0X0nosugar