質問

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.

役に立ちましたか?

解決 2

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.

他のヒント

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.

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top