Pergunta

I have a receiver in my manifest.

<receiver
    android:name="com.deviceinventory.StartAppAtBootReceiver"
    android:enabled="true"
    android:exported="false"
    android:label="StartMyServiceAtBootReceiver" >
    <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

And my com.deviceinventory.StartAppAtBootReceiver onReceive() is

public void onReceive(Context context, Intent intent) {    
   if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        Intent startUpIntent = new Intent(context, StartUpActivity.class);
        startUpIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(startUpIntent);
    }

Since StartUpActivity uses internet, So I want to start this activity after boot when internet connection is available.

Currently It starts sometime before internet connection has been established.

I don't have idea how to modify the receiver in manifest and BroadCastReceiver

Foi útil?

Solução

For that you need to add one more Receiver to check Internet Connection is Enable/Disable, When its enable you can start your bootReceiver or Any Activity you want to run.

public class NetworkInfoReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, final Intent intent) {
try {
    /***
     * Here we need to check its running perfectly means this receiver
     * call when net is off/on. You should have to check
     * */
    final ConnectivityManager conMngr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    // Check if we are connected to an active data network.
    final NetworkInfo activeNetwork = conMngr.getActiveNetworkInfo();
    final boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

    if (isConnected) {
    /**
     * Start service/receiver/activity here.
     * */

    } else {
    /**
     * Stop service/receiver/activity here.
     * */
    }
} catch (final Exception e) {
}
}
}

And you add this receiver into AndroidManifest.xml file.

     <receiver
        android:name=".NetworkInfoReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

Outras dicas

You can try editing you intent filter. Now you recierver will respond when the device is booted completly. Change that to respond it when the connectivity changes...

<receiver android:name=".NetworkChangeReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>

Hope this peice of code will help you.

@Hemant you need to check in onReceive that both the conditions met to start a service.


 public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
      if(intent.equals(Intent.ACTION_BOOT_COMPLETE) && wifiManager.isWifiEnabled()){
      //start your service
       }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top