I am passing intent to a service from two different locations in my application. One intent has a bundle from which I want to extract data.But I get NPE when I try run getIntent().getExtras() method.
EDIT: In one class:
basket.putString("KEY1", id[i]);
basket.putString("KEY2", message[i]);
basket.putString("KEY3", timeFormat[i]);
passNotiData = new Intent(getActivity(),
CheckService.class);
passNotiData.putExtras(basket);
startService(passNotiData);
In another class,
Intent myIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, myIntent, 0);
context.startService(myIntent);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 60000, PERIOD, pi);
And in service class,
Bundle gotBasket = intent.getExtras();
oldId = gotBasket.getString("KEY1");
oldTime = gotBasket.getString("KEY3");
//start service from activity or any place
Intent intent = new Intent(this, MyService.class);
Bundle bundle = new Bundle();
bundle.putString("TEST", "test got it.");
intent.putExtras(bundle);
startService(intent);
//In service class declare override method of service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
if (intent != null && intent.getExtras() != null) {
String str = intent.getExtras().getString("TEST");
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG)
.show();
}
return START_NOT_STICKY;
}
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.