Android BroadcastRecevier for App Permissions - java

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.

Related

Permissions in BroadcastReceiver on Android 6

whats wrong with runtime permissions in broadcast receivers on Android 6?
I have call log broadcast receiver which I defined in AndroidManifest and
it starts when I launch this app. MainActivity contain onCreate method, in this method
I try to check permissions and call permission dialog, but it's too late, because
broadcast receiver is starting before onCreate method and then I don't have permission
to read call log.

Start Activity on Reboot Only

Basically I am trying to make an activity containing a button which reboots the device and after the reboot returns to the same activity.
I understand that this question may get confused with others about rebooting the device, but that is not the focus of this question as I can get the device to reboot fine.
I have made the button reboot the device but the only way I can get it to start the activity after it's finished rebooting is to register a broadcast receiver for BOOT_COMPLETED in the manifest. The trouble is that this method starts the activity every time the device boots which is undesirable. When I register the receiver on the button click listener it does not start the activity after the reboot.
I was wondering if there might be an extra in BOOT_COMPLETED that I could use to decide if it had been purposefully rebooted.
Any advice would be appreciated, thanks in advance!
Just save an integer corresponding to device purposely being rebooted through your activity. Use SharedPreference for the same. On reboot, in your broadcast receiver, check if the value is set. If it is set, start your activity, otherwise, let it go.
EDIT :
Always, unset this value when reboot is complete and your Activity is in front.
Your XML should be stored in a file named AndroidManifest.xml, not manifest.java.
Another reason your code is not being run, might be that your App is installed on external storage (sdcard). BOOT_COMPLETE is sent to applications before external storage is mounted. So if application is installed to external storage it won't receive BOOT_COMPLETE broadcast message.
If that isn't the problem, there is already a very good description of how to get boot completed receivers working on Android.
Trying to start a service on boot on Android

Is there any way to block SMS programmatically in Android KitKat?

Is this completely and utterly impossible in android Kitkat since Google has made so many changes to the way messaging works? I have tried using broadcast receivers and abortBroadcast, but to no avail.
Is there any way to block SMS programmatically in Android KitKat?
No. Starting with KitKat, the SMS_RECEIVED broadcast cannot be aborted, so any app with the RECEIVE_SMS permission can still listen for it and retrieve the incoming message. If your app is the default app, it can choose not to write the message to the Provider, so it will not appear to any app querying the Provider for messages, but even the default app cannot abort the SMS_RECEIVED broadcast.

SMS Broadcast Receiver not working in KitKat

My app needs to receive SMS. So far it seems to work on real devices and emulators from Gingerbread to JellyBean. I'm now testing it in a KitKat emulator, and the Broadcast Receiver is not fired.
I'm registering a the receiver in the manifest using the classic sms action:
<receiver android:name=".receivers.MyReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
I've also added the SMS permission in the manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
I've read the blog post on SMS changes in KitKat, where they say this intent filter should still work:
Any filters for the SMS_RECEIVED_ACTION broadcast in existing apps will continue to work the same on Android 4.4, but only as an observer of new messages, because unless your app also receives the SMS_DELIVER_ACTION broadcast, you cannot write to the SMS Provider on Android 4.4.
Which is fine because I just need to listen for new messages, and I don't need my app to be the default SMS app nor modify the provider (This is a very simple app with no activities, only a receiver and a service that is called from the receiver).
The emulator is very laggy, so I think it might be a problem with the emulator only. Too bad I don't have any KitKat device to test it. For now I'm testing in KitKat using the DDMS telephony tab. I've added logging to the receiver but I don't see anything in logcat. There are no exceptions nor any other message that could indicate problems. It looks the receiver is not being notified, but on the other hand I've double-checked and the app is installed.
What could be going wrong?
As #Seshu Vinai has pointed in the comments, the problem turns out to be the security restriction introduced in Android 3.1. I thought it was not the case since i had tested it before on JellyBean devices and emulators and the app seemed to work. But it did so only because I had previously launched an activity fron some unit tests, and thus the app was activated.
You can find more info in the following questions:
How to start a Service when .apk is Installed for the first time
Android BroadcastReceiver won't work
Android BroadcastReceiver not working after install
The broadcast receiver will not be called unless your app is the default app in KITKAT. You will be able get the contents directly, but you will not be able to listen to new messages. I had the same problem. I got to know after making my App as default.
Try making your app the default. You will notice the difference.

Attempt to change component state security exception in android

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.

Categories