when android device onopen, run application

138 views Asked by At

i has android package update application. Sometimes we publish new android version or bug fixing packages. Then users download package and loads package. Also we publish force update and packages was downloaded and loaded. It has to communicate with server using broadcast and service.

It works properly if application runs once before. But we need some information about devices to report something. If user does not run application, Service will not work. So app does not work properly.

For this, We need our application runs as soon as android device open. So service will be active. And app works properly.

Update packages comes to our company from different companies. They should change package to start my application as soon as packages loaded?

1

There are 1 answers

4
Raymond On

If you're asking if you can start any services as soon as it has been downloaded, then it's impossible. How can you expect your code to run when the application is never started?

Edit:

All you can do to start the app after the device is booted is setting a receiver with an intent filter in your manifest.

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

    <receiver android:enabled="true" android:name=".BootUpReceiver"
        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>
</application>

You'll also need a BroadcastReceiver, so add this class to your source.

public class BootUpReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MainActivity.class);  //MyActivity can be anything which you want to start on bootup...
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }

}

I do not know if this will also work with updating the device, but this is all that is possible. This will popup the App when the device is booted.

I also wonder, is it really required to do this as soon as the device is updated? Couldn't you just check the Android version once the app is booted. Then save this version to the sharedPreferences. Next time you run compare the current version to the one that is stored. If it's not the same, then update you packages. This could be an alternative solution.