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.
Related
In the AndroidManifest file, I want to capture the BOOT_COMPLETED event when the user re-boots their device. I am adding this permission:
"uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"
I have seen two "intent-filters" used by others on Stackoverflow:
"Intent.ACTION_BOOT_COMPLETED" and
"android.intent.action.BOOT_COMPLETED"
What is the preferred action string here? Please advise and explain.
Intent.ACTION_BOOT_COMPLETED == android.intent.action.BOOT_COMPLETED
They're both the same, because if you look into what the value of Intent.ACTION_BOOT_COMPLETED is, you'll see that it's android.intent.action.BOOT_COMPLETED.
Typically in the Manifest, you'll use android.intent.action.BOOT_COMPLETED due to Intent.ACTION_BOOT_COMPLETED being Java code rather than xml.
But in your code, you can use Intent.ACTION_BOOT_COMPLETED as an alternative due to it being much easier to remember.
Here is a complete solution:
Set the permission in the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
You need a receiver to run when your system restarts so something like this:
public class StartMyActivityAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// everything here executes after system restart
}
}
}
Include this receiver in your manifest like below:
<receiver
android:name=".service.StartMyActivityAtBootReceiver"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
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'm trying to implement a broadcast receiver that catch the boot complete event.
I put the permission in the manifet
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
I put the intent filter after the receiver tag in the manifest (the class file is in the receivers package)
<receiver android:name=".receivers.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT" />
</intent-filter>
</receiver>
and finally I declared the receiver class. The class should load some data from the database and set an alarm. However to check if it works I've put a Toast but it's not displayed and a vibra.
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent callingIntent) {
Vibrator vibrator=(Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(5000);
Toast.makeText(context, "BOOT RECEIVED", Toast.LENGTH_LONG).show();
}
}
Anyone knows why please?
All just installed applications gets into the stopped state (the actual file is /data/system/packages-stopped.xml)
Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state. See this link: android 3.1 launch control.
Intent with action android.intent.action.BOOT_COMPLETED has a FLAG_EXCLUDE_STOPPED_PACKAGES extra flag. It means that all stopped applications will not receive the BOOT_COMPLETED events.
To get your application out of the stopped state, start it manually just after installation. Then you can do a reboot and will see the expected Toast.
I would like to launch my app when my tablet starts, so that the main activity of my app is the first thing that the user see when they start the tablet.
I've read about LauncherActivity but I don't understand how to use it.
Can anyone help me with suggestions, links or tutorials for this?
Is LauncherActivity the best way or are there alternatives?
These lines of code may be helpful for you...
Step 1: Set the permission in AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Step 2: Add this intent filter in receiver
<receiver android:name=".BootReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Step 3: Now you can start your application's first activity from onReceive method of Receiver class
public class BootReceiver 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);
}
}
If you want to start the app when the tablets starts, you need to listen to the BOOT_COMPLETED action and react to it. BOOT_COMPLETED is a Broadcast Action that is broadcast once, after the system has finished booting. You can listen to this action by creating a BroadcastReceiver that then starts your launch Activity when it receives an intent with the BOOT_COMPLETED action.
Add this permission to your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Create a Custom BroadcastReceiver in your project:
public class MyBroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
Then modify your manifest file by adding the BroadCastReceiver to the Manifest:
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Answer by #vishesh chandra is correct. But on some device doesn't work because app was installed on external storage by default. Please ensure that you specify
android:installLocation="internalOnly"
otherwise you will not receive any Boot Complete actions if the app is installed in the SD card. Add this into application tag in manifest.xml file and it will work.
Usage:
<application
android:name=".Data.ApplicationData"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#mipmap/ic_launcher"
android:installLocation="internalOnly"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<!--activities, services...-->
</application>
I would like to add one point in this question which I was facing for couple of days. I tried all the answers but those were not working for me. If you are using android version 5.1 please change these settings.
If you are using android version 5.1 then you have to dis-select (Restrict to launch) from app settings.
settings> app > your app > Restrict to launch (dis-select)
please see picture.
After trying BootReceiver code, you will see that your app will not start after boot. Android restricts starting activities from the background.
https://developer.android.com/guide/components/activities/background-starts
The working way to start activity after boot is to give SYSTEM_ALERT_WINDOW permission to the app.
In the AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
In the MainActivity, add this request permission code. This will open a settings window. User must enable the permission for your app.
// To start app after boot
private void startSystemAlertWindowPermission(){
try{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(! Settings.canDrawOverlays(this)) {
Log.i(TAG, "[startSystemAlertWindowPermission] requesting system alert window permission.");
startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:"+getPackageName())));
}
}
}catch (Exception e){
Log.e(TAG, "[startSystemAlertWindowPermission] error:", e);
}
}
If the user gives permission, your MainActivity will start after boot.
If your app does not open an activity, but just opens a background service, this permission is not needed.
I have implement a feature that will update the database when needed.
(ex. change one of the column value of the database to B)
If system crash or reboot or something that is out of order,
is there any thing I can know then I can handle to recover the database back?
(ex. recover one of the column value of the database back to A)
Thanks for help.
<uses-permission android:name=”android.permission.RECEIVE_BOOT_COMPLETED” />
Define BroadcastReceiver which does the actions you want.
You will need in your manifest sth like:
<receiver android:name=”.MyReceiver”>
<intent-filter>
<action android:name=”android.intent.action.BOOT_COMPLETED” />
</intent-filter>
</receiver>
A crash, by definition, can't be caught.
That's what makes a crash a crash.
When device completes, it Broadcasts a intent and by registering to this broadcast u can get the call:
public class YourReceiverName extends BroadcastReceiver{
#Override
public void onReceive(final Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
//Do your task here....
}
}
}
add permission to manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and register this receiver in manifest file:
<receiver android:name="Your receiver name">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and for Database, it is upto you how you handle it and what your app demands.