First of all I'm using api 23 and not android N so android.net.conn.CONNECTIVITY_CHANGE should still work for me but it doesn't.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="enis.example.com.connectivitytest">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name="com.connectivitytest.ConnectionChangeReceiver"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
</manifest>
ConnectionChangeReceiver
package com.connectivitytest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;
public class ConnectionChangeReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent )
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_MOBILE );
if ( activeNetInfo != null )
{
Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_LONG ).show();
Log.v("Active Network Type : ", activeNetInfo.getTypeName());
}
if( mobNetInfo != null )
{
Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_LONG ).show();
} Log.v("Mobile Network Type : ", activeNetInfo.getTypeName());
}
}
there is no toast message whatsoever so I added the log message just to be clear but that didn't appear in the logcat either. I even tried the following code: https://gist.github.com/mjohnsullivan/1fec89187b1274dc256e but it's all the same, no error but nothing happens, no toast message nor log message
I mean the whole point of having a receiver is to do things in the background, and now I run an activity in the foreground ?
The user needs to run your activity once, to move your app out of the so-called "stopped state" that your app is placed into after being installed. Thereafter, your receiver will work as you intend, until:
In that latter case, your app is returned to the stopped state, and your receiver will no longer work, until the user launches your activity again.
what if I don't need anything else except for the the tasks that should be ran when there is a network available ?
Most likely, there are aspects of your app's behavior that the user will want to configure. And, for that, the user will need a user interface.
What do I write in the activity to begin with ?
If nothing else, if you intend to ship in the Play Store, you will need your privacy policy.