我有一个应用程序,需要找到用户位置,并在各个类中提取位置,所以我写了一个单独的类(非活动类),使用位置服务获取用户位置,它在Android M下工作正常,但需要运行Android M中的时间权限,我想检查我的Location类中的权限,我知道如何检查它们但我不能在我的Location类中使用onRequestPermissionsResult方法,因为我的Location类不会从任何活动扩展。
那么我应该做些什么呢?任何帮助/线索表示感谢提前感谢
由于public void onRequestPermissionsResult(int requestCode,String permissions [],int [] grantResults)是ActivityCompat.OnRequestPermissionsResultCallback接口的抽象方法。见the documentation
只需在所需的类中实现此接口即可。例如
class location implements ActivityCompat.OnRequestPermissionsResultCallback{ }
现在只需覆盖onRequestPermissionsResult()
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
// case Statements
}
}
欢迎您从非UI类调用checkSelfPermission()
,因为它只需要一些Context
。
但是,您必须在某些活动或片段上调用requestPermissions()
。您在同一活动或片段上覆盖onRequestPermissionsResult()
。这与调用startActivityForResult()
和实现onActivityResult()
没什么不同。
我们的想法是,在您执行涉及处理位置的非UI类的任何操作之前,请求您的许可。
1-创建一个透明的活动
<activity android:name=".activity.activity.CheckStoragePermissionsActivity" android:theme="@style/Theme.Transparent">
<style name="Theme.Transparent" parent="Theme.AppCompat">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
2-自定义您的活动
public class CheckStoragePermissionsActivity extends AppCompatActivity {
private String[] permissions;
private int pCode = 12321;
public static PermissionListener permissionListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkPermissions();
}
private void checkPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
boolean flag = false;
for (String s : permissions)
if (checkSelfPermission(s) != PackageManager.PERMISSION_GRANTED)
flag = true;
if (flag) {
requestPermissions(permissions, pCode);
} else {
permissionListener.permissionResult(true);
finish();
}
}else
finish();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == pCode) {
boolean flag = true;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
for (int i = 0, len = permissions.length; i < len; i++)
if (grantResults[i] != PackageManager.PERMISSION_GRANTED)
flag = false;
if (flag) {
if (permissionListener != null)
permissionListener.permissionResult(true);
} else if (permissionListener != null)
permissionListener.permissionResult(false);
finish();
}
}
}
3- permissionListener
是一个静态接口,可以在context.startActivity(...)之前直接设置,或者使用您的计划来查找权限结果。
public interface PermissionListener extends Serializable {
void permissionResult(boolean hasPermission);
}
4-终于打电话给context.startActivity(new Intent(context, CheckStoragePermissionsActivity.class));
CheckStoragePermissionsActivity
完成所有需要并在用户允许或拒绝操作后完成。
你无法覆盖它。此方法仅适用于活动和碎片。但是您可以在Location类中创建一个静态方法,并从您的activity / fragment的overReden onRequestPermissionResult方法中调用它。
我已经为Location定制了一个自定义的实现。您还可以使用名为Let的库来获取权限。
使用这个类:
public class permissionModule {
final public static int STORAGE_PERMISSION_CODE = 23;
private Activity activity;
public permissionModule(Activity activity) {
this.activity = activity;
}
public void startGetPermission(){
if (isStorageReadable()){
Toast.makeText(activity,"you Already have the permission to access storage",Toast.LENGTH_SHORT).show();
}else {
requestStoragePermission();
}
}
private void requestStoragePermission() {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
private boolean isStorageReadable() {
int result = ContextCompat.checkSelfPermission(activity,
Manifest.permission.READ_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED;
}
}
并在您的活动电话中:
new permissionModule(MainActivity.this).startGetPermission();
最后为了结果,在你的活动中覆盖它:
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == permissionModule.STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Snackbar permissionWarning = Snackbar.make(rootLayout, "tnk", Snackbar.LENGTH_INDEFINITE);
permissionWarning.setAction("ok", new View.OnClickListener() {
@Override
public void onClick(View view) {
//...
}
});
permissionWarning.show();
} else {
boolean showRationale = shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE);
if (showRationale) {
Snackbar permissionWarning = Snackbar.make(rootLayout, "Permission Required to access storage", Snackbar.LENGTH_INDEFINITE);
permissionWarning.setAction("ok", new View.OnClickListener() {
@Override
public void onClick(View view) {
//requestStoragePermission();
}
});
permissionWarning.show();
} else {
Snackbar snackbar = Snackbar.make(rootLayout, "you denied the permission for ever pleas fix it in setting", Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("setting", new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", MainActivity.this.getPackageName(), null);
intent.setData(uri);
MainActivity.this.startActivity(intent);
}
});
snackbar.show();
}
}
}
}