我正在编写将使用WiFi Aware来发布服务的应用程序,但是我无法通过验证设备是否支持该服务的初始步骤。 API(来自Google,这里为https://developer.android.com/guide/topics/connectivity/wifi-aware)指定使用以下代码行:
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_AWARE);
但是,由于我还没有实例化一个称为“ context”的对象,所以不能将其放入MainActivity类。我试图四处寻找答案,但没有找到任何有效的方法。任何形式的帮助将不胜感激。谢谢。
更新
似乎可以单独使用该方法,但是除非在某个方法(例如我的Publish()
方法)中调用它,否则它将导致该应用崩溃。我在下面附加了我的代码以供参考。谢谢。
我的代码:
package com.patrickutz.wifiawarepublish;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.aware.AttachCallback;
import android.net.wifi.aware.WifiAwareManager;
import android.net.wifi.aware.WifiAwareSession;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.net.wifi.aware.PublishConfig;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Constant values of WifiAwareManager
String state_change = WifiAwareManager.ACTION_WIFI_AWARE_STATE_CHANGED;
int data_init = WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR;
int data_resp = WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER;
public void publish(View view) {
// Check whether or not device supports WiFi Aware
boolean hasWiFiAware = getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_AWARE);
// Toast myToast = Toast.makeText(this, message, duration);
// Messages for whether or not device has WiFi Aware
Toast hasAware = Toast.makeText(this, "WiFi Aware Supported", Toast.LENGTH_SHORT);
Toast noAware = Toast.makeText(this, "WiFi Aware Unsupported", Toast.LENGTH_SHORT);
if (hasWiFiAware) {
hasAware.show();
} else {
noAware.show();
}
System.out.println(hasWiFiAware);
// Create WiFiAwareManager object
WifiAwareManager wifiAwareManager = (WifiAwareManager)getSystemService(Context.WIFI_AWARE_SERVICE);
// Check if WiFi Aware is available
boolean awareAvailable = wifiAwareManager.isAvailable();
// Messages for whether or not WiFi Aware is available
Toast isAvailable = Toast.makeText(this, "WiFi Aware Supported", Toast.LENGTH_SHORT);
Toast notAvailable = Toast.makeText(this, "WiFi Aware Unsupported", Toast.LENGTH_SHORT);
if (awareAvailable) {
isAvailable.show();
} else {
notAvailable.show();
}
// AttachCallback attachCallback = new AttachCallback();
// Handler handler = new Handler();
//
// wifiAwareManager.attach(attachCallback, handler);
// private static final String AWARE_FILE_SHARE_SERVICE_NAME = "Test Publish";
// PublishConfig config = new PublishConfig.Builder()
// .setServiceName(AWARE_FILE_SHARE_SERVICE_NAME)
// .build();
// Get the text views
TextView showStateChangeTextView = (TextView) findViewById(R.id.stateChangeTextView);
TextView showDataPathRoleInitTextView = (TextView) findViewById(R.id.dataPathRoleInitTextView);
TextView showDataPathRoleRespTextView = (TextView) findViewById(R.id.dataPathRoleRespTextView);
// Display the new values of current state in the text view.
showStateChangeTextView.setText("State: " + state_change);
showDataPathRoleInitTextView.setText("Data Initiator: " + Integer.toString(data_init));
showDataPathRoleRespTextView.setText("Data Responder: " + Integer.toString(data_resp));
}
}
答案 0 :(得分:1)
您不必初始化Context
对象。层次结构中的活动是Context的子类。
您可以在getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_AWARE);
类中简单地调用Activity
。不必显式初始化Context
答案 1 :(得分:0)
看到此Google教程,我正在寻找一种简单的方法来向我们展示调用此方法必须使用context
。然后,在activity
内,实例化此变量是不必要的,因为她已经存在,因此,只需调用getPackageManager()...