将一个值从broadcastreceiver类传递到wifi直接的主要活动

时间:2014-07-20 10:17:33

标签: java android eclipse wifi-direct wifip2p

我想将字符串变量结果中的值( WiFiDirectBroadcastReceiver )发送到 WiFiDirectActivity ,我尝试按照所示进行操作在this.

但是当我运行应用程序时,它一直在寻找同行。当我评论那些添加的行(WiFiDirectBroadcastReceiver的最后3行)时,它工作正常。

WiFiDirectBroadcastReceiver中的 onReceive 功能代码以及WiFiDirectActivity中的 onResume()代码将被添加。

我如何将该值传递给WiFiDirectActivity?我在这里失踪了什么?

WiFiDirectBroadcastReceiver.java

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

      //if (MyIntentService.ACTION_MyIntentService.equals(action))
     // {
        //result = intent.getStringExtra(MyIntentService.EXTRA_KEY_OUT);
          result = "hello";
      //}

    if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {

        // UI update to indicate wifi p2p status.
        int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
        if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
            // Wifi Direct mode is enabled
            activity.setIsWifiP2pEnabled(true);
        } else {
            activity.setIsWifiP2pEnabled(false);
            activity.resetData();

        }
        Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
    } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

        // request available peers from the wifi p2p manager. This is an
        // asynchronous call and the calling activity is notified with a
        // callback on PeerListListener.onPeersAvailable()
        if (manager != null) {
            manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager()
                    .findFragmentById(R.id.frag_list));
        }
        Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
    } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {

        if (manager == null) {
            return;
        }

        NetworkInfo networkInfo = (NetworkInfo) intent
                .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);

        if (networkInfo.isConnected()) {

            // we are connected with the other device, request connection
            // info to find group owner IP

            DeviceDetailFragment fragment = (DeviceDetailFragment) activity
                    .getFragmentManager().findFragmentById(R.id.frag_detail);
            manager.requestConnectionInfo(channel, fragment);
            //fragment.peerCountInfo=result;
            //fragment.peerCt = peerCountFromDlist;
            //fragment.peerNm = peerNameFromDlist;
        } else {
            // It's a disconnect
            activity.resetData();
        }
    } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
        DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
                .findFragmentById(R.id.frag_list);
        fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
                WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
        //peerCountFromDlist = fragment.peerTot;
        //peerNameFromDlist = fragment.deviceid;

    }

    intent.putExtra("message",result);
    intent.setClass(context, WiFiDirectActivity.class);
    context.startActivity(intent);
}

WiFiDirectActivity.java

public class WiFiDirectActivity extends Activity implements ChannelListener, DeviceActionListener {

public static final String TAG = "wifidirectdemo"; private WifiP2pManager manager; private boolean isWifiP2pEnabled = false; private boolean retryChannel = false; private final IntentFilter intentFilter = new IntentFilter(); private Channel channel; private BroadcastReceiver receiver = null; private String resultString; private int peerCount; /** * @param isWifiP2pEnabled the isWifiP2pEnabled to set */ public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) { this.isWifiP2pEnabled = isWifiP2pEnabled; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // add necessary intent values to be matched. intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); intentFilter.addAction(MyIntentService.ACTION_MyIntentService); manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); channel = manager.initialize(this, getMainLooper(), null); } /** register the BroadcastReceiver with the intent values to be matched */ @Override public void onResume() { super.onResume(); receiver = new WiFiDirectBroadcastReceiver(manager, channel, this); registerReceiver(receiver, intentFilter); Bundle extras = getIntent().getExtras(); if (extras != null) { resultString = extras.getString("message"); } }

1 个答案:

答案 0 :(得分:0)

我发现这种简单的方法可以将一些数据从BroadcastReceiver发送到Main Activity,这要归功于Mike M.(同样 setIsWifiP2pEnabled(boolean isWifiP2pEnabled)方法可行)

发送"message"并将其检索到str变量;

在MainActivity中创建一个公共方法:

   public void setResult(String result){
          str = result;
}

然后在BroadcastReceiver中调用它:

activity.setResult("message");