Reputation: 217
I am getting the NPE while starting the service. I have just gone through the service tutorial on android developer site.
Logs are showing unable to resume activity...
@Override
protected void onResume() {
super.onResume();
CustomIntentService cis = new CustomIntentService();
Intent intent1 = new Intent(this, CustomIntentService.class);
intent1.putExtra("NUM", 1);
cis.startService(intent1);
}
My service is :
public class CustomIntentService extends IntentService {
private final static String TAG = "CustomIntentService";
public CustomIntentService() {
super("CustomIntentService");
Log.d(TAG,"out CustomIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent");
Log.d(TAG, "service num = " + intent.getIntExtra("NUM", 0));
if (Looper.getMainLooper() == Looper.myLooper()) {
Log.d(TAG, "In main ui thread");
} else {
Log.d(TAG, "In worker thread");
}
}
}
Upvotes: 0
Views: 688
Reputation:
Change your code for onResume to the following:
@Override
protected void onResume() {
super.onResume();
//CustomIntentService cis = new CustomIntentService();
Intent intent1 = new Intent(this, CustomIntentService.class);
intent1.putExtra("NUM", 1);
startService(intent1);
}
This should fix the issue, remember intent knows which service to start and startService() is called on context. So here activity's instance will be the context.
Also,
Since Service is a component, so you should be declaring it in the AndroidManifestFile
<service
android:name=".CustomIntentService">
</service>
*Note: CustomIntentService should be under current directory, or you can supply absolute path also.
You can refer this
Upvotes: 2