关闭我的应用程序时,如何从设备断开连接?

问题描述:

我正在开发Android Studio中的一个项目,我应该使用Wifi-Direct连接两个设备。关闭我的应用程序时,如何从设备断开连接?

我设法通过Wifi-direct连接到另一台设备。我的问题是,当我在手机上手动关闭应用程序时,设备不会断开连接。我怎样才能做到这一点?我对Android Studio和Java颇为陌生。

这里是我WifiDirectActivity类:

public class WiFiDirectActivity extends Activity implements WifiP2pManager.ChannelListener, DeviceListFragment.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 WifiP2pManager.Channel channel; 
private BroadcastReceiver receiver = null; 

/** 
* @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_peers); 

    // 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); 

    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); 
    if (!amConnected) { 
     performSearch(); 
    } 
    } 



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

/** 
* Remove all peers and clear all fields. This is called on 
* BroadcastReceiver receiving a state change event. 
*/ 
public void resetData() { 
    DeviceListFragment fragmentList = (DeviceListFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_list); 
    DeviceDetailFragment fragmentDetails = (DeviceDetailFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_detail); 
    if (fragmentList != null) { 
     fragmentList.clearPeers(); 
    } 
    if (fragmentDetails != null) { 
     fragmentDetails.resetViews(); 
    } 
} 

@Override 
public void showDetails(WifiP2pDevice device) { 
    DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_detail); 
    fragment.showDetails(device); 

} 

@Override 
public void connect(WifiP2pConfig config) { 
    manager.connect(channel, config, new WifiP2pManager.ActionListener() { 

     @Override 
     public void onSuccess() { 
      // WiFiDirectBroadcastReceiver will notify us. Ignore for now. 
     } 

     @Override 
     public void onFailure(int reason) { 
      Toast.makeText(WiFiDirectActivity.this, "Connect failed. Retry.", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

@Override 
public void disconnect() { 
    final DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_detail); 
    fragment.resetViews(); 
    manager.removeGroup(channel, new WifiP2pManager.ActionListener() { 

     @Override 
     public void onFailure(int reasonCode) { 
      Log.d(TAG, "Disconnect failed. Reason :" + reasonCode); 


     } 

     @Override 
     public void onSuccess() { 
      fragment.getView().setVisibility(View.GONE); 

     } 

    }); 
} 

@Override 
public void onChannelDisconnected() { 
    // we will try once more 
    if (manager != null && !retryChannel) { 
     Toast.makeText(this, "Channel lost. Trying again", Toast.LENGTH_LONG).show(); 
     resetData(); 
     retryChannel = true; 
     manager.initialize(this, getMainLooper(), this); 
    } else { 
     Toast.makeText(this, 
       "Severe! Channel is probably lost premanently. Try Disable/Re-Enable P2P.", 
       Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
public void cancelDisconnect() { 

    /* 
    * A cancel abort request by user. Disconnect i.e. removeGroup if 
    * already connected. Else, request WifiP2pManager to abort the ongoing 
    * request 
    */ 
    if (manager != null) { 
     final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager() 
       .findFragmentById(R.id.frag_list); 
     if (fragment.getDevice() == null 
       || fragment.getDevice().status == WifiP2pDevice.CONNECTED) { 
      disconnect(); 
     } else if (fragment.getDevice().status == WifiP2pDevice.AVAILABLE 
       || fragment.getDevice().status == WifiP2pDevice.INVITED) { 

      manager.cancelConnect(channel, new WifiP2pManager.ActionListener() { 

       @Override 
       public void onSuccess() { 
        Toast.makeText(WiFiDirectActivity.this, "Aborting connection", 
          Toast.LENGTH_SHORT).show(); 
       } 

       @Override 
       public void onFailure(int reasonCode) { 
        Toast.makeText(WiFiDirectActivity.this, 
          "Connect abort request failed. Reason Code: " + reasonCode, 
          Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 
    } 

} 


private void performSearch() { 



    final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_list); 
    fragment.onInitiateDiscovery(); 
    manager.discoverPeers(channel, new WifiP2pManager.ActionListener() { 

     @Override 
     public void onSuccess() { 
      Toast.makeText(WiFiDirectActivity.this, "Discovery Initiated", 
        Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onFailure(int reasonCode) { 
      Toast.makeText(WiFiDirectActivity.this, "Discovery Failed : " + reasonCode, 
        Toast.LENGTH_SHORT).show(); 
     } 

    }); 
} 

} 

当您关闭该应用程序的执行将通过onPause()回调运行,那么你应该调用disconnect()方法。但是,花5分钟时间了解Android的生命周期原理:https://developer.android.com/guide/components/activities/activity-lifecycle.html。这是开发Android应用程序之前要学习的第一件事情之一。

+0

谢谢你的回答,这个工作时,应用程序被完全终止?原因当我去其他活动时,我不希望它断开连接,只有当我关闭应用程序时。并感谢你的链接,我知道我缺乏很多基础知识。当谈到这个项目时,我可能会耿耿于怀。 – Kspr

+0

然后,你可能需要onStop()回调,或者你应该创建一个持续控制器类,它具有连接和diconnect功能,所以你可以将类一个活动传递给另一个,并且可以在onStop()中调用controller.disconnect() 。 –