Android 6.0後ip撥號器許可權問題
一.關於6.0以後的許可權問題:
因為android 6.0以後添加了許可權問題,需要開發人員自己在程式碼中配置請求許可權,所以這裡舉一個簡單例子處理請求許可權的問題
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>二.開發IP撥號器
開發撥號器需要請求如上許可權,首先需要在清單檔案中配置該許可權,然後自己在程式碼中註冊該許可權。
三.具體程式碼如下
1.註冊許可權程式碼:
// 允許程式監視、修改有關撥出電話PROCESS_OUTGOING_CALLS
private static final int PROCESS_OUTGOING_CALLS = 1;
private static String[] OUTGOING_CALLS = {
Manifest.permission.PROCESS_OUTGOING_CALLS,
};
/**
* Checks if the app has permission to PROCESS_OUTGOING_CALLS
*
* If the app does not has permission then the user will be prompted to grant permissions
*
* @param activity
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have PROCESS_OUTGOING_CALLS permission檢查應用是否擁有監聽修改播出電話的許可權
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.PROCESS_OUTGOING_CALLS);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user如果我們沒有許可權就彈出提示請求使用者賦予許可權
ActivityCompat.requestPermissions(activity, OUTGOING_CALLS, PROCESS_OUTGOING_CALLS);
}
}
2.儲存ip電話號碼的程式碼
//我是在MainActivity的onCreate完成的,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt= (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//點選後彈出許可權請求提示,也可以在onCreate中請求,開啟activity就發出請求
verifyStoragePermissions(MainActivity.this);
EditText ed= (EditText) findViewById(R.id.editText);
String ipn=ed.getText().toString();
SharedPreferences sp=getSharedPreferences("ipnumber",MODE_PRIVATE);
sp.edit().putString("ipnumber",ipn).commit();
}
});
}
3.自定義廣播接受者重寫onReceive()方法
public void onReceive(Context context, Intent intent) {
String sip=getResultData();
SharedPreferences sharedPreferences=context.getSharedPreferences("ipnumber",Context.MODE_PRIVATE);
String ipnumber=sharedPreferences.getString("ipnumber","");
setResultData(ipnumber+sip);
abortBroadcast();
}
四.關於許可權處理的一些相關連結
//Android M (API23) 中對許可權的授權處理
http://www.tuicool.com/articles/V3eErq
//請求讀寫許可權
http://blog.csdn.net/ynwjcf/article/details/52153372
//許可權介紹
http://blog.csdn.net/zhangmiaoping23/article/details/50053715