How to pass context
to a service with putExtra
?
The problem is : android.os.Application
cannot be cast to java.io.Serializable
How to solve this problem?
// main activity
public void btnGetLocate() {
Intent intent = new Intent(this, Sleeper.class);
intent.putExtra("context", (Serializable) getApplicationContext());
startService(intent);
}
// another file
public class MyService extends IntentService {
public Sleeper() {
super("MyService");
}
protected void onHandleIntent(Intent intent) {
Context context = (Context) intent.getExtras().getSerializable("context"); // make Error, main problem
MyClass mc = new MyClass(context);
...
}
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "destroyed"), Toast.LENGTH_SHORT).show();
}
}