gpt4 book ai didi

android - 如何在marshmallow 及以上android 版本中以编程方式启用移动热点?

转载 作者:太空狗 更新时间:2023-10-29 13:07:38 25 4
gpt4 key购买 nike

我尝试使用以下代码在 Android Marshmallow 中创建 wifi 网络共享热点。

public class WifiAccessManager {

private static final String SSID = "mHotspot";
public static boolean setWifiApState(Context context, boolean enabled) {
//config = Preconditions.checkNotNull(config);
try {
WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (enabled) {
mWifiManager.setWifiEnabled(false);
}
WifiConfiguration conf = getWifiApConfiguration();
mWifiManager.addNetwork(conf);

return (Boolean) mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class).invoke(mWifiManager, conf, enabled);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public static WifiConfiguration getWifiApConfiguration() {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = SSID;
conf.allowedKeyManagement.set(Integer.parseInt("12345678"));
return conf;
}

而且我已经在 list 文件中授予访问 wifi 的所有权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

但在marshmallow及以上版本不激活

每当我启用热点时,我都会收到此错误

08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err: java.lang.reflect.InvocationTargetException
08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err: at java.lang.reflect.Method.invoke(Native Method)
08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err: at com.example.deneebo.ffconnect.WifiAccessManager.setWifiApState(WifiAccessManager.java:28)
08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err: at com.example.deneebo.ffconnect.DeviceFoundActivity.onCreate(DeviceFoundActivity.java:72)
08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.app.Activity.performCreate(Activity.java:6689)
08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2709)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2825)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.app.ActivityThread.-wrap12(ActivityThread.java)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.os.Handler.dispatchMessage(Handler.java:110)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.os.Looper.loop(Looper.java:203)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6339)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at java.lang.reflect.Method.invoke(Native Method)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: Caused by: java.lang.SecurityException: com.example.deneebo.ffconnect was not granted this permission: android.permission.WRITE_SETTINGS.
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.os.Parcel.readException(Parcel.java:1684)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.os.Parcel.readException(Parcel.java:1637)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.net.wifi.IWifiManager$Stub$Proxy.setWifiApEnabled(IWifiManager.java:1888)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: at android.net.wifi.WifiManager.setWifiApEnabled(WifiManager.java:1748)
08-18 13:56:32.306 27844-27844/com.example.deneebo.ffconnect W/System.err: ... 15 more

请帮助解决这个问题。

谢谢

最佳答案

最后,我从谷歌的开发者网站上得到了解决方案

https://developer.android.com/reference/android/Manifest.permission.html#WRITE_SETTINGS

我在 list 文件中添加了以下权限

 <uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

并在我的代码中添加以下几行

>  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
> if (!Settings.System.canWrite(getApplicationContext())) {
> Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" +
> getPackageName()));
> startActivityForResult(intent, 200);
>
> }
> }

非常感谢您的帮助 Mr.lorenzo-s

关于android - 如何在marshmallow 及以上android 版本中以编程方式启用移动热点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45750987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com