我看到了代码Here。 public void displayPeers(最终WifiP2pDeviceList对等体)如何在这里工作。 当我在设备中运行时,它会显示在列表视图中的设备中,但它如何获取没有初始化方法的设备列表,即。 displaypeers()没有调用?
之后我更改了项目的代码,Displaypeers方法无法正常工作/调用。但当我导入它完整的项目时,它的工作原理。它是如何工作的?请帮我 。 如果我犯了错误,有谁能告诉我如何称呼这种方法? oncreate函数本身中的Displaypeers(最终WifiP2pDeviseList对等体)。 谢谢 。抱歉我的英语不好。
public void searchForPeers(View view) {
//Discover peers, no call back method given
wifiManager.discoverPeers(wifichannel, null);
}
.... ....
public void displayPeers(final WifiP2pDeviceList peers)
{
//Dialog to show errors/status
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("WiFi Direct File Transfer");
//Get list view
ListView peerView = (ListView) findViewById(R.id.peers_listview);
//Make array list
ArrayList<String> peersStringArrayList = new ArrayList<String>();
//Fill array list with strings of peer names
for(WifiP2pDevice wd : peers.getDeviceList())
{
peersStringArrayList.add(wd.deviceName);
}
//Set list view as clickable
peerView.setClickable(true);
//Make adapter to connect peer data to list view
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, peersStringArrayList.toArray());
//Show peer data in listview
peerView.setAdapter(arrayAdapter);
peerView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3) {
//Get string from textview
TextView tv = (TextView) view;
WifiP2pDevice device = null;
//Search all known peers for matching name
for(WifiP2pDevice wd : peers.getDeviceList())
{
if(wd.deviceName.equals(tv.getText()))
device = wd;
}
if(device != null)
{
//Connect to selected peer
connectToPeer(device);
}
else
{
dialog.setMessage("Failed");
dialog.show();
}
}
// TODO Auto-generated method stub
});
}
编辑:Oncreate方法
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
// getActionBar().setDisplayHomeAsUpEnabled(true);
wifiManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
wifichannel = wifiManager.initialize(this, getMainLooper(), null);
wifiClientReceiver = new WiFiClientBroadcastReceiver(wifiManager, wifichannel, this);
wifiClientReceiverIntentFilter = new IntentFilter();
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
connectedAndReadyToSendFile = false;
filePathProvided = false;
fileToSend = null;
transferActive = false;
clientServiceIntent = null;
targetDevice = null;
wifiInfo = null;
registerReceiver(wifiClientReceiver, wifiClientReceiverIntentFilter);
setClientFileTransferStatus("Client is currently idle");
//setTargetFileStatus("testing");
}
答案 0 :(得分:0)
你应该看一下整个项目。除了ClientActivity之外,还有更多的类。它们都相互作用,形成一个有效的应用程序。
从WiFiClientBroadcastReceiver调用displayPeers()。我建议你仔细查看所有文件: