1

i have created a BroadcastReceiver called Autostart to be called, when the device is booted:

public class Autostart extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent arg1)
    {
        Log.i("mydebug","autostart");
        Intent intent = new Intent(context, RegisterService.class);
        context.startService(intent);

    }
}

this is the Service that is supposed to be called:

public class RegisterService extends Service {


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("mydebug","service");
        BroadcastReceiver receiver = new NetWatcher();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        registerReceiver(receiver, intentFilter);
        return super.onStartCommand(intent, flags, startId);
    }
}

and here my Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ginso.podcasts">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>
        <receiver android:name=".NetWatcher">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".Autostart">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <service android:name=".RegisterService"/>
    </application>

</manifest>

i also register the NetWatcher in my MainActivity the same way. Now when i start the app, the Netwatcher immediatly gets called and also everytime i change my wifi state. When i restart my phone, the autostart calls the service, which registers the NetWatcher, which also gets immediatly called. But if i change the wifi state now, nothing happens

Ginso
  • 757
  • 13
  • 31

1 Answers1

0

register NetWatcher in onCreate() of your service Try Changing your action to android.net.conn.CONNECTIVITY_CHANGE instead WifiManager.NETWORK_STATE_CHANGED_ACTION

Rasel
  • 5,120
  • 3
  • 31
  • 37