4

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);
     }
}
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Moses Kirathe
  • 322
  • 1
  • 3
  • 14

9 Answers9

9

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.

Abhishek Luthra
  • 2,575
  • 1
  • 18
  • 34
  • Thanks, @Abhishek for your answer. previously I was trying to use from manifest only and it took so much of my time. – amit pandya Mar 28 '19 at 14:06
2

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!

Moses Kirathe
  • 322
  • 1
  • 3
  • 14
1

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
        }
    }
Avinash Roy
  • 953
  • 1
  • 8
  • 25
1

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.

Avinash Roy
  • 953
  • 1
  • 8
  • 25
1

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)
alchemist
  • 37
  • 3
1

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.

  • Worked for me. However, the broadcast receiver no longer outlives the application. As a result, I'm unable to receive broadcasts while the app is closed. – Caleb Koch Feb 17 '23 at 21:38
0

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>
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42
0

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.

chacham15
  • 13,719
  • 26
  • 104
  • 207
-1

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.

Raja Mali
  • 15
  • 5
  • 2
    What do you mean by "above rules"? – Yunnosch Apr 07 '23 at 07:36
  • 2
    What do you mean by "Make sure that is is helpful"? – Yunnosch Apr 07 '23 at 07:42
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 11 '23 at 12:49
  • Asked question in everything is correct but it is till not working so Go to the app setting and clear the permision then my problem is resolved so you can try it. – Raja Mali Apr 12 '23 at 10:44