When I have a target API of 23 on Android M Preview 3, I cannot seem to acquire the Manifest.permission.WRITE_SETTTINGS permission.
requestPermissions(new String[]{Manifest.permission.WRITE_SETTINGS}, 101);
Request permission doesn't bring up the dialog I would expect, but if I make the following call without this permission,
RingtoneManager.setActualDefaultRingtoneUri(activity, RingtoneManager.TYPE_RINGTONE, ringUri);
The call will except because I don't have the permission.
I'm not sure where to go from here. Is there a new ringtone API for 23? Or did this permission change just make it impossible for any non-system apps to change the ringtone?
To use WRITE_SETTINGS, based on the docs:
Have the <uses-permission> element in the manifest as normal.
Call Settings.System.canWrite() to see if you are eligible to write out settings.
If canWrite() returns false, start up the ACTION_MANAGE_WRITE_SETTINGS activity so the user can agree there to allow your app to actually write to settings.
In other words, writing to settings is now a double-opt-in (agree to install, agree separately in Settings to allow), akin to device admin APIs, accessibility services, etc.
Also note that I have not tried using these yet — this is based on research that I did yesterday on Android 6.0 changes.
In addition to the answer from CommonsWare and the comment from Ogix, here is some dummy code:
private boolean checkSystemWritePermission() {
boolean retVal = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
retVal = Settings.System.canWrite(this);
Log.d(TAG, "Can Write Settings: " + retVal);
if(retVal){
Toast.makeText(this, "Write allowed :-)", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Write not allowed :-(", Toast.LENGTH_LONG).show();
FragmentManager fm = getFragmentManager();
PopupWritePermission dialogFragment = new PopupWritePermission();
dialogFragment.show(fm, getString(R.string.popup_writesettings_title));
}
}
return retVal;
}
The Fragment PopupwritePermission then gives a window where the situation is explained. A click on the OK Button will open the Android System Menu where the Permission can be granted:
private void openAndroidPermissionsMenu() {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
startActivity(intent);
}
The previous answers are great, I have just little addition for also getting the result for the permission asking.
public static void youDesirePermissionCode(Activity context){
boolean permission;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permission = Settings.System.canWrite(context);
} else {
permission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
}
if (permission) {
//do your code
} else {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
context.startActivityForResult(intent, MainActivity.CODE_WRITE_SETTINGS_PERMISSION);
} else {
ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_SETTINGS}, MainActivity.CODE_WRITE_SETTINGS_PERMISSION);
}
}
}
And then in the Activity:
#SuppressLint("NewApi")
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MainActivity.CODE_WRITE_SETTINGS_PERMISSION && Settings.System.canWrite(this)){
Log.d("TAG", "MainActivity.CODE_WRITE_SETTINGS_PERMISSION success");
//do your code
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MainActivity.CODE_WRITE_SETTINGS_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//do your code
}
}
This is a complete example:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.System.canWrite(context) {
// Do stuff here
}
else {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
As of android Marshmellow , you require to use runtime permissions which aims to more security , or use permission when need here is documenatation
and for Write Settings documentation is here
In manifest add
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
In your class
private boolean checkSystemWritePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(Settings.System.canWrite(context))
return true;
else
openAndroidPermissionsMenu();
}
return false;
}
private void openAndroidPermissionsMenu() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
context.startActivity(intent);
}
}
And use it like this
try {
if (checkSystemWritePermission()) {
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
Toast.makeText(context, "Set as ringtoon successfully ", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "Allow modify system settings ==> ON ", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.i("ringtoon",e.toString());
Toast.makeText(context, "unable to set as Ringtoon ", Toast.LENGTH_SHORT).show();
}
The permission android.permission.WRITE_SETTINGS is now in the group signature|appop|pre23|preinstalled like android.permission.CHANGE_NETWORK_STATE and android.permission.SYSTEM_ALERT_WINDOW
This means you get it on sdk 22 and below. On newer version you have to be an app operator.
I have used bellow like..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean retVal = true;
retVal = Settings.System.canWrite(this);
if (retVal == false) {
if (!Settings.System.canWrite(getApplicationContext())) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
Toast.makeText(getApplicationContext(), "Please, allow system settings for automatic logout ", Toast.LENGTH_LONG).show();
startActivityForResult(intent, 200);
}
}else {
Toast.makeText(getApplicationContext(), "You are not allowed to wright ", Toast.LENGTH_LONG).show();
}
}
Manifest permission
<uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions" />
• Kotlin Version in Simple Steps
Follow these steps:
1. Add the permission's usage element in the manifest.xml normally:
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
2. Where you want to change the settings, check the write access:
if (context.canWriteSettings) {
// change the settings here ...
} else {
startManageWriteSettingsPermission()
}
3. Also add these lines of code in case of requesting the permission:
private fun startManageWriteSettingsPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent(
Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:${context.packageName}")
).let {
startActivityForResult(it, REQUEST_CODE_WRITE_SETTINGS_PERMISSION)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
REQUEST_CODE_WRITE_SETTINGS_PERMISSION -> {
if (context.canWriteSettings) {
// change the settings here ...
} else {
Toast.makeText(context, "Write settings permission is not granted!", Toast.LENGTH_SHORT).show()
}
}
}
}
val Context.canWriteSettings: Boolean
get() = Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.System.canWrite(this)
companion object {
private const val REQUEST_CODE_WRITE_SETTINGS_PERMISSION = 5
}
Mention below permission in AndroidManifest.xml
In Activity use below if else for changing setting.
if(Settings.System.canWrite(this)){
// change setting here
}
else{
//Migrate to Setting write permission screen.
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + mContext.getPackageName()));
startActivity(intent);
}
In my case i have solved by this way.:
public void checkSystemWriteSettings(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(context)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + context.getApplicationInfo().packageName));
startActivity(intent);
}else
{
Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 10);
}
}
}
Related
I have had this code working for some time now but with Android Orea it doesn't work.
Context context = mContextRef.get();
if (context == null) {
return;
}
// Get the update
String filepath = getDownloadLocation();
File apkFile = new File(filepath);
Uri fileLoc;
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
fileLoc = android.support.v4.content.FileProvider.getUriForFile(context, mFileProviderAuthority, apkFile);
} else {
intent = new Intent(Intent.ACTION_VIEW);
fileLoc = Uri.fromFile(apkFile);
}
intent.setDataAndType(fileLoc, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
This code works with all version of android except 8.0 and above.
Edit
Sorry for not explaining more. IT doesn't work meaning, it flashes a window for a fraction of a second and disappears (some times imperceptibly) and continues on in the old app. The file is successfully downloaded (I can navigate to it and install) but when I try to start the activity to install it programmatically it fails with no errors or logs at all.
Apparently if your app targets later versions of android the android.permission.REQUEST_INSTALL_PACKAGES permission is required in the manifest otherwise nothing happens except a single line error in the LogCat.
So include the following in manifest.xml file:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
Manifest API for REQUEST_INSTALL_PACKAGES
Please add the <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/> permission in Manifest.xml first.
We should check the result of getPackageManager().canRequestPackageInstalls() if the SDK version is equal or large than 26.
The code is below:
private void checkIsAndroidO() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean result = getPackageManager().canRequestPackageInstalls();
if (result) {
installApk();
} else {
// request the permission
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.REQUEST_INSTALL_PACKAGES}, INSTALL_PACKAGES_REQUESTCODE);
}
} else {
installApk();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case INSTALL_PACKAGES_REQUESTCODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
installApk();
} else {
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
startActivityForResult(intent, GET_UNKNOWN_APP_SOURCES);
}
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case GET_UNKNOWN_APP_SOURCES:
checkIsAndroidO();
break;
default:
break;
}
}
I hope it will help you.
I am trying to open System default Messaging activity when clicking on a button. Here is my code:
smsUri = Uri.parse("tel:" + teacherPhone2);
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("address", teacherPhone);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
It works fine below MarshMallow. But in MarshMallow the app crashes when clicking that button. I have set the user-permission and also requested send sms permission.
Can anyone tell me why this happens?
Check runtime permission before sending sms:
if (Build.VERSION.SDK_INT >= 23) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(RegistrationActivity.this,Manifest.permission.SEND_SMS);
if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(RegistrationActivity.this,new String[]{Manifest.permission.SEND_SMS},SEND_SMS);
return;
}else{
sendSms(mobile);
}
} else {
sendSms(mobile);
}
1. Add SEND_SMS permission in manifest
<uses-permission android:name="android.permission.SEND_SMS"/>
2. Request permission if not granted, send sms otherwise
if (Build.VERSION.SDK_INT >= 23) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(RegistrationActivity.this,Manifest.permission.SEND_SMS);
if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(RegistrationActivity.this,new String[]{Manifest.permission.SEND_SMS},SEND_SMS);
return;
} else{
sendSms(mobile);
}
} else {
sendSms(mobile);
}
3. Override onActivityResult in order to send sms after permission is granted
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(RegistrationActivity.this,Manifest.permission.SEND_SMS);
if(checkCallPhonePermission == PackageManager.PERMISSION_GRANTED){
sendSms(mobile);
}
}
The reason may be in MarshMallow we can turn off and on permissions for particular app. So check for permissions turned on or off before sending SMS.
String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.CAMERA};
if(!hasPermissions(this, PERMISSIONS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
try this code
From my activity when i call Contacts to get a contact number the application crashed. Application is running ok on other devices but when i'm try to run it in android 6.0, it crashed. i've no idea what i'm doing wrong.
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
After getting the the data in onActivityResult method.
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = this.getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
Log.d(TAG, "name : "+name+" , Phone Number : "+ phoneNumber);
}
}
break;
}
}
can Anyone help me ?
All of the answers here are wrong. You shouldn't need READ_CONTACT permission at all to retrieve single contact using ACTION_PICK intent. You should be granted this permission temporary to be able to retrieve contact specific data. However there are some devices that don't implement this API in a good way. Myself I have found Sony Xperia Z3, but I heard about HTC devices also had this problem.
To have the user select a contact and provide your app access to all the contact information, use the ACTION_PICK action and specify the MIME type to Contacts.CONTENT_TYPE.
The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact. The response grants your app temporary permissions to read that contact using the Contacts Provider API even if your app does not include the READ_CONTACTS permission.
Source: https://developer.android.com/guide/components/intents-common.html#Contacts
[UPDATE]
After upgrading Sony Xperia Z3 system image to version 23.5.A.1.291 bug does not occur anymore.
From android 6.0 (Mashmello) , android has introduced Run Time Permissions for users to grand permission to apps while the app is running, not when they install the app. So, Your calling should be like this ->
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
/*If Android M*/
if (ContextCompat.checkSelfPermission(SendMoneyByDetailsActivity.this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
/*If Android M and Not permission granted */
ActivityCompat.requestPermissions(SendMoneyByDetailsActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS);
} else {
/*If Android M and permission granted */
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
} else {
/*IF not Android M*/
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
And when user grant the permission then you can call the intent to pick contact list ->
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 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
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
} else {
// permission denied
}
return;
}
}
}
And don't forgot to add permission in your manifest file.
<uses-permission android:name="android.permission.READ_CONTACTS" />
It not your manifest problem its problem with android 6.0 it requires Runtime Permissions...
after adding this in manifest
<uses-permission android:name="android.permission.READ_CONTACTS" />
Just change your code to this In your code..
public class MainActivity extends Activity {
public static final int REQUIRED_PERMISSIONS = 001;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//give here permissions what ever you want...
// if you are using one add only one... not all..
if ((CheckPermission(this, Manifest.permission.READ_CONTACTS))&&
(CheckPermission(this, Manifest.permission.READ_PHONE_STATE))&&
(CheckPermission(this, Manifest.permission.INTERNET))&&
(CheckPermission(this, Manifest.permission.ACCESS_NETWORK_STATE))&&
(CheckPermission(this, Manifest.permission.ACCESS_WIFI_STATE)))
{
PermHandling();
}
//now reqest runtime permissions..
else {
RequestPermission(MainActivity.this, Manifest.permission.READ_CONTACTS, REQUIRED_PERMISSIONS);
RequestPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE, REQUIRED_PERMISSIONS );
RequestPermission(MainActivity.this, Manifest.permission.INTERNET, REQUIRED_PERMISSIONS );
RequestPermission(MainActivity.this, Manifest.permission.ACCESS_NETWORK_STATE, REQUIRED_PERMISSIONS );
RequestPermission(MainActivity.this, Manifest.permission.ACCESS_WIFI_STATE, REQUIRED_PERMISSIONS );
}
}
private void PermHandling() {
//Your app internal parts....
//Here your stuff works...
}
//private void NewPermHandling(){
//}
#Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case REQUIRED_PERMISSIONS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
PermHandling();
} else {
//Toast.makeText(this, "Please Grant Permissions other wise app will close.!", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
if (ContextCompat.checkSelfPermission(thisActivity,
Permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Permission)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Permission},
Code);
}
}
}
public boolean CheckPermission(Context context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
}
I also faced same problem my applications worked on till 5.1 but 6.0+ due to Runtime permissions We need to add them.. and accept those permissions at runtime
I think this is permission issue
add bellow permission inside manifest file
<uses-permission android:name="android.permission.READ_CONTACTS" />
accept permissions in marshmallows
https://developer.android.com/training/permissions/requesting.html
actual implementation
add bellow code inside Main Activity
call checkAndAddPermission() method inside onCreate() method
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 1244;
private void checkAndAddPermission() {
List<String> permissionsNeeded = new ArrayList<>();
final List<String> permissionsList = new ArrayList<>();
if (!addPermission(permissionsList, android.Manifest.permission.READ_CONTACTS))
permissionsNeeded.add("ReadContacts");
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
ActivityCompat.requestPermissions(MainActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
} else {
ActivityCompat.requestPermissions(MainActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
}
#TargetApi(Build.VERSION_CODES.M)
private boolean addPermission(List<String> permissionsList, String permission) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
return false;
}
return true;
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
// Initial
perms.put(android.Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(android.Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
This is what I am doing now. I am getting the following error
android.content.ActivityNotFoundException: No Activity found to handle
Intent { act=android.intent.action.CALL dat=16477210790 }
#Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(store_contact_no.getText().toString()));
startActivity(callIntent);
}
You don't need to declare dial intent-filter in manifest and don't need any permissions to ACTION_DIAL. Look for my implementation
private void startDialActivity(String phone){
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+phone));
startActivity(intent);}
also is good to check is telephony supported on device
private boolean isTelephonyEnabled(){
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
return tm != null && tm.getSimState()==TelephonyManager.SIM_STATE_READY;}
use this code.
this may help you.
Add this to your manifest
<uses-permission android:name="android.permission.CALL_PHONE" />
In Activity check permission,
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 1);
}
} else {
Call(sNumber);
}
where sNumber is your number in string
then Override onRequestPermissionsResult,
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 100:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted,
Call(sNumber);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 1);
}
}
break;
}
}
add this method,
private void Call(String sNumber)
{
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + sNumber));
startActivity(callIntent);
}
When I have a target API of 23 on Android M Preview 3, I cannot seem to acquire the Manifest.permission.WRITE_SETTTINGS permission.
requestPermissions(new String[]{Manifest.permission.WRITE_SETTINGS}, 101);
Request permission doesn't bring up the dialog I would expect, but if I make the following call without this permission,
RingtoneManager.setActualDefaultRingtoneUri(activity, RingtoneManager.TYPE_RINGTONE, ringUri);
The call will except because I don't have the permission.
I'm not sure where to go from here. Is there a new ringtone API for 23? Or did this permission change just make it impossible for any non-system apps to change the ringtone?
To use WRITE_SETTINGS, based on the docs:
Have the <uses-permission> element in the manifest as normal.
Call Settings.System.canWrite() to see if you are eligible to write out settings.
If canWrite() returns false, start up the ACTION_MANAGE_WRITE_SETTINGS activity so the user can agree there to allow your app to actually write to settings.
In other words, writing to settings is now a double-opt-in (agree to install, agree separately in Settings to allow), akin to device admin APIs, accessibility services, etc.
Also note that I have not tried using these yet — this is based on research that I did yesterday on Android 6.0 changes.
In addition to the answer from CommonsWare and the comment from Ogix, here is some dummy code:
private boolean checkSystemWritePermission() {
boolean retVal = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
retVal = Settings.System.canWrite(this);
Log.d(TAG, "Can Write Settings: " + retVal);
if(retVal){
Toast.makeText(this, "Write allowed :-)", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Write not allowed :-(", Toast.LENGTH_LONG).show();
FragmentManager fm = getFragmentManager();
PopupWritePermission dialogFragment = new PopupWritePermission();
dialogFragment.show(fm, getString(R.string.popup_writesettings_title));
}
}
return retVal;
}
The Fragment PopupwritePermission then gives a window where the situation is explained. A click on the OK Button will open the Android System Menu where the Permission can be granted:
private void openAndroidPermissionsMenu() {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
startActivity(intent);
}
The previous answers are great, I have just little addition for also getting the result for the permission asking.
public static void youDesirePermissionCode(Activity context){
boolean permission;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permission = Settings.System.canWrite(context);
} else {
permission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
}
if (permission) {
//do your code
} else {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
context.startActivityForResult(intent, MainActivity.CODE_WRITE_SETTINGS_PERMISSION);
} else {
ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_SETTINGS}, MainActivity.CODE_WRITE_SETTINGS_PERMISSION);
}
}
}
And then in the Activity:
#SuppressLint("NewApi")
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MainActivity.CODE_WRITE_SETTINGS_PERMISSION && Settings.System.canWrite(this)){
Log.d("TAG", "MainActivity.CODE_WRITE_SETTINGS_PERMISSION success");
//do your code
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MainActivity.CODE_WRITE_SETTINGS_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//do your code
}
}
This is a complete example:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.System.canWrite(context) {
// Do stuff here
}
else {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
As of android Marshmellow , you require to use runtime permissions which aims to more security , or use permission when need here is documenatation
and for Write Settings documentation is here
In manifest add
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
In your class
private boolean checkSystemWritePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(Settings.System.canWrite(context))
return true;
else
openAndroidPermissionsMenu();
}
return false;
}
private void openAndroidPermissionsMenu() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
context.startActivity(intent);
}
}
And use it like this
try {
if (checkSystemWritePermission()) {
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
Toast.makeText(context, "Set as ringtoon successfully ", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "Allow modify system settings ==> ON ", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.i("ringtoon",e.toString());
Toast.makeText(context, "unable to set as Ringtoon ", Toast.LENGTH_SHORT).show();
}
The permission android.permission.WRITE_SETTINGS is now in the group signature|appop|pre23|preinstalled like android.permission.CHANGE_NETWORK_STATE and android.permission.SYSTEM_ALERT_WINDOW
This means you get it on sdk 22 and below. On newer version you have to be an app operator.
I have used bellow like..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean retVal = true;
retVal = Settings.System.canWrite(this);
if (retVal == false) {
if (!Settings.System.canWrite(getApplicationContext())) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
Toast.makeText(getApplicationContext(), "Please, allow system settings for automatic logout ", Toast.LENGTH_LONG).show();
startActivityForResult(intent, 200);
}
}else {
Toast.makeText(getApplicationContext(), "You are not allowed to wright ", Toast.LENGTH_LONG).show();
}
}
Manifest permission
<uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions" />
• Kotlin Version in Simple Steps
Follow these steps:
1. Add the permission's usage element in the manifest.xml normally:
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
2. Where you want to change the settings, check the write access:
if (context.canWriteSettings) {
// change the settings here ...
} else {
startManageWriteSettingsPermission()
}
3. Also add these lines of code in case of requesting the permission:
private fun startManageWriteSettingsPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent(
Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:${context.packageName}")
).let {
startActivityForResult(it, REQUEST_CODE_WRITE_SETTINGS_PERMISSION)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
REQUEST_CODE_WRITE_SETTINGS_PERMISSION -> {
if (context.canWriteSettings) {
// change the settings here ...
} else {
Toast.makeText(context, "Write settings permission is not granted!", Toast.LENGTH_SHORT).show()
}
}
}
}
val Context.canWriteSettings: Boolean
get() = Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.System.canWrite(this)
companion object {
private const val REQUEST_CODE_WRITE_SETTINGS_PERMISSION = 5
}
Mention below permission in AndroidManifest.xml
In Activity use below if else for changing setting.
if(Settings.System.canWrite(this)){
// change setting here
}
else{
//Migrate to Setting write permission screen.
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + mContext.getPackageName()));
startActivity(intent);
}
In my case i have solved by this way.:
public void checkSystemWriteSettings(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(context)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + context.getApplicationInfo().packageName));
startActivity(intent);
}else
{
Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 10);
}
}
}