android: use Intent.ACTION_BOOT_COMPLETED or ...? - android

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>

Related

How to get Android.Telephony.PhoneState when screen is off?

I have a question pretty similar to this one, but it was asked 6 years ago I thought that maybe something has chnaged.
Basically I want no notify user when signal is lost. I've implemented class that inherits from PhoneStateListener and OnServiceStateChanged method in it. Also I have a service that runs in background and listens to changes. Everything works fine as long, as screen is on.
But apparently Android do not invoke OnServiceStateChanged when screen is off. Is there any way to get Android.Telephony.PhoneState directly from service or maybe some other workaround?
My code so far is below:
PhoneStateListener
Service
Try to create BroadcastReciever Class
https://developer.android.com/guide/components/broadcasts
and change the AndroidManifest.xml file, I think you required following permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
After creating Broadcast Receiver class insert following code snippet in AndroidManifest.xml
<receiver android:name="//BroadcastReceiver Class Name" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE">
</action>
</intent-filter>
</receiver>
AndroidManifest.xml file, I think you required following permission
<uses-permission
android:name="android.permission.MODIFY_PHONE_STATE"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.CALL_PHONE" />
creating Broadcast Receiver class in AndroidManifest.xml
<receiver android:name=".services.Blocker">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Blocker.java
public class Blocker extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
/* write your code here */
}
}

Is rooted device required to start app on boot?

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.

Android Receivers not doing as expected

Well honestly, they aren't doing anything at all. Let me start by saying that I know that Android reworked receivers in 3.1, specifically boot control. I know that they made it so that ACTION_BOOT_COMPLETED cannot be used unless the application has been previously launched by the user. However, people have been successful in using them in current application, yet I am never hitting my receivers for my BOOT_COMPLETED or my SHUTDOWN.
Quick Edit - Please look at the bottom of this post for corrected Shutdown Receiver, I have gotten it to work and am now just stuck in my efforts to get BOOT_COMPLETED to work.
My manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smashingboxes.speedblock"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<!-- PERMISSIONS -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<!-- RECEIVERS -->
<receiver android:name=".BootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".ShutdownReceiver" >
<intent-filter>
<action android:name="android.intent.action.SHUTDOWN" />
</intent-filter>
</receiver>
Now my implemented receiver classes are fairly straight forward:
BOOT_COMPLETED Receiver (the one that isn't working)
public class BootReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context c, Intent i) {
Intent starterIntent = new Intent(c, LaunchActivity.class);
// Start the activity (by utilizing the passed context)
starterIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.getApplicationContext().startService(starterIntent);
}
}
I have tried different things based on what I have seen as far as solutions, such as altering my launching activity to include
/* May need this, as of 3.1 we can't call BOOT_COMPLETED until the app has been run successfully */
Intent intent = new Intent("com.smashingboxes.speedblock.intent");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
this.sendBroadcast(intent);
or including this in my boot receiver intent-filter in my manifest
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
Nothing seems to work. When Logs are inserted into my receiver methods they are never hit. Apparently people are still using these two receivers fairly regularly, which is why I am having trouble understanding why neither of them work. Have I missed something with my registration or something?
--EDIT--
I have solved the problem with my shutdown receiver. First, I foolishly forgot the ACTION_ portion of the tag. Secondly, HTC has separate shutdown methods, in my case I needed to add an intent-filter to my Receiver request:
<receiver android:name=".ShutdownReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
Now my Shutdown Receiver works, still no luck on the Boot Completed Receiver though.
I found the answer and I think that it is actually important to note, as it seems like an issue others will run into at some point. My issue was that I was launching a "Launcher Activity" called just that, LauncherActivity. Basically, it acted as my gateway to start up services, receivers, and such when the application was launched. It is a very simple Activity class:
public class LaunchActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/* May need this, as of 3.1 we can't call BOOT_COMPLETED until the app has been run successfully */
Intent intent = new Intent("com.smashingboxes.speedapp.intent");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
this.sendBroadcast(intent);
}
#Override
protected void onStart(){
super.onStart();
Intent serviceIntent = new Intent(this, MainService.class);
startService(serviceIntent);
// NOTE: It is VERY, VERY important that we DO NOT finish this activity.
// If we do, our BootReceiver will no longer receive its broadcast and
// we won't auto-start on boot
//finish();
}
}
Note that I was calling finish() on the LaunchActivity. The problem with this is that it seemed to tell Android that the application was in the stopped state, meaning that it won't allow the reception of the BOOT_COMPLETED broadcast, even though I had a number of services running in the background. The LauncherActivity MUST stay active throughout the entire life-cycle or the BOOT_COMPLETED receiver becomes useless. Something that I haven't really seen mentioned before, but again something I think is worth noting.

Restore Alarm manager after phone reboot

i am building a small widget for learning purpose, it simply has an configuration activity where i set the update interval. it works normally and i can create multiple instance of it.
but when i reboot the phone the alarm manager stops, and the widget won't update.
after some search and google'ng i learned that i have to add a BOOT COMPLETE receiver
but after several attempts i failed to implement so any one has an i idea about how to add that or any good source code example on widgets.
To do something at boot you simply do following.
First in the manifest, this is added under application tag:
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="packagename.ACTION"/>
<action android:name="packagename.ACTION2"/>
</intent-filter>
</receiver>
<receiver android:name="BootSetter" >
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In order for this to work you need to add permission to receive the Broadcast in the manifest with following line:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Then you have a class BootSetter:
public class BootSetter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Do your stuff
}
}
There is a similar post, though not completly the same here. It's about running an alarm every day at noon.
I think you are setting alarm manager in class other then AppWidgetProvider extended class(widget class) .Better you should set an alarmmanager in OnUpdate method AppWidgetProvider extended class (widget class)then there will be no need of setting the alarm again after boot.

Is there any event/intent to let me know when system crash or reboot?

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.

Categories

Resources