使用Service进行操作——异步任务

Using Service to carry out operation - asynchronous tasks

我在创建的库模块中设置了绑定服务。

如果这不是一个库模块,我的问题就很简单了,因为我需要对其他人来说是动态的。

当前应用程序使用库模块通过 Wifi-Direct 在两个 phone 之间搜索和创建连接(目前没有问题)。

我现在的问题是我的应用程序(我正在测试以确保库模块没有遗漏任何东西)不知道两个 phone 何时有 "connected"。

我尝试了一个 while 循环来不断请求内容并循环直到它不为空,这在技术上可行吗?但是我没有运气。

我正在考虑实现等待功能,但这是最后的手段!

我正在使用 android 指南创建我的库 Link


实际问题

我正在尝试获取有关连接的信息以显示给用户,显然,如果连接尚未建立,它将为空!从而引起我的问​​题。

我找到的一个解决方案是单步执行并手动等待另一个 phone 接受连接并成功连接,然后继续请求信息等。

如果有任何问题,请告诉我,因为我知道这很令人困惑!

更新:当您的库检测到成功的 P2P 连接时,您将希望在您的库中使用 sendBroadcast() 向您的应用程序发送一个意图。您可能希望仅当当前打开 Activity 时才在您的应用程序中接收意图。请参阅下面的新代码:

看到这被添加到建立了 P2P 连接的情况下,请注意您应该将 com.yourapp.example 替换为您的包名称:

  Intent i = new Intent("com.yourapp.example.P2PCONNECTED");
  context.sendBroadcast(i);

在您的库中定义 BroadcastReceiver 的代码:

    WiFiDirectFilter = new IntentFilter(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    WiFiDirectFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    WiFiDirectFilterBroadcastReceiver = new BroadcastReceiver() {            
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

           Log.i("MyApp", action);

            if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
                int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);   

                    if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {   
                        Log.i("MyApp", "WifiDirect WIFI_P2P_STATE_CHANGED_ACTION Enabled: true");
                        //WiFi Direct Enabled
                        //Do something....
                    } 
                    else { 
                        Log.i("MyApp", "WifiDirect WIFI_P2P_STATE_CHANGED_ACTION Enabled: false");
                        //WiFi Direct not enabled...
                        //Do something.....
                    }

            }
            else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
                   NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);   

                    if(networkInfo != null) {
                        boolean isWiFiDirectConnected = networkInfo.isConnected();
                        Log.i("MyApp", "WifiDirect WIFI_P2P_CONNECTION_CHANGED_ACTION Connected: " + );
                        if (isWiFiDirectConnected){
                           //WiFi Direct connected!
                           //Send Broadcast to your app
                           Intent i = new Intent("com.yourapp.example.P2PCONNECTED");
                           context.sendBroadcast(i);
                        }
                        else{
                           //WiFi Direct not connected
                           //Do something

                        }
                    }

            }
        }
    };

然后在您应用的任何 activity 或片段中,您需要在 onResume() 中注册并在 onPause() 中取消注册,请参见下面的代码:

 @Override
public void onResume() {
    super.onResume();
    IntentFilter iFilter= new IntentFilter("com.yourapp.example.P2PCONNECTED");
    //iFilter.addAction("someOtherAction"); //if you want to add other actions to filter
    this.registerReceiver(br, iFilter);
}

@Override
public void onPause() {
    this.unregisterReceiver(br);
    super.onPause();
}

private BroadcastReceiver br = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals("com.yourapp.example.P2PCONNECTED")){
             this.runOnUiThread(mUpdateP2PStatus);
        }
    }

};

private final Runnable mUpdateP2PStatus= new Runnable() {

    @Override
    public void run() {
      //TODO: Update your UI here                        
    }
};