Android 6.0.1 couldn't enable wifi hotspot programmatically

前端 未结 5 1763
孤城傲影
孤城傲影 2021-02-09 02:20

When I tried to enable wifi tethering from the following code it throws the exception

java.lang.reflect.InvocationTargetException at java.lang.reflect.Method

5条回答
  •  隐瞒了意图╮
    2021-02-09 02:45

    Permission is not your problem. You need something like this code :

    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); //You need a callback for activity result so that check if user enabled this feature, go for starting hotspot (google for it)
    } else {
        // Do your stuff about starting hotspot (in network thread)
    }
    

    }

提交回复
热议问题