[英]How to get bundle from an intent if multiple intents are passed to the service
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.
一个意图有一个我想从中提取数据的包,但是当我尝试运行getIntent()。getExtras()方法时得到了NPE。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.