I need to init a service,it needs permission of READ_PHONE_STATE
.I declare it and apply dynamically.The callback function has been rewritten.However, the virtual machine don't pop a dialog box of requesting permission.
The code reads as follows.
Androidmanifest.xml:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Apply dynamically:
requestPermissions(new String[]{Manifest.permission.CALL_PHONE},1);
Please help me. Be deeply grateful.
Use below functions for checking runtime permissions in onCreate() method:
checkPermission();
requestPermission();
and Functions are:
private boolean checkPermission() {
return ( ContextCompat.checkSelfPermission(getApplicationContext(), READ_PHONE_STATE ) == PackageManager.PERMISSION_GRANTED);
}
private void requestPermission() {
ActivityCompat.requestPermissions(LoginActivity.this, new String[]{READ_PHONE_STATE}, REQUEST_SMS);
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_SMS:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
}else {
Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and sms", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(READ_PHONE_STATE)) {
showMessageOKCancel("You need to allow access to both the permissions",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{READ_PHONE_STATE},
REQUEST_SMS);
}
}
});
return;
}
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
import static android.Manifest.permission.SEND_SMS; public class CoreActivity extends AppCompatActivity implements OnAccountListener { boolean init_state; AccountApi accountApi; UiHandler uiHandle=new UiHandler(this); @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Message msg=new Message(); msg.what=MsgCode.STATE_LOGIN_START; uiHandle.sendMessage(msg); ActionBar actionBar=getSupportActionBar(); actionBar.hide(); requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},1); // PackageManager pm = getPackageManager(); // boolean permission = (PackageManager.PERMISSION_GRANTED == // pm.checkPermission("android.permission.ACCESS_NETWORK_STATE", "com.example.asus.tl")); // if (permission) { // Log.d("permission","yes"); // }else { // Log.d("permission","no"); // } checkPermission(); requestPermission(); init_state=API.init(this); Log.d("API.init",init_state+""); Log.d("service",isServiceRunning()+""); Thread getAccountThread=new Thread(delayInitApi); getAccountThread.start(); } private boolean isServiceRunning() { ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if ("com.algebra.sdk.Controller".equals(service.service.getClassName())) { return true; } } return false; } @RequiresApi(api = Build.VERSION_CODES.M) private boolean checkPermission() { return ( this.checkSelfPermission(getApplicationContext(), SEND_SMS ) == PackageManager.PERMISSION_GRANTED); } private int checkSelfPermission(Context applicationContext, String sendSms) { return 1; } private void requestPermission() { ActivityCompat.requestPermissions(CoreActivity.this, new String[]{SEND_SMS}, 1); } public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ }else { Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and sms", Toast.LENGTH_SHORT).show(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (shouldShowRequestPermissionRationale(SEND_SMS)) { // showMessageOKCancel("You need to allow access to both the permissions", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{SEND_SMS}, 1); } } }; return; } } } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } }
You should use
<uses-permission-sdk-23 android:name="android.permission.READ_PHONE_STATE" />
instead of
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
in your Manifest
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.