0

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

future engineer
  • 109
  • 1
  • 8
  • https://stackoverflow.com/a/17181263/115145 – CommonsWare Feb 18 '17 at 23:23
  • I've read that somewhere but I couldn't completely understand it. 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 ? what if I don't need anything else except for the the tasks that should be ran when there is a network available ? What do I write in the activity to begin with ? and how does it work, does the receiver listens on networks available and then runs the activity ? I'm sorry but I'm very confused. – future engineer Feb 18 '17 at 23:43

1 Answers1

1

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:

  • the user uninstalls your app, or
  • the user goes into Settings, finds your app in the list, and clicks the "Force Stop" button

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Developed preload app with no default activity.. Registered network change receiver in the manifest. The app is not able to receive CONNECTIVITY_CHANGE action post reboot. I am asking here because you have mentioned that it requires an activity to kick start the receiver. – shakunthalaMK Jul 18 '18 at 06:26
  • @shakunthalaMK: "Developed preload app with no default activity" -- then talk to the device manufacturer that is preloading your app. – CommonsWare Jul 18 '18 at 11:34