In my first application, I define a custom permission and an implicit BroadcastReceiver in manifest file:
<permission
android:name="com.example.test.TEST"
android:protectionLevel="signature" />
<receiver
android:name=".TestBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:permission="com.example.test.TEST">
<intent-filter>
<action android:name="com.example.test.TEST_RECEIVER" />
</intent-filter>
</receiver>
And this is the TestBroadcastReceiver.java:
public class TestBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Test", "Hello World!");
Toast.makeText(context, "Hello World!", Toast.LENGTH_LONG).show();
}
}
In my second app, I've added the permission in manifest file:
<uses-permission android:name="com.example.test.TEST" />
And here, I send the broadcast:
getActivity().sendBroadcast(new Intent("com.example.test.TEST_RECEIVER"));
But nothing is called in first app. I know we can't use implicit broadcast in android O and above but according to here, there is an exception for broadcasts that require a signature permission:
Broadcasts that require a signature permission are exempted from this
restriction, since these broadcasts are only sent to apps that are
signed with the same certificate, not to all the apps on the device.
So how can I signal my other apps in android O?
According to CommonsWare answer, the problem is that I was missing setPackage() part. So I changed the code as below and now broadcast is received:
getActivity().sendBroadcast(new Intent("com.example.test.TEST_RECEIVER").setPackage("com.example.test"));
Related
I need a Broadcast Receiver name "NetworkReciver.java" that should execute when Internet is Connected or Disconnected. But it is noit executing.
Manifest permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<receiver android:name=".NetworkReciever" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
In MainActivity.java requested permissions
String[] permissionsRequired = new String[]{Manifest.permission.SET_ALARM,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,};
// Since these are Normal Permissions
ActivityCompat.requestPermissions(this, permissionsRequired, 1);
NetworkReciever.java:
public class NetworkReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.i("KAMLESH", "Network REceiver Alarm Set in Hair Style");
}
}
you must register NetworkReciever this broadcast receiver like registerReceiver(new NetworkReciever()); in any activity
From documentation
Apps targeting Android 7.0 (API level 24) and higher do not receive CONNECTIVITY_ACTION broadcasts if they declare the broadcast receiver in their manifest. Apps will still receive CONNECTIVITY_ACTION broadcasts if they register their BroadcastReceiver with Context.registerReceiver() and that context is still valid.
It means you need to register your receiver dynamically, not from manifest. Read Dynamically register/unregister a broadcast receiver in android which will give you the idea about dynamic way.
NOTE : The CONNECTIVITY_ACTION is deprecated since API 28. There are few other options introduced and you can find in the same url CONNECTIVITY_ACTION
Im trying to set up a receiver to relaunch my applications alarms/notifications once the phone reboots.
Im getting stuck with a permission denial error:
W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.BOOT_COMPLETED flg=0x9000010 (has extras) } to com.closedbracket.trackit/.BootBroadcastReceiver requires android.permission.RECEIVE_BOOT_COMPLETED due to sender null (uid 1000)
I've looked at a lot of SO questions simillar to this but haven't found a solution yet.
This is my manifest:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="com.android.permission.RECEIVE_BOOT_COMPLETED" />
<application
....
<receiver
android:name="com.closedbracket.trackit.BootBroadcastReceiver"
android:enabled="true"
android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
And this is my BootBroadcastReceiver:
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("BootBroadcastReceiver", "Received");
}}
I am testing this with the Android Stuido Emulator by doing the restart functionality. I then check the logs and see the Permission Denial line in reference to my broadcast receiver and don't see my log of the onReceive method.
Literally tried everything I could, even changing the manifest's android:enable/export values, and adding the permission inside of it. Made no difference.
If anyone has any ideas, please let me know. Thank you.
You have the permission com.android.permission.RECEIVE_BOOT_COMPLETED, but as the error message says, you are supposed to have android.permission.RECEIVE_BOOT_COMPLETED without the com. at the beginning.
You can only have one action per intent filter. That's your issue.
For some reason it falls back to the last one on the list, in your case QUICKBOOT_POWERUN.
Add 2 intent filters in the broadcast receiver, each one with 1 action only and it will successfully receive both broadcasts.
I have developed application to start app on boot, but it is working on rooted devices only some of code as below.
permission and receiver in AndroidManifest.xml file is given below.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:enabled="true" android:name="in.com.appname.StartAppAtBootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
below are the class to receive on boot.
public class StartAppAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
my question is that this code will work with non rooted devices?
if yes then what is missing because same app is working with rooted devices.
This must work without root.
Else try to use a simple tutorial like: Android AutoStart App after boot
Differences:
Check which action occurs.
Use startService instead of startActivity
Another tip: Set permissions in top of manifest, and receiver in application-part.
This should work without root.
There are a couple of things to notice:
app must be started at least once before it can receive BOOT_COMPLETED
if app is force closed, it will lose it ability to receive BOOT_COMPLETED until next launch.
Also, permission should be at the root of manifest and not inside application:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.x.y" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
.
.
.
make sure you are not running in above situations.
I want to start my application at startup in Android 4.0. To do that, I wrote some codes and these are completely the same with the #Ahmad's codes (in the answer). However, although I select my application as always, when tablet opens, it asks 'What do you prefer?' (Android's default launcher or my application). I don't want it to ask that question and it must start my application automatically.
Use the BOOT_COMPLETED Intent.
Broadcast Action: This is broadcast once, after the system has
finished booting. It can be used to perform application-specific
initialization, such as installing alarms. You must hold the
RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.
In your Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Set up a Broadcastreceiver:
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
This is how your BroadcastReceiver could look like:
public class MyBroadcastreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
startActivity(i);
}
}
How do we specify in broadcast sending application that which application can receive this broadcast, and in receiving application that which particular application has the permission to send broadcast to its broadcast receiver...
I am new to android..I read the documentation etc on internet but couldn't find the syntax to specify these permissions.
To control who is able to receive the broadcast message, you can use the method sendBroadcast:
public abstract void sendBroadcast (Intent intent, String receiverPermission)
where you precise the name of the required permission. If the receiver does not declare this permission, it will not be able to get the message. For example, the broadcast sender can do:
Intent broadcast = new Intent(this, MyBroadcastReceiver.class);
sendBroadcast(broadcast, "andro.jf.mypermission");
In the manifest of the broadcast sender, a new permission should be declared:
<!-- Declaring the special permission -->
<permission android:name="andro.jf.mypermission"
android:label="my_permission"
android:protectionLevel="dangerous"></permission>
Then, in the application that is supposed to receive this broadcast, you have to declare this permission and say that you use it. In the manifest you can add:
<!-- I use the permission ! -->
<uses-permission android:name="andro.jf.mypermission"/>
and of course, you have to declare your broadcast receiver:
<receiver android:name="MyBroadcastReceiver" android:exported="true" />
You can have a look at this post for a complete example of a custom permission and also the android developer page about this. Be carefull with the order of installation of your apps because the one that defines the permission should be installed first.
If you want to restrict who only can send intents to your broadcast receiver, do it this way:
The broadcast receiver:
<manifest ...>
<!-- Permission declaration -->
<permission android:name="my.app.PERMISSION" />
<receiver
android:name="my.app.BroadcastReceiver"
android:permission="my.app.PERMISSION"> <!-- Permission enforcement for delivering intents to this receiver -->
<intent-filter>
<action android:name="my.app.Action" />
</intent-filter>
</receiver>
...
</manifest>
The broadcast sender:
<manifest ...>
<!-- We declare we own the permission to send broadcast to the above receiver -->
<uses-permission android:name="my.app.PERMISSION" />
...
</manifest>
Sending broadcast from the sender Activity to the receiver:
Intent intent = new Intent();
intent.setAction("my.app.Action");
activity.sendBroadcast(intent);
If you declare the permission like this:
<permission android:protectionLevel="signature" android:name="my.app.PERMISSION" />
Then the sender will be able to use this permission and send broadcasts to receiver only when both the sender and the receiver apps are signed by the same developer certificate.
Declare permission
First you need declare your permission in your AndroidManifest.xml
<permission android:name="YOUR_PERMISSION_STRING" android:protectionLevel="signature"/>
<uses-permission android:name="com.codylab.photogallery.PRIVATE"/>
the android:name value is used as permission value and will used later.
Usage
There are two kinds of permission usages related to broadcast receiver:
(1) Control which application can receive your broadcast:
String PERMISSION_STRING_PRIVATE_RECEIVER = "YOU_NEED_THIS_TO_RECEIVE_THIS_BROADCAST"
sendBroadcast(intent, PERMISSION_STRING_PRIVATE_RECEIVER);
With this usage, you can control only authorized application can handle the broadcast you sent.
(2) Only handle the broadcasts have the specified permission
String PERMISSION_STRING_PRIVATE_BROADCASTER = "ONLY HANDLE BROADCASTS WITH THIS PERMISSION"
IntentFilter filter = new IntentFilter(ACTION_SAMPLE);
registerReceiver(mReceiver, filter, PERMISSION_STRING_PRIVATE_BROADCASTER, null);
With this usage, you can make sure that the broadcaster is authorized.
use an intent filter in receiver tag in manifest
<receiver
android:name="Your receiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="action"/>
<category android:name="category" />
</intent-filter>
</receiver>
To send broadcast to app
Intent intent = new Intent();
intent.setAction("use same action in receiver");
intent.addcategory("use same category in receiver");
context.sendBroadcast(intent);
After half day search and test, based on #JFL's answer, I find the sender app must add both <permission> tag and <uses-permission> tag, then the receiver can receive the broadcast with permission. Otherwise, the receiver app won't receive the broadcast.