我是Android的新手,目前正在设计一款具有WIFI P2P功能的应用。我正在使用该网站的指南: http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html
我的接收器类中有两个问题,我无法解决。 接收者代码:
package com.example.bilent.remote_connect;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager manager;
private WifiP2pManager.Channel channel;
private Connect activity; //YES - from MAIN (Connect)
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,
Connect activity) {
super();
this.manager = manager;
this.channel = channel;
this.activity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Determine if Wifi P2P mode is enabled or not, alert
// the Activity.
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);} }
else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
}
else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
}
else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing
DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
.findFragmentById(R.id.fragment_connect);
fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
}
}
}
出现错误:
activity.setIsWifiP2pEnabled(true);
其中突出显示了setIsWifiP2pEnabled。我拥有所有相关权限,我的主要类名是Connect。
另一个错误是
DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
.findFragmentById(R.id.fragment_connect);
我不确定DeviceListFragment是什么,使用什么ID,以及哪里有错误。
我意识到这些问题可能是非常基本的,我已经浏览了许多相关的P2P相关示例,但我不确定我需要什么程度的知识或者通过哪些指导来解决这个问题。 文档也没有多大帮助。如果你能给出一些指示,那就太棒了。
编辑:这是我的Main(Connect)类及相关布局:
package com.example.bilent.remote_connect;
import android.content.*;
import android.content.BroadcastReceiver;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.content.IntentFilter;
import android.view.MenuItem;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
public class Connect extends ActionBarActivity {
private final IntentFilter intentFilter = new IntentFilter();
Channel mChannel;
WiFiDirectBroadcastReceiver receiver;
WifiP2pManager mManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);
// Indicates a change in the Wi-Fi P2P status.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
// Indicates a change in the list of available peers.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
// Indicates the state of Wi-Fi P2P connectivity has changed.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
// Indicates this device's details have changed.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_connect, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是布局&片段:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.example.bilent.remote_connect.ConnectFragment"
tools:layout="@layout/fragment_connect" android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ConnectFragment">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
答案 0 :(得分:2)
好的第一个问题是您没有在setIsWifiP2pEnabled()
活动中定义Connect
方法。您需要在Connect
活动中添加以下方法的内容;
public class Connect extends ActionBarActivity {
// Other methods omitted for brevity
public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
// Do something in response to the boolean you are supplied
}
}
关于您的片段问题,我强烈建议您查看Creating a Fragment。这是创建片段的最小示例以及如何访问它们。
如果需要,可以在android源代码中找到用于WiFi Direct演示的代码。 DeviceListFragment的代码是here。