0

I created an application that consists of one static receiver:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.docd.connectivityresetter"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

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

    </application>

</manifest>

The receiver

package com.docd.connectivityresetter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public final class ConnectivityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        android.util.Log.i("received", intent.getAction());
        android.util.Log.i("received", intent.getExtras().toString());
    }

}

Installed the application.

1) Nothing received (no logs) when I trigger Airplane Mode (cell network goes down)

2) My application is not listed in "Running" tab of phone's Application settings menu (shouldn't it be listed as "Running" when static receiver is registered)?

I came across this when searching. Everything matches except it doesn't work for me. Intent action for network events in android sdk

Community
  • 1
  • 1
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99

2 Answers2

1

I had same issue, and I solved it by adding :

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<receiver android:name=".ConnectivityReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.SERVICE_STATE" />
    </intent-filter>
</receiver>

If it does not work, an other way is to add a PhoneStateListener in your application :

First, add a service class in your manifest :

<service android:name=".YourService" />

Then, creates your service with the start of phonestatelistener

@Override
public void onCreate(){
    TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
    listener = new PhoneStateListener() {
        @Override
        public void onServiceStateChanged(ServiceState serviceState){
            // Your code regarding ServiceState
        }
    };
    tm.listen(listener,PhoneStateListener.LISTEN_SERVICE_STATE);

    super.onCreate();
}

Then, start the service in the first activity

startService(new Intent(YourActivity.this, YourService.class));

A final solution, maybe not the best one, but if you still want to rely on receivers is to start the PhoneStateListener on the boot event ACTION_BOOT_COMPLETED (don't forget the RECEIVE_BOOT_COMPLETED permission in this case)

  • Adding permission READ_PHONE_STATE didn't change anything for me. – Yaroslav Mytkalyk Nov 20 '13 at 20:13
  • But what about SERVICE_STATE action in addition to CONNECTIVITY_CHANGE ? –  Nov 20 '13 at 20:14
  • Sorry, didn't notice this. Gonna try. – Yaroslav Mytkalyk Nov 20 '13 at 20:15
  • OK. Just tell me once you tried it. And for informations, what informations exactly are you trying to retrieve ? –  Nov 20 '13 at 20:15
  • it didn't work. I'm trying to retrieve the boroadcast when mobile network goes down (no service). – Yaroslav Mytkalyk Nov 20 '13 at 20:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41564/discussion-between-gaetan-and-doctoror-drive) –  Nov 20 '13 at 20:28
  • That was the problem. Until I start an Activity no receivers are registered http://stackoverflow.com/a/20128014/1366471 – Yaroslav Mytkalyk Nov 21 '13 at 17:36
  • Now the receiver works but seems like I really don't receive cell connection updates, only mobile data and other stuff. So I stick to your method. But I will accept my answer because it reflects the problem of receivers not being registered - what the question was about. – Yaroslav Mytkalyk Nov 21 '13 at 18:06
0

Finally I found this great answer when I couldn't receive BOOT_COMPLETED either.

BroadcastReceiver not receiving BOOT_COMPLETED

I would have never known...

Community
  • 1
  • 1
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99