Moses Kirathe
Moses Kirathe

Reputation: 332

Why is my BroadCast Receiver not working?

I am learning about BroadCastReceiver. What I am trying to achieve with the following piece of code is, I would like to see a Toast when I switch to airplane mode, where the app is on or not. What am I not doing / Doing wrong? Please Help. Thanks

ConnectivityChangedReceiver.java class

public class ConnectivityChangedReceiver extends BroadcastReceiver {

     @Override
     public void onReceive( Context context, Intent intent ){

          Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();

     }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.kirathe.mos.c_max">
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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=".ConnectivityChangedReceiver">
        <intent-filter>
            <action android:name="android.intent.action.AIRPLANE_MODE">
            </action>
        </intent-filter>

    </receiver>

</application>

MainActivity.java`

public class MainActivity extends AppCompatActivity {

     private TextView switchStatus;
     private Switch mySwitch;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     }
}

Upvotes: 5

Views: 35118

Answers (9)

Raja Mali
Raja Mali

Reputation: 41

Follow the above rules till your app is not working then go to your app setting and clear data then allow permissions. Make sure it is helpful Thanks.

Upvotes: -1

Himanshu Tekade
Himanshu Tekade

Reputation: 81

intent- filter is not working in the receiver tag in AndroidManifest.xml file for me. So I added everything in MainActivity.java file like this

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    IntentFilter intentFilter = new IntentFilter("android.intent.action.AIRPLANE_MODE");
    broadcastBattery bb = new broadcastBattery();
    registerReceiver(bb, intentFilter);
}

Now everything is working fine.

Upvotes: 1

alchemist
alchemist

Reputation: 55

if you API level greater than 26 you should add permission to your code like that:

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
   
val _receiver = cSmsListener() //<--you BroadCastreciever class 
val intentFilter = IntentFilter()

intentFilter.addAction(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)
getApplicationContext().registerReceiver(_receiver, intentFilter);

var myPermission: Array<String> = arrayOf(Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS)
ActivityCompat.requestPermissions(this, myPermission,1)

Upvotes: 0

Abhishek Luthra
Abhishek Luthra

Reputation: 2665

From the android documentation :

https://developer.android.com/guide/components/broadcast-exceptions

As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. However, several broadcasts are currently exempted from these limitations. Apps can continue to register listeners for the following broadcasts, no matter what API level the apps target.

and

https://developer.android.com/distribute/best-practices/develop/target-sdk

Google Play will require that new apps target at least Android 8.0 (API level 26) from August 1, 2018, and that app updates target Android 8.0 from November 1, 2018.

"android.intent.action.AIRPLANE_MODE" is no longer in the list of exempted broadcasts. So, register your broadcast receiver in activity rather than in AndroidManifest.

Upvotes: 9

chacham15
chacham15

Reputation: 14251

You need to put the receiver in the bundles package:

package my.bundles.id;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class ConnectivityChangedReceiver extends BroadcastReceiver {

@Override
public void onReceive( Context context, Intent intent )
{
    Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();

}
}

You should put it in a package thats the same as the bundleId and that will let the above work. The first dot in the name field means that the Class is a member of the bundles namespace. So, since it wasnt in a package, the dot made it look in the wrong place.

Upvotes: 0

Moses Kirathe
Moses Kirathe

Reputation: 332

So I just found the solution to my problem above. I changed

<receiver android:name=".ConnectivityChangedReceiver">

to

<receiver android:name="ConnectivityChangedReceiver">

(Without the '.' at the beginning of name. Hope it helps a stranded one!

Upvotes: 2

Avinash Roy
Avinash Roy

Reputation: 973

Try it like this,try adding,android:exported="true":

//Add this permission too

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


 <receiver android:enabled="true" android:name=".ConnectivityChangedReceiver"
android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_AIRPLANE_MODE_CHANGED"/>
        </intent-filter>
    </receiver>

The Real state is determined like this:

    @Override
    public void onReceive(Context context, Intent intent) {

        boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
        if(isAirplaneModeOn){

           // handle Airplane Mode on
        } else {
           // handle Airplane Mode off
        }
    }

Upvotes: 1

Avinash Roy
Avinash Roy

Reputation: 973

The Official document of Broadcast Receivers Says:

android:exported

Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.

Upvotes: 1

Luiz Fernando Salvaterra
Luiz Fernando Salvaterra

Reputation: 4182

You have to enable your broadcast in your manifest.xml file :

<receiver android:enabled="true" android:name=". ConnectivityChangedReceiver">
    <intent-filter>
        <action android:name="android.intent.action.AIRPLANE_MODE"/>
    </intent-filter>
</receiver>

Upvotes: 0

Related Questions