Android 6.0以後一般權限不只要在AndroidManifest.xml聲明,Activity class還要實作onRequestPermissionsResult(....)給使用者授權
,每個App都有自己管理權限的頁面如影片,給使用者授權,一開始要判斷該項權限是否授權,每授權就詢問是否要求授權,否則應用程式會崩潰
第一個requestCode引數,給各任何數字check既可(如CAPTURE_AUDIO=9999),第2引數不曉得作用,反正沒用到,第3個引數grantResults,是指授權項,若有2個權限要授權會是grantResults[0],grantResults[1],下面用grantResults.length取得項目數用for去逐一判斷,是否全授權,是就運行程式,否就不能運行程式,再去要求授權.
check_permission.newInstance(this)特別用一個Class去處裡授權項
ContextCompat.checkSelfPermission(mActivity, sti)==PackageManager.PERMISSION_DENIED來判斷該項是否有授權,若沒授權加入一個List,sti是指android.Manifest.permission.XXXXX,
若有沒授權ActivityCompat.requestPermissions(mActivity, get_str, CAPTURE_AUDIO)要求授權,get_str是指沒授權的String List
A.
設定檢查一般權限項目在str_permission設定既可如下,若是只要CAMERA,只要設str_permission={"CAMERA"}要跟AndroidManifest.xml也要加上
private final String[] str_permission={
"CAMERA",
"RECORD_AUDIO",
"WRITE_EXTERNAL_STORAGE",
"READ_PHONE_STATE"
};
B.
若有其他權限要加,根據需要加上需要的一般permission
若知道所有一般permission乾脆全部加上也可,省得麻煩
public String get_permission_string(String st){
String s=null;
switch(st){
case "RECORD_AUDIO":
s=android.Manifest.permission.RECORD_AUDIO;
break;
case "WRITE_EXTERNAL_STORAGE":
s=android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
break;
case "READ_PHONE_STATE":
s= Manifest.permission.READ_PHONE_STATE;
break;
case "CAMERA":
s= Manifest.permission.CAMERA;
break;
//在這加入一般permission case
}
return s;
}
這個check_permission class可當作API來使用,任何App都可使用
MainActivity只需呼叫check_permission.newInstance(this);就會檢查str_permission設定的項目,
AndroidManifest.xml
...........................................................................
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
............................................................................
MainActivity
public class MainActivity extends AppCompatActivity{
..........................................................
private int CAPTURE_AUDIO=9999;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(Build.VERSION.SDK_INT>=23) {
//檢查是否有授權沒授權b=true
check_permission.newInstance(this);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,String[] permission,int[] grantResults){
super.onRequestPermissionsResult(requestCode,permission,grantResults);
if(requestCode==CAPTURE_AUDIO){
//先預設都通過授權
boolean b=true;
for(int i=0;i<grantResults.length;i++){
//逐一檢查,若一項沒通過,就不通過,
if(grantResults[i]!= PackageManager.PERMISSION_GRANTED){b=false;}
}
if(b){
Toast.makeText(this,"OK", Toast.LENGTH_LONG).show();
START_VISUALIZER=true;
}else{
START_VISUALIZER=false;
Toast.makeText(this,"Cancle", Toast.LENGTH_LONG).show();
}
}
}
......................................................................
}
check_permission類,下面使用靜態static, newInstance來new class,如check_permission.newInstance(this)
這樣就會實例化一個check_permission,注意方法中會實例化check_permission類,並返回
public static check_permission newInstance(MainActivity obj){
mActivity=obj;
check_permission mcheck_permission=new check_permission();
return mcheck_permission;
}
check_permission
package com.example.yplin.test_audio;
/**
* 檢查授權
* 1.
* 需要可添加在get_permission_string()權限中
* 2.
* String[] str_permission設定檢查一般權限項目
* 3.
* Activity呼叫 check_permission.newInstance(myActivity)開始check
*/
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Created by yplin on 2017/4/26.
*/
public class check_permission {
private static Activity mActivity=null;
//設定檢查一般權限項目,若是只要CAMERA,只要設str_permission={"CAMERA"}
private final String[] str_permission={
"CAMERA",
"RECORD_AUDIO",
"WRITE_EXTERNAL_STORAGE",
"READ_PHONE_STATE"
};
private int CAPTURE_AUDIO=9999;
public check_permission(){
run_check(str_permission);
}
public static check_permission newInstance(MainActivity obj){
mActivity=obj;
check_permission mcheck_permission=new check_permission();
return mcheck_permission;
}
public void run_check(String[] st){
//不能用null
List<String> list=new ArrayList();
int k=0;
for(int i=0;i<st.length;i++)
{
//一個個判斷
String sti=get_permission_string(st[i]);
boolean b= ContextCompat.checkSelfPermission(mActivity, sti)==PackageManager.PERMISSION_DENIED;
//把沒取得的Permission加入list
if(b){
Log.d("TAG",sti);
list.add(k,sti);
k++;
}
}
//String[]一定要給正確length,根據list.size()給正確length
String[] get_str=new String[list.size()];
//重新產生需要求permission String[] get_str
for(int j=0;j<list.size();j++){
Log.d("TAG",list.get(j));
get_str[j]=list.get(j);
}
//若沒有授權項目,要求授權,0就已全授權忽略
if(list.size()>0){
ActivityCompat.requestPermissions(mActivity, get_str, CAPTURE_AUDIO);
}
}
//根據需要加上需要的一般permission
public String get_permission_string(String st){
String s=null;
switch(st){
case "RECORD_AUDIO":
s=android.Manifest.permission.RECORD_AUDIO;
break;
case "WRITE_EXTERNAL_STORAGE":
s=android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
break;
case "READ_PHONE_STATE":
s= Manifest.permission.READ_PHONE_STATE;
break;
case "CAMERA":
s= Manifest.permission.CAMERA;
break;
//在這加入一般permission case
}
return s;
}
}
沒有留言:
張貼留言