Detect system reboot from inside a service - android

Is it possible to detect when a service is stopped or destroyed by one of its own methods? e.g.
onDestroy() (not currently working)
What I need is to detect, from inside the service, whenever the device is rebooted or turned off.

You could create a BroadcastReceiver to listen for the ACTION_SHUTDOWN and QUICKBOOT_POWEROFF intents:
<receiver android:name=".PowerOffReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
Conversely you can listen for the BOOT_COMPLETED intent for when the phone is back on:
<receiver android:name=".PowerOnReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Keep in mind the latter requires the device to be unlocked by the user first. In newer API, you can listen for LOCKED_BOOT_COMPLETED which will allow you to start your service or do whatever you need before the device is unlocked.

Related

BootReceiver not working in all android devices

i have been trying to overcome issue of Boot_complete receiver not working in certain devices.
Like Vivo device which have iManager app with auto-start manager.
Where user can toggle app from auto start on device boot.
What should i use along with below as intent-filter to restart my service after device reboot.
Earlier thought of using Battery_Change receiver but it won't work from manifest, as i has to be runtime receiver register.
Any suggestion would be really help-full.
Below is what i have used as intent-filter for my app. In most devices its working as expected. But not in all.
<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>
There is one thing my team and I discovered when facing a similar issue.
You can monitor the usb state like so:
<receiver
android:name=".MyReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_STATE" />
</intent-filter>
</receiver>
And if memory serves me right, this will send a broadcast before the regular BOOT_COMPLETED action telling you there is or isn't anything USB connected.
Some manufacturers use their own version of BOOT_COMPLETED as you can read here but the USB_STATE is an alternative, based on the things you want to do. Do note you can get multiple broadcasts using this method!
Alternatively you could look into using an AccessibilityService or the JobService from the firebase sdk. More details here.

Is it possible to know when user started using its new android device?

I am working on an app which will be pre-installed in the android device and i want to trigger it in background when end user actually start using its smart phone from the very first time. So is there any broadcast fired for this purpose or any other possible way to do this?
I've used a BroadcastReceiver to accomplish the running something on start, and as for knowing if it's the first time, you could use a shared preference for the app to track it's status of first time or not with a boolean.
Define a BroadcastReceiver the receiver will need the following in the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name="com.androidfactorem.airwaves.BootAirWaveService"
android:enabled="true"
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>
You can see a full implementation of a BroadcastReceiver on boot in my git project https://github.com/pbirdsall/airwave

intent-filter for knowing when some app is being opened?

I would like to know which intent-filter use for listen when some app is being opened from my BroadcastReceiver.
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
</intent-filter>
Android OS doesn't allow this behavior. No broadcast will be sent out when a particular app is opened.
However, you can have a service that is constantly running in the background and in that service you can use the ActivityManager to get a list of the current open apps. Based on that info, you can start your activity. This is a work around.
There is no system broadcast sent out "when some app is being open", for privacy and security reasons.

Why my phone does not get alert from receiver when my app is not running on background?

I have an app and I want to get notifications. It works fine when my phone is running background. But when I clear memory or restart my phone it is not getting notifications.
My Receiver extends BroadcastReceiver class. And I use this on manifest:
<receiver android:name=".Main$MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.xx.UPDATE_STATUS" />
</intent-filter>
</receiver>
I want to get notifications when my app is running on foreground, background , I mean every time is it possible?

Can multiple classes receive the same Broadcast receiver intent

I've revewied the answer to How does android resolve multiple entries in the AndroidManifest for the same broadcast receiver with different permissions and intent-filters? but my question is slightly different.
If I have 2 classes in my app, 1 to start a service and and another to stop the service based on different system events, can they both receive the same broadcast.
I want to either start or stop my service depending on whether the user switches on or off airplane mode. Will onReceive() in both classes by fired with the following entries in my manifest.
<receiver android:name=".StartService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>"
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
<receiver android:name=".StopService" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
<!-- for power testing only -->
<!--<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>-->
</receiver>
I'm not sure if that is possible but an alternative method would be to catch it in the StartService receiver and then inside that check the status of your Service. If your service is already started, launch your on broadcast and catch that within your StopService?
If i remember rightly you register in a class listeners to capture those events. I.e. Connectivity Manager. So you can either have a single class running as a service that captures the change in connectivity ( at which point you can detect if you're in airplane mode) and do whatever, or register in the Activity that is currently active to listen out for those events as well. Personaly i'd go for a service running in the background in nearly all scenarios.
Edit:
Having read your question it sounds like your starting a service with one class and want to kill that service with the other class. In which case, i don't think isn't necessarily the best way of doing it. Instead in the service you've started, register to capture changes in connectivity and get the service to stop itself.

Categories

Resources