1

我创建了一个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广播。

4

1 回答 1

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")
        }

    }
}
于 2015-12-18T22:37:53.073 回答