最近因爲 安卓6.0權限 還有 7.0文件共享 事情整的焦頭爛額,暴露出一些問題,剛剛開始時並沒有出現什麼權限和共享文件的bug,後來陸陸續續多了一些高版本用戶,導致崩潰率上升了不少,特此需要解決權限問題,之前一直用的是EasyPermission這個框架,剛剛開始也沒怎麼測試直接丟到項目中跑了,後來深入發現這種方法並非100%不出錯,找了幾個關於權限的框架,比如一個日本朋友寫的PermissionDispatch,國內的沒敢用,因爲star的數太少了,來回的測試,認爲提示不好,國內小米和華爲的工程師喜歡搞事,找不出一套兼顧全部機型的方法,最後沒轍了,直接上官網,老老實實看英文解釋說明,寫了一個demo放到優測去測試,結果效果非常好,雖然代碼量多了點,那些框架用annotation註解去搞事,對代碼各種封裝,結果還是沒能解決問題,實際上消耗的時間是非常大的,如果一開始做個老實人,應該啥事都沒有,之前的文件存儲也是一個道理,直接看英文文檔,ok,終於弄懂了,對於國內博客各種抄襲表示深深地** 有時候真的懷疑信息的真實性,是不是生活在黑客帝國或者楚門的世界那樣的地方。
最近kotlin很熱,各種廣告,在開發中也覺得安卓的空指針異常處理是非常難受的,明明不會爲空的東西在某種情況下就空了,這種bug導致崩潰率又上升了幾個點,這一個月的崩潰率2.0左右,離1.5差很多,在說發版本的問題,因爲我和測試的疏忽,金額寫反導致了倆次發佈,實在是汗顏,誰知道這一個月經歷了什麼。
用時一個半月,也算又熟悉了並習慣了開發的生活,成年後的無限可能性讓我着迷。
最後爲了字數貼代碼 官網纔是可靠的乾爹
package com.example.robin.permissiontest;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private final int PERMISSIONS_REQUEST_CON_ST = 1231;
String[] perms = {Manifest.permission.READ_CONTACTS, Manifest.permission.READ_EXTERNAL_STORAGE};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
camera();
}
});
}
private void camera() {
if (hasNoPermission(perms)) {
if (shouldShowExplanation(perms)) {
// explain to the user *asynchronously* -- don't block
Log.d("jack", "-----shouldShowExplanation---");
showExplain();
} else {
Log.d("jack", "-----request permission---");
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MainActivity.this, perms, PERMISSIONS_REQUEST_CON_ST);
}
} else {
Toast.makeText(MainActivity.this, "---u got permission----", Toast.LENGTH_SHORT).show();
Log.d("jack", "-----u got permission do anything here---");
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
Log.d("jack", "--------onRequestPermissionsResult---------" + requestCode);
switch (requestCode) {
case PERMISSIONS_REQUEST_CON_ST:
// If request is cancelled, the result arrays are empty.
if (allPermissionGranted(grantResults)) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Log.d("jack", "-------onRequestPermissionsResult-----finally u got all permission----");
camera();
} else {
if (!shouldShowExplanation(permissions)) { // 完全被拒絕
goSetting();
} else { //
Toast.makeText(MainActivity.this, "---permission denied----", Toast.LENGTH_SHORT).show();
}
}
return;
default:
break;
}
}
private boolean shouldShowExplanation(String[] perms) {
for (int i = 0; i < perms.length; i++) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, perms[i])) {
return true;
}
}
Log.d("jack", "-----shouldShowExplanation return false---");
return false;
}
private boolean hasNoPermission(String[] perms) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Log.d("jack", "-----hasPermission lower api 23---");
return false;
}
for (int i = 0; i < perms.length; i++) {
if (ContextCompat.checkSelfPermission(MainActivity.this, perms[i]) != PackageManager.PERMISSION_GRANTED) {
return true;
}
}
return false;
}
private void showExplain() {
new AlertDialog.Builder(MainActivity.this)
.setTitle("權限說明")
.setMessage("爲了功能正常使用,需要以下權限")
.setNegativeButton("拒絕", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("授權", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this, perms, PERMISSIONS_REQUEST_CON_ST);
dialog.dismiss();
}
}).create().show();
}
private boolean allPermissionGranted(int[] grantResults){
if(grantResults==null || grantResults.length == 0) return false;
for(int i=0; i<grantResults.length; i++){
if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
return false;
}
}
return true;
}
private void goSetting() {
new AlertDialog.Builder(MainActivity.this)
.setTitle("提示")
.setMessage("沒有權限功能無法使用,進入系統設置授權?")
.setNegativeButton("拒絕", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("設置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//前往應用詳情界面
try {
Uri packUri = Uri.parse("package:" + getPackageName());
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packUri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
dialog.dismiss();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "請手動進入系統設置進行授權", Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
}).create().show();
}
}
I'm fish, I'm on.