I just can't get what's wrong here.
Activity.java:
...
Intent intent = new Intent(Activity.this, Service.class);
intent.putExtra(Service.KEY_TEST, "123456789");
startService(intent);
...
Service.java:
...
private Intent intent;
public static final String KEY_TEST;
@Override
public void onCreate() {
super.onCreate();
Log.d("TEST", intent.getStringExtra(KEY_TEST)); // when I remove this line,
// it works, otherwise gives NullPointerException and FC's
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.intent = intent;
return START_STICKY;
}
...
The Service clearly doesn't receive the extras sent from the Activity. When I try to get any extras I sent before, the app force closes and LogCat gives NullPointerException. When I remove the line where I try to get the extras, the app doesn't force close, but I don't obviously receive the extras either.
put this line Log.d("TEST", intent.getStringExtra(KEY_TEST));
in,
onStartCommand(Intent intent, int flags, int startId)
method..
EDIT:
For more info how the service life cyclces follows, Look at this Service Lifecycle.