解析Android Studio';WIFI的P2P实现

解析Android Studio';WIFI的P2P实现,android,android-fragments,android-wifi,wifi-direct,Android,Android Fragments,Android Wifi,Wifi Direct,我是android新手,目前正在设计一款具有WIFI P2P功能的应用程序。我正在使用该网站的指南: 我的receiver类中有两个问题我无法解决。 接收器代码: package com.example.bilent.remote_connect; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net

我是android新手,目前正在设计一款具有WIFI P2P功能的应用程序。我正在使用该网站的指南:

我的receiver类中有两个问题我无法解决。 接收器代码:

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);
其中SETISWIP2PENABLED突出显示。我拥有所有相关权限,我的主要类名是Connect

另一个错误是on

DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
                .findFragmentById(R.id.fragment_connect);
我不确定什么是DeviceListFragment,使用什么ID,以及两者都存在错误

我意识到这些问题可能是非常基本的,我已经看过许多相关的P2P相关的例子,但我不确定我需要多少知识,或者需要通过哪些指南来解决这个问题。 这些文档也没有多大帮助。如果你能给我一些建议,那就太好了

编辑:这是我的主要(连接)类和相关布局:

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" />


好的第一个问题是您没有在
连接
活动上定义
setIsWifiP2pEnabled()
方法。您需要在
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
    }

} 
关于你的碎片问题,我强烈建议你看看。这是创建片段以及如何访问片段的最低示例


如果需要,WiFi Direct演示所用的代码可以在android源代码中找到。DeviceListFragment的代码是。

您看到的错误是什么?它们是否与运行时、编译器或IDE相关?“无法解析方法/符号”错误。在编译之前,这些区域以红色突出显示。那么,我想是IDE吗?啊,好吧,第一个错误表明在
连接
活动中没有名为
setIsWifiP2pEnabled()
的公共方法。您是否介意分享您的
Connect
活动的来源以及该活动的布局。非常感谢!但是,DeviceListFragment仍然突出显示为错误?DeviceListFragment是您自己的类吗?我假设您已经从示例代码中获取了这一点。在这种情况下,您应该看看如何实例化创建片段。是的,我只是从示例代码中获取,没有提供额外的类。我想我必须创建一个新类,然后在我的主类中实例化它,对吗?另外,你能给我指出一些可能有助于android智能的资源方向吗?网上有这么多,但我不确定其中有多少是有用的或相关的。告诉你合适的Android培训指南,这可能是我能在短时间内想到的最简单的最小噪音示例。有关链接,请参见我的答案。