I'm starting an IntentService
with the following method:
private void startMyService(Data data) {
Intent intent = new Intent(this, MyService.class);
intent.putExtra(KEY_DATA, data.toJson());
startService(intent);
}
The Data
class:
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);
}
}
The relevant part of the 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
}
}
I have 2 test scenarios:
With 2000 objects, the Service
runs flawlessly.
With 4000 objects, the onCreate()
method of the Service
is called and that's it... onHandleIntent()
is not called. The app just throws an ANR after a while.
I've tested with both Log.d()
calls and breakpoints on the first line of onHandleIntent()
, it is not called at all when Data
holds 4000 objects.
TransactionTooLargeException
is not thrown.
I get no Exception
s thrown at all, no idea what's wrong.
What could be the cause of this behaviour?
You may not see the TransactionTooLargeException
if you are filtering the logcat. There is a reasonable limit to the amount of data that you can pass in an Intent
. 4000 objects serialized to JSON is definitely too mucn! This is the wrong approach. Your application architecture is flawed.
You can simply store the 4000 objects in a static
variable so that the Service
can get to them without having to serialize them, pass them from Activity
to Service
, then deserialize them.
Alternatively, you need to serialize the 4000 objects to a file, or use a database to hold the data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.