我正在关注Android 的 WiFi 直接指南,因为我正在构建一个使用 WiFi 直接的应用程序。我也有2节课;一种称为ClientReceive,一种称为HostSend。客户端发起与主机的连接,然后主机将数据推送到客户端。
当我开始开始我的 WiFi 直接设置时,我制作了一个广播接收器来监听状态变化:
package com.mgamerzproductions.wifidirecttest;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.util.Log;
/**
* A BroadcastReceiver that notifies of important wifi p2p events.
*/
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager manager;
private Channel channel;
private WiFiActivity activity;
/**
* @param manager WifiP2pManager system service
* @param channel Wifi p2p channel
* @param Host class (HostSend) for this constructor.
*/
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
HostSend activity) {
super();
this.manager = manager;
this.channel = channel;
this.activity = activity;
}
/**
* @param manager WifiP2pManager system service
* @param channel Wifi p2p channel
* @param activity activity associated with the receiver
*/
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
ClientReceive activity) {
super();
this.manager = manager;
this.channel = channel;
this.activity = activity;
}
/*
* (non-Javadoc)
* @see android.content.BroadcastReceiver#onReceive(android.content.Context,
* android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
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(Launch.APPTAG, "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(Launch.APPTAG, "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
HostSend connectionListener = activity;
manager.requestConnectionInfo(channel, connectionListener);
} 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));*/
}
}
}
但是,我有另一个类 ClientReceive,它也需要接收器,因为两个设备都需要直接设置 WiFi 并使用回调(我认为这就是所谓的回调?活动的回调?)来建立连接。这两个类都是 WiFi 活动的子类,它本质上是一个空类,它仅使用 super.methodname() 覆盖 Activity 中的一些方法,以便孩子可以访问它。IT 有一些与 WiFi 相关的额外方法。我创建它是为了避免强制转换,但它不起作用,因为子类有不同的方法,如果我使用 WiFiActivity 而不是它的子类,我不能使用子类方法。
有没有办法正确地做到这一点?我的代码正在进行中(这就是为什么它有点难看),但我没有想法。我有 2 个构造函数,一个来自每个设备上的每个活动(一个是主机设备,一个是客户端设备)。我想要做的是在两个类之间共享这个类,但我不知道如何正确地转换 HostSend connectionListener (HostSend) 活动,因为我不知道如何转换这样的项目。
谢谢。对不起,冗长的解释。