5

My target Android is 4.1.2. I created an simple android service which will show Toast on boot. But this application should not have any GUI. I was success running this service only from an activity which show GUI on start.

public class MyServices extends Service {
private MediaRecorder recorder = null;
@Override
public IBinder onBind(Intent intent) {

    return null;

}
public int onStartCommand(Intent intent, int flags, int StartId)
{

    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();

}
}
Swadesh
  • 651
  • 2
  • 8
  • 22

2 Answers2

4

You can start this service from RebootReceiver but As of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.

Reboot Receiver -> Android BroadcastReceiver on startup - keep running when Activity is in Background

Community
  • 1
  • 1
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • What is RebootReceiver? – Squonk Feb 16 '14 at 14:41
  • @Squonk, I have updated the answer and explained the `Reboot Receiver` now. – Ajay S Feb 16 '14 at 15:05
  • But it won't work without a MAIN / LAUNCHER `Activity`. In other words the app must have a UI so the user can start it at least once to acknowledge they know what the app is going to do. – Squonk Feb 16 '14 at 15:07
  • Afaik, this does not require any GUI to execute this broadcast receiver. This receiver respond when you restart the device not on the clicking the launcher icon. – Ajay S Feb 16 '14 at 15:10
  • 1
    No - this is a security feature to prevent malware. You cannot start a service or automatically register a `BroadcastReceiver` without user interaction with Android v4. The OP wants to create an app without a UI and start a `Service` - it can't be done. Sorry, yes I did downvote because your answer won't work and is misleading. – Squonk Feb 16 '14 at 15:18
  • OP want to start the service anywhere without the activity/GUI. So Afaik this will work because I recently used the Android support v4 library and it worked. You should give a try once. Even OP did not say in the question that app do not any launcher icon. OP simply said to start the service without the activity – Ajay S Feb 16 '14 at 15:29
  • The OP explicitly says **"But this application should not have any GUI."** - Also, please read point 1.4 of http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html – Squonk Feb 16 '14 at 15:40
  • 1
    I have read @Squonk, thanks for letting me know. I have updated the answer. – Ajay S Feb 16 '14 at 15:51
0

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, MyServices.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>

After this is done, your application (Application class) will run along with services, but no Activities, 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.

Mahmoud
  • 353
  • 1
  • 3
  • 14
  • 1
    This won't work unless the app has a MAIN/LAUNCHER `Activity` and the user has launched it. – Squonk Feb 16 '14 at 14:50