我创建了一个WebService
类来处理对Web服务的多个不同请求,此类使用不同key
的广播和意图,具体取决于WebService
类中最初调用的方法。我坚持如何在WebServiceReceiver
...
以下是WebService
:
//Broadcast the intent with data received from service call.
broadcastIntent.putExtra(broadcastIntentKeyName, response.toString());
sendBroadcast(broadcastIntent);
这是我的onReceive
:
public class WebServiceReceiver extends BroadcastReceiver
{
public WebServiceReceiver() {}
@Override
public void onReceive(Context context, Intent intent) {
Debug.waitForDebugger();
//NOTE: Not sure if i'm approaching this the right way, sure doesn't seem like it...
//Its possible that some of these will be NULL.
String GetRequestForRouteWithDriverId_DATA = intent.getStringExtra("Helper_GetRequestForRouteWithDriverId");
String StoreDataInServer_DATA = intent.getStringExtra("Helper_StoreDataInServer");
String SubmitDriverRouteData_DATA = intent.getStringExtra("Helper_SubmitDriverRouteData");
MainActivity.getInstance().updatetextViewControl(GetRequestForRouteWithDriverId_DATA);
}
}
我所做的工作,但就像我在代码评论中提到的那样,它并不像是正确的方式。
有没有更好的方法来解决这个问题?我只是想重新使用此onReceive
来处理所有WebService
广播。
答案 0 :(得分:2)
也许您可以使用Intent.setAction()
方法
broadcastIntent.setAction("Your action");
broadcastIntent.putExtra("Your Extra");
sendBroadcast(broadcastIntent);
然后在你的接收器上......
public class WebServiceReceiver extends BroadcastReceiver
{
public WebServiceReceiver() {}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("Your Action")){
String yourExtra = intent.getStringExtra("Your extra")
}
}
}