0

Hi i have developed an service application using activity and that works great in 2.2 and 2.3 android version and it starts on boot and runs my app for once in 30 mins and sending location to server but in 4.0 the app is not running on services on boot can anybody say me why?

my code:

BroadcastReceiver.java:

public class autostart extends BroadcastReceiver {
    public void onReceive(Context arg0, Intent arg1) {
        if ("android.intent.action.BOOT_COMPLETED".equals(arg1.getAction())) {
            Intent intent = new Intent(arg0, gps_back_process.class);
            arg0.startService(intent);
            Log.i("Autostart", "started");
        }
    }
}

service.java:

public class gps_back_process extends Service
{
    private static final String TAG = "MyService";
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.d("Testing", "Service got created");
        Toast.makeText(this, "gps_back_process,onCreate();", Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }


    @Override
    public void onStart(Intent intent, int startid)
    {
        Intent intents = new Intent(getBaseContext(),MainActivity.class);
        intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intents);
        Toast.makeText(this, "gps_back_process.onCreate();", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
    }
}

Manifest:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <uses-permission android:name="android.permission.BROADCAST_STICKY" />

    <receiver android:name=".autostart" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
<activity android:name=".MainActivity" >
        </activity>

        <service
            android:name=".gps_back_process"
            android:enabled="true" />
    </application>

thank you.

3 Answers3

1

Once the user runs the app for the first time (and does not force stop it), everything behaves as before — a reboot will cause BOOT_COMPLETED broadcasts to be received and so on. However, if the user installs the app, until and unless they run the app manually, no broadcasts will be received. And if the user force-stops the app, until and unless they run the app manually, no broadcasts will be received.

Check this for more detail.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

You should add add android.permission.RECEIVE_BOOT_COMPLETED,

If you don't have this permission your app won't receive the boot completed intent.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
0

This happens because from Android 3.1+ you Service will not run on Boot unless you start(launch) your Application atleast once after installation. So, when you install your Application and restart the device prior of launching the Applications MainActivity your BroadCastReceiver won't be fired. For that you have to launch your MainActivity once and then restart the device. That works!

For reference you can check my question here.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • can you say me how to run my service after installing it to device my service implements a Activity? –  May 03 '13 at 06:57
  • If you have an Activity then you can start your Service in Activities `onCreate()` but above 3.1+ its not possible to start Service on installation of Application. – Lalit Poptani May 03 '13 at 06:58
  • okay thank you, servicing onCreate() method will work below 3.1 ?? –  May 03 '13 at 07:01
  • yes If you want to start Service below 3.1 on boot of device it will work fine :-) – Lalit Poptani May 03 '13 at 07:02
  • is there any solution to run it on 3.1 and above? –  May 03 '13 at 07:04
  • you have to start the Application atleast once. And you don't want your Application icon to be shown in Application Drawer then you can remove Application icon by some code stuff. – Lalit Poptani May 03 '13 at 07:05