Ideally, we want our app to be the first one to get launched after every reboot, it is a system app so can we set it as default through code?
best you can do is BOOT_COMPLETED handling
manifest
<receiver
android:name="custom.package.BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Java side
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// your code
}
}
}
but I'm afraid that with this approach you can never be sure if it will be called at first. And I know one "kind" of apps that will be called/opened earlier that any app will receive above broadast - devices Launcher
Related
I have written an Android application for a large store for the purposes of production inspection. I have a few Android tablets with the app installed on each that will be provided to the store.
As the only purpose of this tablet is to run a single app, is there a way to force this app to load upon switching on the tablet, and only allow this app to be used? The app does require an active 4G connection so it still needs to use a few of the core functions of Android as well as actually running the app.
I don't know much about this so apologies for naivety in advance, but could someone please shed some light on this for me?
First, you need the permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Also, in yourAndroidManifest.xml, define your service and listen for the BOOT_COMPLETED action:
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartMyServiceAtBootReceiver"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.
public class StartMyActivityeAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MyActivity.class);
context.startService(serviceIntent);
}
}
}
I want to run an application automatically when the phone reboot. I have added these lines to my code:
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, MyActivity.class);
context.startService(serviceIntent);
}
}
And I have added these lines to my Manifest :
<receiver
android:name=".BootComplete"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and it's permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>
It works properly when MyActivity class contains just some background tasks. For example when I want to turn on a LED. But when I want to play a media (ex. a video) it doesn't work. Just says service has started and doesn't play anything.
How can I fix it? Is there anything that I must add to the code?
The problem is not with your code! It's about android device.
When the device boot up, you receive the broadcast. But pay attention, when you receive that broadcast, the SD CARD is still populating and you can't access it. If that media is on SD card you cant play it.
Also, you should pay attention to something else: if the user install your app in his/her SD card, that broadcast receiver would never trigger.
So, if you want to prevent problem in some devices, in your manifest, choose internal for your install location too.
Do this change in BroadcastReceiver:
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, MyActivity.class);
// Change is here...
serviceInten.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(serviceIntent);
}
}
In the manifest:
<receiver
android:name=".BootComplete"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter android:priority="500">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
And leave permission as it is.
I created a boot receiver class:
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d("Boot receiver","Working")
}
}
My manifest file:
<receiver android:name="com.voonik.android.receivers.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
I used the adb command - am broadcast -a android.intent.action.BOOT_COMPLETED
Still i cant be sure whether it is working.How can i test it?
I think you have missed the #Override for on receive.
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Boot receiver","Working")
}
in my manifest i have;
<receiver
android:name=".BootUpReceiver"
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>
and;
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
in BootUpReceiver.java i have;
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, Main.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
You should just be able to see your Log in the logcat, try using System.out.println("BOOT RECEIVED");
instead of Log
Keep in mind that packages have different states (Major changes came from Android 3.1).
If the user is not opening the app by himself (at least once) than your package will not receive this intent.
Otherwise it will be really easy to run some malicious code without user interaction.
As you can imagine for security reasons.
There you can find more information: Trying to start a service on boot on Android
Unless you will place your APK in /system/apps folder and will sign it with system certificate.
P.S.: There is some flag that you can pass with the intent, might be interesting for you FLAG_INCLUDE_STOPPED_PACKAGES
Is it possible to show a notification in android wear after the system has been restarted. I can see Weather notification comes up automatically after the android wear is restarted.
I would like to get my app's notification to come up the same way? Is there a bootup receiver?
Is this possible? if so how?
Thanks!
The mechanism is the same as on standard Android devices. Please follow these steps:
Create your MyBootCompletedReceiver in Java code:
public class MyBootCompletedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// do your things - show notification for example
}
}
}
Add permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Register a receiver in your manifest:
<receiver android:name=".MyBootCompletedReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Is there any callback / receiver / anything that gets called once when my application is installed?
For this, you have to make one application apart from your main application that can keep tracking about the install or uninstalled application from your device.
For this you need to register the Receiver.
<receiver android:name=".AppStatusReceiver" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
</intent-filter>
</receiver>
AppStatusReceiver.java
public class AppStatusReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Intent: " + intent.getAction());
}
}
Note : If you are looking this to be manage from your main application callback then it is no possible.
There is no way for an app to run code when it was installed.
For other apps, you could create a BroadcastReceiver for ACTION_PACKAGE_ADDED, but as the docs explain, this won't work for the app that was newly installed:
Note that the newly installed package does not receive this broadcast.