BootReceiver doesen't work - android

I know this has been asked tons of times, and there are hundreds of example on internet, but i want to understand what's wrong in my code.
As the title suggest i want to execute some code while the phone turn on, specifically i want to set some Alarms for getting notifications, but that's not relevant now, my problem is that the boot receiver onReceive method is never called apparently
I have the right permissions in the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And i've also registered the receiver in the manifest
<receiver
android:name=".BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And I've already created the receiver class
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context pContext, Intent intent) {
Toast.makeText(pContext,"waiting for debugger",Toast.LENGTH_LONG).show();
android.os.Debug.waitForDebugger();
//Stuff for the alarms
}
}
Can someone explain me what i'm a failing without posting always the same examples that i see everywhere?
I want to know what's wrong in my code, not how it should be done.
PS: : I forgot to say that i need to stop the code for debugging the alarm things, but i don't think that's the problem since it doesen't even show the Toast.
UPDATE: full manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="feddycapdev.conapo.turnario" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Calendario"
android:label="#string/title_activity_calendario"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Settings_Activity"
android:label="#string/title_activity_settings_" >
</activity>
<activity
android:name=".SettingGiorno"
android:label="#string/title_activity_setting_giorno" >
</activity>
<receiver
android:name=".BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:enabled="true" android:name=".WebNotificChecker" />
<service android:enabled="true" android:name=".Sveglia" />
<service android:enabled="true" android:name=".NotificaVigilanza" />
</application>
</manifest>

I should run the activity before the receiver?
Something has to use an explicit Intent to start one of your application's components before any manifest-registered receivers will work. For 99% of Android apps, that means that the user has to start your app from the home screen. If your app is serving as a plugin to some other app, you may not need the activity — please discuss this with the developers of the app that would be hosting your plugin.
how can I set notification if the user doesn't open activity?
You wouldn't set the notification if the user does not open the activity. Your app will only run when the user lets you run. If the user chooses not to start your app, or if the user chooses to "Force Stop" your app from within Settings, your manifest-registered receiver will not receive broadcasts.

Related

On Boot Broadcast Receiver not working in miui of Xiaomi (Poco x3)

I know many similar questions have been asked before, but I couldn't make it work no matter what solution I tried.
I have a broadcast receiver code like the following.
class OnBootBroadcast : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
logD("onReceive() started -> intent action: [${intent?.action}]")
// this is only to test if on boot broadcast is working
context?.let {
val i = Intent()
i.setClass(it, MainActivity::class.java)
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK;
it.startActivity(i);
}
// tried to test by adding notification as well, didn't show
// do stuff here
}
}
My manifest file is like
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".CustomApplication"
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".view.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".broadcast.NotificationBroadcast" />
<receiver
android:name=".broadcast.OnBootBroadcast"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
tools:node="remove" />
</application>
I know Chinese custom ROMs like MIUI kill apps' background services.
To handle that, I tried the following things.
Turn on "Autostart" in Security > Manage apps
This fixed issue of work manager background service not working when app is closed by swiping. But didn't fix the broadcast issue.
Set "No restrictions" in Settings > Battery & Performance > App battery saver
I am trying to use the on boot broadcast to re-add some alarm managers for exact timed notifications.
If there is some alternative which can achieve this, that info would be appreciated too.
Thanks in advance :)
I had the exact same problem with my Mi phone. I tried many suggested solutions but it didn't work. So I got a bit carried away and added every possible BOOT_COMPLETE trigger to my intent-filter and it worked.
<receiver android:name=".receiver.BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
In the end, I didn't need to change anything in manifest file. Only android.intent.action.BOOT_COMPLETED was fine as the rest of the implicit broadcasts are not in exception list.
The main problem was that MIUI is taking some minutes to start the broadcast, which I was not noticing in the log. And when it did start, since the test code was starting an activity from background process, MIUI was killing it (according to log, starting a foreground UI is not enabled for the background process started by MIUI for boot broadcast). So after I removed the activity starting code, further logs started showing too.

service not started on BOOT COMPLETE

I have a service that I would like to start on BOOT COMPLETE
when it is started , I have a toast message displayed.
my problem is that when the device boots up , the toast is displayed and is stuck on screen, and the service is not starting correctly.
however if I am trying to start my service through an activity , the service is starting well and the toast disappears after a few seconds correctly.
my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tfl.extprotocolservice"
android:versionCode="7"
android:versionName="1.6" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<application
android:allowBackup="true"
android:icon="#drawable/launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="com.tfl.extprotocolservice.ExtProtocolBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".ExtProtocolService" >
<intent-filter>
<action android:name="com.tfl.extprotocolservice.ISetIpPort" />
</intent-filter>
<intent-filter>
<action android:name="com.tfl.extprotocolservice.IExtMessage" />
</intent-filter>
</service>
<!--
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
-->
</application>
</manifest>
my broadcast receiver:
public class ExtProtocolBroadcastReceiver extends BroadcastReceiver {
/* broadcast receiver to start on BOOT COMPLETE*/
#Override
public void onReceive(Context context, Intent intent) {
Intent StartServiceIntent=new Intent(context,ExtProtocolService.class);
context.startService(StartServiceIntent);
}
}
btw, the activity in the manifest is commented because I don't really need it , it was just to test starting the service from an activity.
If your application has no activities, your BroadcastReceiver will never get called.
When you install an application, it is installed in the "stopped state". applications in "stopped state" do not get broadcast Intents delivered to them.
In order to get your application out of "stopped state", the user must manually launch your application (at least once). In order to do this, you must offer him an Activity that he can use to start your application.
Once your application is no longer in "stopped state", Android will deliver broadcast Intents to it. That is, until the user "force stops" your application.
If the user "force stops" your application, it will go back to "stopped state" and will no longer get the broadcast Intents. Until the user manually starts your application again.
I tried with am broadcast -a android.intent.action.BOOT_COMPLETED then it restart the device.
You can try <action android:name="android.intent.action.USER_PRESENT"/>
After more research, I think it was the fastboot mode which will not broadcast BOOT_COMPLETE.
Your service is filtering actions, but your intent doesn't provide any.
Fix with this:
StartServiceIntent.setAction("com.tfl.extprotocolservice.IExtMessage");

my application not start on BootUp mobile [duplicate]

This question already has answers here:
how to start my application when ever mobile restart or turn on
(3 answers)
Closed 9 years ago.
this is my manifiest file below my application not start my application when i turn On mobile i follow this like how to start my application when ever mobile restart or turn on
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.installedapps22"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/cherry_icon" android:label="#string/app_name">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:enabled="true" android:name="com.app.reciever.BootUpReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".ListInstalledApps" > </activity>
<activity android:name=".TabsLayoutActivity" />
</application>
</manifest>
Class File
package com.example.installedapps22;
public class BootUpReciever extends BroadcastReceiver
{
#Override
public void onReceive(final Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Check this question of mine, and the answer provided - they are related, and might solve your problem:
Intent.ACTION_USER_PRESENT not received on HoneyComb or ICS (Samsung) devices
Even though the intent used is different, the security issue I describe below is similar.
The problem might be that you need to run the application once BEFORE it will respond to the BOOT_COMPLETED intent.
This is a security measure.
If you do not run the application, it will not start on boot. Give it a try.
i.e.
write app
install app
RUN APP
reboot phone to check if it works

time since factory reset android

I'm trying to log time reliable how long a device is run. I've tried my best with BOOT_COMPLETED BroadcastReceiver. Due to the fact that all receiver are removed if an app is force stopped this method is not working for me.
I was wondering if there is a way to get the uptime of an Android device since the last factory reset has been performed?
The Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".TestReceiver"
android:enabled="true"
android:exported="true"
android:label="Test Receiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
Receiver Code:
public class TestReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("bootreceiver", intent.toString());
}
}
For anyone reaching this actually looking for a answer to the title question:
You can try getting the install time for one of the system packages, for instance:
long curTime = System.currentTimeMillis();
long installTime = getPackageManager().getPackageInfo("com.android.providers.applications", 0).firstInstallTime;
long timeDiff = curTime - installTime;
I'm not pretty sure how do you want to calculate uptime from factory reset, not from last reboot. However, if you are storing some of your data and checking was it erased on boottime - it might work.
However, I think here is a good idea to use BroadcastReceiver for BOOT_COMPLETED. And it's generally false that receivers are deleted after force close. It applies only for receivers from code.
Instead, you can define your receiver in Manifest - http://developer.android.com/guide/topics/manifest/receiver-element.html
If you'll do - it will be executed each reboot, regardless of anything.
Good luck
You have a problem defining permission in your Manifest.
It should look like that:
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".TestReceiver"
android:enabled="true"
android:exported="true"
android:label="Test Receiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
You should define you app's permission in Uses block. And the permssion that you'd added to your Broadcast - it does not add you a permission to receive it, it just forces any other app, that can trigger your broadcast, to have this permission. So unuseful in your case.
Hope it helps

Android: Start Service on boot?

I'm reaaaaally new to Java, but an experienced C#-coder.
I've created a service which I can start/stop from an activity.
My question is, how do I "install" this service so it does start upon boot of my device?
I found this:
Trying to start a service on boot on Android
I've tried to implemented this like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="james.jamesspackage" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:debuggable="true">
<activity android:name=".jamessActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".MyService">
<intent-filter>
<action android:name="james.jamesspackage.MyService" />
</intent-filter>
</service>
<receiver android:name="james.jamesspackage.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</application>
</manifest>
What's wrong? Can I have an activity and a service/receiver in one manifest?
Thanks
James
How to start service on device boot(autorun app, etc.)
For first: since version Android 3.1+ you don't recieve BOOT_COMPLETE if user never started yor app at least once or user "force closed" application.
This was done to prevent malware automaticaly register service. This security hole was closed in newer versions of Android.
Solution:
Create app with activity. When user run it once app can recieve BOOT_COMPLETE broadcast message.
For second: BOOT_COMPLETE is sent before external storage is mounted. if app is installed to external storage it won't receive BOOT_COMPLETE broadcast message.
In this case there is two solution:
Install your app to internal storage
Instal another small app in internal storage. This app recieves BOOT_COMPLETE and run second app on external storage.
If your app already installed in internal storage then code below can help you understand how to start service on device boot.
In Manifest.xml
Permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Register your BOOT_COMPLETED reciever:
<receiver android:name="org.yourapp.OnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Register your service:
<service android:name="org.yourapp.YourCoolService" />
In reciever OnBoot.java:
public class OnBoot extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
// Create Intent
Intent serviceIntent = new Intent(context, YourCoolService.class);
// Start service
context.startService(serviceIntent);
}
}
For HTC you maybe need also add in Manifest this code if device don't catch RECEIVE_BOOT_COMPLETED:
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
Reciever now look like this:
<receiver android:name="org.yourapp.OnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
How to test BOOT_COMPLETED without restart emulator or real device?
It's easy. Try this:
adb -s device-or-emulator-id shell am broadcast -a android.intent.action.BOOT_COMPLETED
How to get device id? Get list of connected devices with id's:
adb devices
adb in ADT by default you can find in:
adt-installation-dir/sdk/platform-tools
Enjoy! )
Looks like the name in the receiver section is wrong. This is what my application entry in the AndroidManifest.xml looks like:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".BootListener"
android:enabled="true"
android:exported="false"
android:label="BootListener">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".UpdateService">
</service>
<activity android:name=".Info"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TravelMapperPreferences"
android:label="Settings">
</activity>
</application>
Note that the names are relative to the package in the manifest declaration. Your receiver name should be ".MyBroadcastReceiver" since the package of the manifest contains james.jamesspackage

Categories

Resources