[英]Permission granted in app by user Android M
I want to in my app. 我想在我的应用中。 if permission
already granted not show dialog
in android this is my code 如果已经授予permission
不会在Android中显示dialog
,这是我的代码
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
requestContactsPermissions();
break;
case R.id.button1:
// requestCameraPermissions();
Toast.makeText(mcontext, "Hello", Toast.LENGTH_LONG).show();
break;
}
}
private void requestContactsPermissions() {
boolean requestPermission = PersimissionTest.requestContactsPermissions(this);
if (requestPermission == true) {
ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACTS, REQUEST_ID);
} else {
ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACTS, REQUEST_ID);
}
}
@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { @Override public void onRequestPermissionsResult(int requestCode,StringPermission [],int [] grantResults){
if (requestCode == REQUEST_ID) {
if (PersimissionTest.verifyPermissions(grantResults)) {
Toast.makeText(mcontext, "Contacts permissions granted.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(mcontext, "Contacts permissions were not granted.", Toast.LENGTH_LONG).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
The following code checks if the app has permission to read the user's contacts, and requests the permission if necessary: 以下代码检查应用程序是否具有读取用户联系人的权限,并在必要时请求该权限:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else
{
// Permission already granted ... This is where you can continue your further business logic...
}
And the following is the callback method: 以下是回调方法:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
For detailed explanation visit here (Android Official Page) or here (Tutsplus Tutorial): 有关详细说明,请访问此处 (Android官方页面)或此处 (Tutsplus教程):
Request Permission Like This , if permission already granted than insert contact else it will ask for permission 像这样请求许可,如果已授予许可而不是插入联系人,则它将请求许可
private void insertDummyContactWrapper(String mStringPhoneContactName, String mStringPhoneContactNumber) {
boolean hasWriteContactsPermission = ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_CONTACTS);
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SwipableHomeActivity.this, new String[]{android.Manifest.permission.WRITE_CONTACTS}, CONSTANTS.MY_PERMISSIONS_REQUEST_WRITE_CONTACTS);
return;
}
//Insert Contact
}
Get Permission Result Like This 获取这样的权限结果
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case CONSTANTS.MY_PERMISSIONS_REQUEST_WRITE_CONTACTS:
if (grantResults != null && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
ReadPhoneContacts(getApplicationContext(), mStringPhoneContactName, mStringPhoneContactNumber);
} else {
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
Check this i have implemented in your question 检查一下我已经在您的问题中实现了
public static boolean requestContactsPermissions(Activity activity) {
boolean requestPermission;
if (ContextCompat.checkSelfPermission(activity, android.Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermission = true;
}else{
requestPermission = false;
}
return requestPermission;
}
public static boolean requestCameraPermissions(Activity activity) {
boolean requestPermission;
if (ContextCompat.checkSelfPermission(activity, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
requestPermission = true;
} else {
requestPermission = false;
}
return requestPermission;
}
Change Your Class With This 以此来改变你的班级
public class MainActivity extends AppCompatActivity implements
OnRequestPermissionsResultCallback, OnClickListener{
public static final String TAG = "PermissionActivity";
private View mLayout;
Button mButtonCheckPermission = null;
private static final int REQUEST_CONTACTS = 1;
private static String[] PERMISSIONS_CONTACT = {Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mButtonCheckPermission = (Button) findViewById(R.id.button_check_permissions);
mButtonCheckPermission.setOnClickListener(this);
mLayout = findViewById(R.id.sample_main_layout);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_check_permissions:
requestContactsPermissions();
break;
default:
break;
}
}
private void requestContactsPermissions() {
boolean requestPermission = PermissionUtil.requestContactsPermissions(this);
if (requestPermission == true){
Log.i(TAG,
"Displaying contacts permission rationale to provide additional context.");
// Display a SnackBar with an explanation and a button to trigger the request.
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
Snackbar.make(mLayout, R.string.permission_contacts_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_CONTACT,
REQUEST_CONTACTS);
}
})
.show();
}else{
Snackbar.make(mLayout, R.string.permision_available_contacts,
Snackbar.LENGTH_LONG)
.show();
}
}
else {
ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (requestCode == REQUEST_CONTACTS) {
Log.i(TAG, "Received response for contact permissions request.");
// We have requested multiple permissions for contacts, so all of them need to be
// checked.
if (PermissionUtil.verifyPermissions(grantResults)) {
// All required permissions have been granted, display contacts fragment.
Snackbar.make(mLayout, R.string.permision_available_contacts,
Snackbar.LENGTH_LONG)
.show();
} else {
Log.i(TAG, "Contacts permissions were NOT granted.");
Snackbar.make(mLayout, R.string.permissions_not_granted,
Snackbar.LENGTH_LONG)
.show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}}
I have done 我已经做好了
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
if (!checkPermission()) {
requestContactsPermissions();
}else{
Toast.makeText(mcontext, "Permission already granted.", Toast.LENGTH_LONG).show();
}
break;
case R.id.button1:
// requestCameraPermissions();
Toast.makeText(mcontext, "Hello", Toast.LENGTH_LONG).show();
break;
}
}
private boolean checkPermission(){
int result = ContextCompat.checkSelfPermission(mcontext, Manifest.permission.READ_CONTACTS);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
Try this code .. 试试这个代码..
/**
* this method check permission and return current state of permission need.
*/
private boolean checkPermissions() {
int permissionState = ActivityCompat.checkSelfPermission(this,
Manifest.permission.CAMERA);
return permissionState == PackageManager.PERMISSION_GRANTED;
}
/**
* this method request to permission asked.
*/
private void requestPermissions() {
boolean shouldProvideRationale =
ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA);
if (shouldProvideRationale) {
Log.i(TAG, "Displaying permission rationale to provide additional context.");
} else {
Log.i(TAG, "Requesting permission");
// previously and checked "Never ask again".
}
}
/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
Log.i(TAG, "onRequestPermissionResult");
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
if (grantResults.length <= 0) {
// If user interaction was interrupted, the permission request is cancelled and you
// receive empty arrays.
Log.i(TAG, "User interaction was cancelled.");
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission was granted. Kick off the process of building and connecting
// GoogleApiClient.
buildGoogleApiClient(); // hear your operation method.
}
else {
// Permission denied.
}
}
}
then after check like below code into oncreate method .. 然后像下面的代码检查后进入oncreate方法..
if (!checkPermissions()) {
requestPermissions();
}
buildGoogleApiClient(); // your method define.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.