我的应用程序想要在开始时申请Wifi权限。到目前为止,我已经设法通过设置给予许可。但我不想再这样做了。到目前为止,我有看过这个以前的Question。我能理解一点。但它还不够。我创建了一个 .class 来管理Wifi活动,比如打开/关闭,检查wifi状态。如果Wifi权限被授予,它的效果很好。但如果没有,它确实有效。
这是我创建的 WifiController.class 。
import android.content.Context;
import android.net.wifi.WifiManager;
import javafxports.android.FXActivity;
public class WifiController implements WifiInterface
{
WifiManager wifiManager=null;
WifiController()
{
wifiManager = (WifiManager)FXActivity.getInstance().getSystemService(Context.WIFI_SERVICE);
}
WifiManager getWifimanager()
{
return wifiManager;
}
public boolean isTurnedOn()
{
return wifiManager.isWifiEnabled();
}
public boolean turnOn()
{
wifiManager.setWifiEnabled(true);
return true;
}
public boolean turnOff()
{
wifiManager.setWifiEnabled(false);
return true;
}
}
所以,请启发我。在此先感谢。
答案 0 :(得分:2)
Android开发者网站有一个关于运行时权限如何在这里工作的一个很好的演练示例:
https://developer.android.com/training/permissions/requesting.html
也不要忘记将权限添加到您的清单中。
答案 1 :(得分:2)
您在运行时要求获得许可:
Class com.paypal.api.payments.PayoutBatch not subtype of
[map type; class java.util.Map, [simple type, class java.lang.Object] ->
[simple type, class java.lang.Object]]
而不是做你想做的事(如果用户获得了许可):
if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_PERMISSION);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
您可以在这里阅读更多内容: https://developer.android.com/training/permissions/requesting.html
答案 2 :(得分:1)
基于此answer,我创建了以下类,以便在Android版> = Marshmallow时对应用启动请求权限:
//FirebaseOptions options = new FirebaseOptions.Builder().setApplicationId("geofire").setDatabaseUrl(GEO_FIRE_DB).build();
//FirebaseApp app = FirebaseApp.initializeApp(this, options);
// setup GeoFire
this.geoFire = new GeoFire(FirebaseDatabase.getInstance().getReferenceFromUrl(GEO_FIRE_REF));
在AndroidManifest.xml中,您必须添加相应的活动:
public class AndroidPlatform {
private static final String KEY_PERMISSIONS = "permissions";
private static final String KEY_REQUEST_CODE = "requestCode";
private static final int REQUEST_CODE_ASK_PERMISSIONS = 246;
private static final int REQUEST_CODE = 123;
private final FXActivity activity;
public AndroidPlatform() {
activity = FXActivity.getInstance();
}
public void checkPermissions() {
if (Build.VERSION.SDK_INT >= MARSHMALLOW) {
List<String> requiredPermissions = new ArrayList<>();
addPermission(activity, requiredPermissions , Manifest.permission.ACCESS_COARSE_LOCATION);
// additional required permissions
if (!requiredPermissions.isEmpty()) {
Intent permIntent = new Intent(activity, PermissionRequestActivity.class);
permIntent.putExtra(KEY_PERMISSIONS, requiredPermissions.toArray(new String[requiredPermissions .size()]));
permIntent.putExtra(KEY_REQUEST_CODE, REQUEST_CODE);
activity.startActivityForResult(permIntent, REQUEST_CODE_ASK_PERMISSIONS);
}
}
}
private void addPermission(Activity activity, List<String> permissionsList, String permission) {
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
}
}
public static class PermissionRequestActivity extends Activity {
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
FXActivity.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
finish();
}
@Override
protected void onStart() {
super.onStart();
String[] permissions = this.getIntent().getStringArrayExtra(KEY_PERMISSIONS);
int requestCode = this.getIntent().getIntExtra(KEY_REQUEST_CODE, 0);
ActivityCompat.requestPermissions(this, permissions, requestCode);
}
}
}
此外,您还必须将android-support-library添加到build.gradle:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<activity android:name="yourpackage.AndroidPlatform$PermissionRequestActivity" />
有关wifiManager /权限的更多信息,请参阅link