Android某些功能的使用需要获得权限,对于一些常用权限,可以再Androidmanifest.xml中添加。添加方式如下:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
对于某些权限,在其中添加之后也不能获取,需要主动询问用户。询问方法如下:
import android.Manifest; import android.app.Activity; import android.content.pm.PackageManager; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.os.Bundle; import android.os.Handler; import android.content.Intent; import java.util.ArrayList; public class StartActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback{ private static final int WAITTIME = 500; private ArrayList<String> permission=new ArrayList<String>(); private ArrayList<String> askPermissionList = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start); permission.add(Manifest.permission.READ_EXTERNAL_STORAGE); permission.add(Manifest.permission.READ_PHONE_STATE); checkPermission(permission,askPermissionList); if(askPermissionList.size() >0) { requestPermissions(askPermissionList,1); }else{ new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(StartActivity.this, MainActivity.class); startActivity(intent); finish(); } }, WAITTIME); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(verifyPermisson(grantResults)){ new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(StartActivity.this, MainActivity.class); startActivity(intent); finish(); } }, WAITTIME); }else { finish(); } } /*** * 请求权限的方法,requestPermissions() 传入一个Activity,一个需要的权限的数组, * 和一个整形的request code,与用户完成交互后,系统会回调该方法并返回对用的request code * @param needAskPermisson * @param requestCode */ private void requestPermissions(ArrayList<String> needAskPermisson,int requestCode) { String[] needpermisson = new String[needAskPermisson.size()]; needpermisson = needAskPermisson.toArray(needpermisson); ActivityCompat.requestPermissions(this,needpermisson,1);//弹出对话框 } /**** * 判断应用权限的有无,如果没有这项权限,则将权限加入到askPermissionsList * @param permissions * @param askpermissionsList */ private void checkPermission(ArrayList<String> permissions, ArrayList<String> askpermissionsList) { for(int i=0;i<permissions.size();i++) { //检查权限的方法,第一个参数是context,第二个参数是权限名,返回值为PERMISSION_GRANTED(有权限)和PERMISSION_DENIED(无权限) if(ContextCompat.checkSelfPermission(StartActivity.this,permissions.get(i))!= PackageManager.PERMISSION_GRANTED){ askpermissionsList.add(permissions.get(i)); } } } private boolean verifyPermisson(int[] grantResults) { if(grantResults.length<1) { return false; } for(int result:grantResults) { if(result!=PackageManager.PERMISSION_GRANTED){ return false; } } return true; } }
一般情况下,在刚进入到应用时询问用户权限,在此处,StartActivity是程序的入口,在Androidmanifest.xml中进行定义,如下:
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".StartActivity" android:label="@string/title_activity_main" android:screenOrientation="portrait" android:theme="@style/AppTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>