Android: Start Service on boot? - android

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

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.

Android PHONE_STATE not fired if phone is inactive for a night

My Android app invokes a webservice or sends SMS when a new incoming call received. I use android.intent.action.PHONE_STATE intent for this.
Everything works fine, except: the app must be restarted every morning.
It looks like if something prevents the app from receiving this broadcast if it is not used for hours, for a night.
What can cause this kind of behavior?
<application>
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>

Broadcastreceiver not working after boot

I have created an apps that will receive notifications from firebase thus hoping to start the app service after user boot their phone so that they do not need to manually start the apps again. However, the broadcast receiver seems to just not working.
AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.simplifiedcoding.firebasecloudmessaging">
<!-- Adding Internet Permission -->
<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:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
Defining Services
-->
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name=".NotificationService"/>
<receiver android:name=".Broadcast" android:exported="true" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Broadcast.java (BroadcastReceiver)
public class Broadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
Intent service = new Intent(context, NotificationService.class);
context.startService(service);
Toast.makeText(context, "Broadcast started", Toast.LENGTH_LONG).show();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have checked the followings
Permission RECEIVE_BOOT_COMPLETED declared and not within tag
receiver tag is being written correctly
Am I doing wrong or still missing something else? Do I have to call it at mainAcitivity which I doubt I should ? Any guidance are much appreciated.
Got the solution and it was a very dumb reason.
I'm using Oppo's phone to test and Oppo has its own Security Manager App where you have to manually allow specific apps to be start up automatically after boot. That's all.
I suspect most android phones has this feature as well thus bear in mind to check whether there is such app and if it does then remind the user to allow the apps in the Security Manager App before they start rebooting their phone and not able to use any service's your App intended to provide.
Hope this helps!
Try this, it worked for me.
<receiver android:enabled="true" android:name=".Broadcast"
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>

My broadcast receiver can't get BOOT_COMPLETED broadcast

I install and open it , and reboot my phone , but my receiver did't receive broadcast to start my service and without log.
My phone is Asus LF2 .
How can I start my service in device boot completed?
My Receiver
public class BootReceiver extends BroadcastReceiver {
private final String TAG = getClass().getSimpleName();
public BootReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG , "Deyu onReceive " + intent.getAction());
context.startService(new Intent(context, AlarmMessageService.class));
}
}
My manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="go.deyu.dailytodo"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".app.App"
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>
<receiver
android:name=".receiver.BootReceiver"
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>
<service
android:name=".AlarmMessageService"
android:enabled="true">
</service>
</application>
</manifest>
I find why my app in my phone can't get boot complete broadcast.
There is a Auto-start Manager setting in my Asus Phone.
When I allow my app to Auto-start , my App work fine....
Remove android:permission="android.permission.RECEIVE_BOOT_COMPLETED" from the <receiver> element.
Try below code
<receiver
android:name=".receivers.RestartReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Android 3.1 or above,if you want to deal with android.intent.action.BOOT_COMPLETED broadcastreceiver.You must pay attention to this:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
1.Make sure you have turned on your app after you installed.I think you did it.
2.Check your android mobile device settings: Settings -> Apps -> Your App -> Force Stop.If the Force Stop is turned on,please turn off it.
3.Another point you should check is android:name=".receiver.BootReceiver",be careful of the path,maybe system can not find your BootReceiver.

NFC broadcast problem

I really read 10 or 20 topics about that and unfortunately I didn't make it working. My receiver can capture broadcast but only if I send it via sendBroadcast(intent) from my app. I want it to capture broadcast from NFC adapter. F.e someone puts NFC Tag near my device and then my app should start or show in browsing menu, however that doesn't happen. Even if my app starts, and I put NFC Tag near device, it can't capture it, and in browsing menu I see other apps, which can.
My receiver:
public class SomeBroadcastReceiver extends BroadcastReceiver {
private final String TAG = "SomeBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Got intent: " + intent);
}
}
And my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nfc">
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<receiver android:enabled="true" android:name=".broadcast.SomeBroadcastReceiver">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="#xml/technologies"/>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>
</receiver>
<activity android:name=".simulator.FakeTagsActivity"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="TagViewer"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="10" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
In FakeActivity i have this lines:
Intent exampleIntent = new Intent("android.nfc.action.NDEF_DISCOVERED");
sendBroadcast(exampleIntent);
And when app reches them, my receiver captures intent, so I think that receiver is fine, but maybe I miss something in manifest? Are there special permission to capture global broadcast? Or should I start service or sth?
You can't capture those intents with a BroadcastReceiver, because only Activities can receive NFC intents. You can find more information about it in the NFC guide.
I use technique described in this answer - it provides the same effect as a broadcast receiver.
According to here (https://stackoverflow.com/a/5320694/3736955), you cant catch NFC intent by BroadcastReceiver. The only way to handle it is by ForegroundDispatch and onNewIntent() function within activity. When NFC Tag is tapped, it looks for foreground activity to handle him.

Categories

Resources