Skip to content

Instantly share code, notes, and snippets.

@bejibx
Last active August 10, 2016 13:59
Show Gist options
  • Save bejibx/bf4a9db15a03199091daec40b4225352 to your computer and use it in GitHub Desktop.
Save bejibx/bf4a9db15a03199091daec40b4225352 to your computer and use it in GitHub Desktop.
Cancel runtime permission request in Android
import android.Manifest;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import java.util.Arrays;
public class CancellablePermissionRequestActivity extends AppCompatActivity {
private static final String LOG_TAG = CancellablePermissionRequestActivity.class
.getSimpleName();
private static final int NOTIFICATION_ID = 7211;
private static final int REQUEST_CODE_FINISH = 996;
private static final int REQUEST_CODE_PERMISSION = 1112;
private Intent getFinishRequestIntent() {
return new Intent(this, CancellablePermissionRequestActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission_request_proxy);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
Activity activity = CancellablePermissionRequestActivity.this;
PendingIntent pendingIntent = PendingIntent
.getActivity(activity, REQUEST_CODE_FINISH, getFinishRequestIntent(),
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(activity)
.setContentText("Click to stop permission request")
.setContentTitle("Title")
.setSmallIcon(android.R.drawable.ic_lock_power_off)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
NotificationManager notificationManager
= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
ActivityCompat.requestPermissions(activity, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_PERMISSION);
}
});
}
private void cancelNotification() {
NotificationManager notificationManager
= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}
@Override
public void onRequestPermissionsResult(final int requestCode,
@NonNull final String[] permissions,
@NonNull final int[] grantResults) {
Log.d(LOG_TAG, String
.format("onRequestPermissionsResult(requestCode: %d; permissions: %s; " +
"grantResults: %s)", requestCode, Arrays.toString(permissions),
Arrays.toString(grantResults)));
cancelNotification();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment