Created
December 13, 2015 16:59
-
-
Save k4zy/8c46c8155a1bba1d9431 to your computer and use it in GitHub Desktop.
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
@RuntimePermissions | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Button button = (Button) findViewById(R.id.button); | |
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
MainActivityPermissionsDispatcher.makePhoneCallToSampleNumberWithCheck(MainActivity.this); | |
} | |
}); | |
} | |
@NeedsPermission(Manifest.permission.CALL_PHONE) | |
public void makePhoneCallToSampleNumber() { | |
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:0123456789")); | |
startActivity(intent); | |
} | |
@SuppressWarnings("unused") | |
@OnPermissionDenied(Manifest.permission.CALL_PHONE) | |
void deniedPermission() { | |
if (PermissionUtils.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) { | |
Toast.makeText(this, "電話をかけるのに失敗しました。", Toast.LENGTH_SHORT).show(); | |
} else { | |
new MaterialDialog.Builder(this) | |
.theme(Theme.LIGHT) | |
.title("電話をかけることが出来ません") | |
.content("電話をかけるためには、設定画面からご自身で権限を許可して頂く必要があります。") | |
.positiveText("設定画面へ") | |
.negativeText("今はしない") | |
.onPositive(new MaterialDialog.SingleButtonCallback() { | |
@Override | |
public void onClick(MaterialDialog dialog, DialogAction action) { | |
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); | |
Uri uri = Uri.fromParts("package", getPackageName(), null); | |
intent.setData(uri); | |
startActivity(intent); | |
} | |
}) | |
.show(); | |
} | |
} | |
@SuppressWarnings("unused") | |
@OnShowRationale(Manifest.permission.CALL_PHONE) | |
void showRationalForStorage(final PermissionRequest request) { | |
new MaterialDialog.Builder(this) | |
.theme(Theme.LIGHT) | |
.title("電話をかけるには許可が必要です") | |
.content("通話の発信/管理には、ユーザーの方に権限の許可をいただく必要があります。") | |
.positiveText("許可する") | |
.negativeText("今はしない") | |
.onPositive(new MaterialDialog.SingleButtonCallback() { | |
@Override | |
public void onClick(MaterialDialog dialog, DialogAction action) { | |
request.proceed(); | |
} | |
}) | |
.show(); | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { | |
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment