Created
May 23, 2016 09:28
-
-
Save Isabellle/3cf984b0315665e29b916a349352b283 to your computer and use it in GitHub Desktop.
Request one permission on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package isabelle.com.requestpermissiondemo; | |
import android.Manifest; | |
import android.annotation.TargetApi; | |
import android.content.DialogInterface; | |
import android.content.pm.PackageManager; | |
import android.os.Build; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
private final String TAG = MainActivity.class.getSimpleName(); | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
insertDummyContactWrapper(); | |
} | |
final private int REQUEST_CODE_ASK_PERMISSIONS = 123; | |
@TargetApi(Build.VERSION_CODES.M) | |
private void insertDummyContactWrapper() { | |
int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.WRITE_CONTACTS); | |
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) { | |
if (!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_CONTACTS)) { | |
showMessageOKCancel("You need to allow access to Contacts", | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
requestPermissions(new String[] {Manifest.permission.WRITE_CONTACTS}, | |
REQUEST_CODE_ASK_PERMISSIONS); | |
} | |
}); | |
return; | |
} | |
requestPermissions(new String[] {Manifest.permission.WRITE_CONTACTS}, | |
REQUEST_CODE_ASK_PERMISSIONS); | |
return; | |
} | |
Toast.makeText(this, "Insert contact", Toast.LENGTH_SHORT).show(); | |
} | |
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) { | |
new AlertDialog.Builder(MainActivity.this) | |
.setMessage(message) | |
.setPositiveButton("OK", okListener) | |
.setNegativeButton("Cancel", null) | |
.create() | |
.show(); | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { | |
switch (requestCode) { | |
case REQUEST_CODE_ASK_PERMISSIONS: | |
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
// Permission Granted | |
Toast.makeText(this, "Insert contact", Toast.LENGTH_SHORT).show(); | |
} else { | |
// Permission Denied | |
Toast.makeText(MainActivity.this, "WRITE_CONTACTS Denied", Toast.LENGTH_SHORT) | |
.show(); | |
} | |
break; | |
default: | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment