Attempt to change component state security exception in android - java

hi i am working in framework side implementing one test app to disable another app but when i run my test app it is showing
Java.lang.SecurityException: Permission Denial: attempt to change component state from pid=xxx,uid=xxxx,package uid=xxx
at xxxxxxxx
my java code
PackageManager pm = getApplicationContext().getPackagemanager();
pm.setComponentEnabledSetting(new ComponentName("com.example","MainActivity"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
AndroidManifest.xml
<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE>
i have tried above permission with both (Permission is only granted to system apps) and also tried tag but no lucky
how to use system apps granted permission,
Could you please guys help me to resolve this issue
Thanks in advance

If you application is not a part of the system partition, then you will not be granted the permission. Get the app into the system partition and confirm that the PackageManager upon parsing your app during the dexopt grants you your desired permission.

You can not Enable/disable app unless your app is system app

My steps are:
Disable package with command: pm disable "packageName"
Enable packageName with command: pm enable packageName
Launch principal activity if you know the name activity principal,
Intent i = new Intent(Intent.ACTION_MAIN);
i.setComponent(new ComponentName("namePackage", "namePackage.MainPrincipal"));
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);

If your device is rooted, you can install your app as a system app by simply copying the apk into /system/app (/system/priv-app for 4.4+). This will allow you to use the "system only" permissions.

Related

Android: App Can't Be Granted SYSTEM_ALERT_WINDOW,

I am creating an android application that uses WindowManager type: TYPE_APLICATION_OVERLAY so it requires this permission in the manifest file:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Which already exists in the AndroidManifest.xml.
The problem is that once I launched my app, then go manually to my device's Draw Over Other Apps settings to grant the said permission, my app is not there.
Is there something missing? Or is Draw Over Other Apps settings the correct place to go to grant SYSTEM_ALERT_WINDOW?
My target device is android 11, api level 30.
I've followed exactly this instruction here.
I've tried launching an Intent action Settings.ACTION_MANAGE_OVERLAY_PERMISSION to automatically go the same setting to grant permission but it just go to my device's Draw Over Other Apps settings to which my app is excluded.
Screenshot to Draw Over Other Apps Settings

Android BroadcastRecevier for App Permissions

How would you create a broadcast receiver for when there is a change in your app's permissions?
You cannot create a BroadcastReceiver for App permissions since there is no broadcast. As soon as there is a permission change, the app is restarted.
EDIT: You can check for permissions yourselves by following this.

How to use code to open android.permission.SYSTEM_ALERT_WINDOW permission

In my app, I need to use android.permission.SYSTEM_ALERT_WINDOW permission. Now I know to go to setting to open it. But some of the application is automatically opened. I want to know how to do it
If i got it right, you want to ask the user for some permissions on app launch. There's a great library to help you do that in Android :
Github link for the Permission dispatcher library
You can also check these libraries :
App-Runtime-Permissions-Android
Assent
MarshmallowPermissionManager
Every app that requests the SYSTEM_ALERT_WINDOW permission and that is installed through the Play Store (version 6.0.5 or higher is required), will have granted the permission automatically.
If instead the app is sideloaded, the permission is not automatically granted. You can try to download and install the Evernote APK from apkmirror.com. As you can see you need to manually grant the permission in Settings -> Apps -> Draw over other apps.
It is a new behaviour introduced in Marshmallow 6.0.1.
If you want the app to be sideloaded, you show manually show a prompt and direct the user to enable Draw over other apps permissions from the settings. Have a look at Requesting permissions

Android 6.0 RuntimeException: Fail to connect to camera service

An error occurred with my app which ran in Nexus5 (Android6.0).
The error was
java.lang.RuntimeException: Fail to connect to camera service
I had added the permission to the AndroidManifest.xml.
But the app is OK with another phone(Android5.1), and genymotion AVD(Android 4.0).
The key code is below
#Override
public void onResume() {
super.onResume();
try {
mCamera = Camera.open();
}catch (Exception e){
Log.e(TAG,"open camera failed",e);
}
}
Write in your gradle.build targetSdkVersion 22. It is a temporary solution but work.
open Settings -> Apps -> click you app -> Permissions -> open Camera permissions.
you can see:
http://developer.android.com/training/permissions/requesting.html
This happens because, in Android 6.0 the permission model is different. You have to implement the new permission model which asks for the permission on run time. Even if you don't ask it, you can manually enable it in the phone's app setting, but that is not gona work when you publish your app in the play store.
Here is an Article on How to get Run time permissions
Also you can check Nick's answer here for getting multiple permissions
From android 6.0 you can handle the app permission weather you will give or not specific permission for an application.
In your case if you didn't enable camera permission for your app then this issues may arise.
So, you need to enable permission from settings->apps->your_app->enable camera permission.
Add Camera Permission before Opening the Camera:
follow the Link for adding Permissions:
https://developer.android.com/training/permissions/requesting.html

Accessing security system settings

How would you access the security system settings eg airplane mode.. from android studio. I can do it through adb but I'd prefer to do it through android studio in my application. I tried writing code that gives the user an option to select 'modify system settings options' to give full access but it still keeps telling Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS, most of the answers are a bit old so it would be nice if someone directed me to a path
All permissions that your app wants to use (INTERNET,BLUETOOTH, etc) must be defined on the Manifest.XML file like this:
<uses-permission android:name="permission here"/>
Spesifically the WRITE_SECURE_SETTINGS permission's protection level is "signature" (more on that here) and that means that permission is only granted to systems apps. So to "fix" your Permission denial error you need to add this permission into the Manifest.XML file, but this is redundant because it wont let you USE that permission due to its protection level.

Categories