0
我试图从一个活动,从服务器获取数据,并尝试使用广播接收器获取另一个活动中的数据,但广播接收器类没有被调用,我有意图服务注册我的服务类清单还我的广播接收器类不被称为
这里是我的代码来启动服务
Intent intent = new Intent(LoginActivity.this, MyWebRequestService.class);
startService(intent);
类MyWebRequestService
//inside on handle intent
new AsynchCall().execute();
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MyWebRequestReceiver.PROCESS_RESPONSE);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
//here I am getting the data in my arraylist
broadcastIntent.putStringArrayListExtra("namelist", servicelist);
sendBroadcast(broadcastIntent);
的活动,我想收到我的数据
IntentFilter filter =IntentFilter(MyWebRequestReceiver.PROCESS_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MyWebRequestReceiver();
registerReceiver(receiver, filter);
在相同的活动我已经写了我的广播接收器类
public class MyWebRequestReceiver extends BroadcastReceiver
{
public static final String PROCESS_RESPONS = "com.rentpro.PROCESS_RESPONSE";
@Override
public void onReceive(Context context, Intent intent)
{
listofnames = intent.getStringArrayListExtra("namelist");
}
}
A也请确保你的应用不会通过调用'unregisterReceiver()'来泄漏接收者,最好是在你的activity的onPause()方法中。 – Siri0S
是的,我已经在onPause中调用了它,但是如何在sendBroadcast(broadcastIntent)之前调用registerReceiver(receiver,filter);在我的情况下,我已经参考了这个链接[链接](http://www.mysamplecode.com/2011/10/android-intentservice-example-using.html) –