0

How can I do that? I want service is run,when only wifi is active,when change is not.

When the app opens firstly,for there is not changed wifi connection,so app doesn't work.
I hope I could tell.

onReceive(..) in BroadcastReceiver class

        public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        ConnectivityManager conMan = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();

        if (netInfo.isConnected()) {

            Intent ServiceIntent = new Intent(context, BackService.class);
            context.startService(ServiceIntent);

        } else {
            Intent ServiceIntent = new Intent(context, BackService.class);
            context.stopService(ServiceIntent);
        }
}

AndroidManifest.xml

        <receiver android:name=".MyStartServiceReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </intent-filter>
    </receiver>

WiFi connection is changed to start more than one service can be.So it's a problem.How can I When wifi connection is active not a changed,background service is running?

erginduran
  • 1,678
  • 5
  • 28
  • 51

1 Answers1

1

You can check the connectivity type of the device when the application starts. Have a look at this answer.

Hope this will help you.

Community
  • 1
  • 1
Anu
  • 1,884
  • 14
  • 30
  • didn't work.service return START_NOT_STICKY; and I don't start service in MainActivity or other Activity class.I start only in BroadcastReceiver if wifi connected.How can I do that? – erginduran Aug 22 '14 at 06:32