Android -- launching app when system boot - android

hey all,
I have a app needed to be launched when system boot,
Registering a BroadcastReceiver to receive RECEIVE_BOOT_COMPLETED Intent is a solution I have known,
but I want to know how does the desktop app auto run from boot?
I also want to know any other means available so that I can choose a suitable one for my scenario.
any replies will be welcome.

Write this code in manifest file...
receiver android:name=".AfterBoot"
intent-filter
action android:name="android.intent.action.BOOT_COMPLETED"
intent-filter
receiver
uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"
-- AfterBoot.java file.......
public class AfterBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// call your app launcher activity here ....
}
}

Related

How to make an activity start without clicking notification even when the app is closed?

I have an app where users can call each other through an SDK. Everything is working fine. I just need to be able to make calls like fabebook and whatsapp even when the app is closed. It should be able to start a particular activity even when the app is completely closed. I've followed some stackoverflow questions and I tried using the notification receiver but Its not working.
NotificationReceiver:
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent intentNotification = new Intent();
intentNotification.setAction("com.start.app");
context.sendBroadcast(intentNotification);
}
}
Manifest:
<receiver
android:name=".Notifications.NotificationReceiver"
android:enabled="true"
android:exported="false"></receiver>
What exactly you looking for ..
This can be done by creating a broadcast receiver class and send a broadcast when message received and on your broadcast receivers onReceive method call the specific activity you wish to open.
Dont forget to add the reciever in your manifest
Not tested but i think it will work..

Listen for user changed event (multi user device)

I've an app that starts itself if the phone is booted. A user told me his phone is used by two people, one of them is using my app and one not.
So I need some event to listen to when the user is switched, so that I can start my apps service if the correct user is using the phone. Anything I can use for that?
Edit
I'm listening to the boot event with a broadcast receiver registered in the manifest, so I know what this is. But I could not find anything suitable for switching users on a device
You need to look for something called BroadcastReciever in android. They are used to capture events such as camera click, phone booting up, screen unlocked etc... These events have a callback called onReceive where you can implement your login.
It's quite easy and you can Google it.
In your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
In your application element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In MyBroadcastReceiver.java:
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}

Google Awareness Api events while app isn't running

I would like either a BroadcastReceiver or IntentService (depending on how long my eventual processing takes) to start when a Google Awareness API "fence" fires. For example, perhaps I want to know how many times I activate a set of beacon fences over the course of the day (assuming I keep my phone with me). All the examples I've found show registering broadcast receivers in code, but my understanding is that I would need to register a broadcast receiver in the manifest in order for the OS to send the broadcast to it if my app isn't running. What's more, the intent ID appears to be a custom one, so I would guess I'd have to register it with the OS at least once via code?
I'm guessing I'm going to have to create one or more test apps to figure this out by trial and error, but would sure appreciate hearing from anyone who has tried this and would like to share your results!
It is just enough if you specify BroadCastReceiver in your Manifest file.
Its not a must that you need to register it in the code even after declaring the Manifest <receiver> entry. Just think about how the platform is able to handle Activities you register it only in the Manifest file(if not we get ActivityNotFoundException) the same way Broadcasts can also be register only in the Manifest file.
You need to declare the receiver like:
<receiver android:name=".MyFenceReceiver" >
<intent-filter>
<action android:name="android.intent.action.FENCE_RECEIVER_ACTION" />
</intent-filter>
</receiver>
Extend the BroadcastReceiver class.
public class MyFenceReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
FenceState fenceState = FenceState.extract(intent);
if (TextUtils.equals(fenceState.getFenceKey(), "geofence")) {
switch(fenceState.getCurrentState()) {
case FenceState.TRUE:
break;
case FenceState.FALSE:
break;
case FenceState.UNKNOWN:
break;
}
}
}
}
More info in https://developer.android.com/guide/topics/manifest/receiver-element.html

Start Service on boot but not entire Android app

I am working with Android.
I have an app I am working on uses an Activity to setup specific user input values that are then used by a service to provide alerts based on those values. Doing the research I determined how I could get the app to start up when the phone boots, however, what I really want is to have the service start but not have the app load to the screen. Currently the entire app loads to the screen when I turn on the device and then I have to exit out of it.
I have downloaded similar programs that have interfaces for settings but otherwise run in the background. How is that done?
First you have to create a receiver:
public class BootCompletedReceiver extends BroadcastReceiver {
final static String TAG = "BootCompletedReceiver";
#Override
public void onReceive(Context context, Intent arg1) {
Log.w(TAG, "starting service...");
context.startService(new Intent(context, YourService.class));
}
}
Then add permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and register intent receiver:
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
After this is done, your application (Application class) will run along with services, but no Activities.
Ah, and don't put your application on SD card (APP2SD or something like that), because it has to reside in the main memory to be available right after the boot is completed.

onBoot Complete as user preference

Is there a way that trough the application, I can subscribe and unsubscribe for the "ACTION_BOOT_COMPLETED" message?
If so, how can I do this?
Any kind of pointer will help me.
Thanks in advance,
Regards,
Vinay
Check out my answer to this question. And these links.
http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/
Trying to start a service on boot on Android
Android: how to start a service at boot based on user-settings?
The best way to do this is use PackageManager.setComponentEnabledSetting(): http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting(android.content.ComponentName, int, int)
So simply decide whether you want your receiving to be enabled or not by default by setting android:enabled in the manifest. Then use this API to explicitly enable or disable the component at run time as desired. If the component is disabled, at boot it will not be available, and thus not receive the broadcast.
As a matter of fact, there is:
Your boot receiver:
public class BootstrapReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context cntxt, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_BOOT_COMPLETED.equals(action)) {
// do your thing
}
}
}
Add a receiver and a permission for receiving boot events:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
....
<receiver android:name=".BootstrapReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Via the application (Use this if you want to programatically add/remove boot receiver and not via AndroidManifest.xml. But remember, you will still have to declare the boot permission in your AndroidManifest.xml)
Context ctx = getContext();
BootstrapReceiver booter = new BootstrapReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
ctx.registerReceiver(booter, filter);
Unregister:
ctx.unregisterReceiver(booter);

Categories

Resources