Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Airplane Mode Receiver in Manifest?

I am working on developing an Android app that will be used as a handheld controller in an industrial environment. In order to make the tablet less desirable to take home, I was going to try and programatically turn on Airplane Mode all the time but that was deprecated in 4.2, which is also the version we are using in order to have Android Open Accessory. However I was thinking that if I could register a receiver within the manifest such that it will launch my app anytime Airplane Mode is changed and then check if it is off and prompt the user to turn it back on.

I have tried this following solution, also found as the answer to this problem.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<receiver android:name=".receiver.ConnectivityReceiver">
<intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>

I have also tried this code:

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

also found as an aswer by saxos here.

Now neither of these solutions cause my app to launch when airplane mode is changed.

Is it possible to register a BroadcaseReceiver for Airplane Mode within the manifest file and have that intent launch the app?

Edit 1: I do have the BroadcastReceiver working within the java code so that I can detect this situation while my app is active. That is not what I am looking for though. Thanks.

Edit 2: This is my entire manifest file currently:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.i2c"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="17" 
    android:maxSdkVersion="17"/>
    <!-- SDK 14 = 4.0 ICS
         SDK 17 = 4.2 JB -->             

<!-- Assures software is not loaded onto unsupported screen size, i.e. phones -->
<supports-screens android:xlargeScreens="true"
    android:largeScreens="true"
    android:normalScreens="false"
    android:smallScreens="false"
    android:anyDensity="true"/>

<uses-feature android:name="android.hardware.usb.accessory"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.i2c.mainActivity"
        android:label="@string/app_name"  
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.AIRPLANE_MODE" />
        </intent-filter>
        <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
            android:resource="@xml/accessory_filter"/>
    </activity>
</application>

The USB_ACCESSORY ATTACHED and the MAIN launcher work just fine and start my app from an onCreate point. However the AIRPLANE_MODE does not seem to do anything.

And here is my BroadcastReceiver that is within the java code:

    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("I2CActivity", "onReceive of BroadcastReceiver()");
        String action = intent.getAction();
        if(ACTION_USB_PERMISSION.equals(action)) {
            Log.d("ACTION_USB_PERMISSION", action.toString());
            synchronized (this) {
                Log.d("syncronized", this.toString());
                UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                if(intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if (accessory != null) {
                        OpenAccessory(accessory);
                    }
                }
                else {
                    Log.d("mUsbReceiver", "permission denied for accessory " + accessory);
                }
            }
        }
        else if(UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {
            Log.d("ACTION_USB_ACCESSORY_ATTACHED", action.toString());
            UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
            if (accessory != null) {
                resumeAccessory();
            }
        }
        else if(UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
            Log.d("ACTION_USB_ACCESSORY_DETACHED", action.toString());
            UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
            if (accessory != null) {
                closeAccessory();
            }
        }
        else if(Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
            Log.e("Airplane Mode", "Airplane mode changed");
            airplaneMode = Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
        }
    }
};

however from my understanding this is only registered when the app is running, I want a receiver that is registered even when the app is not running.

like image 925
TronicZomB Avatar asked Dec 09 '22 17:12

TronicZomB


2 Answers

With a better understanding of Intents and XML defined receivers I was able to figure this out. This has been tested on API 17 only. Here is the code for the manifest:

<receiver android:name="com.example.i2c.AirplaneModeReceiver">
     <intent-filter>
         <action android:name="android.intent.action.AIRPLANE_MODE"/>
     </intent-filter>
</receiver>

And then create a java class file called AirplaneModeReceiver and put the following in it:

public class AirplaneModeReceiver extends BroadcastReceiver {
   public void onReceive(Context context, Intent intent) {
       Intent startActivityIntent = new Intent(context, MainActivity.class);
       startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(startActivityIntent);
   }    
}

Now this will start the activity whenever there is a change in Airplane Mode, be it on or off. I didn't pursue writing code for the check whether or not Airplane Mode was off and then starting the activity any further since switching to a home screen launcher style application instead.

like image 79
TronicZomB Avatar answered Dec 11 '22 11:12

TronicZomB


Here is the snippet for receiver

public void onReceive(Context context, Intent intent) {
        if (intent.getAction().intern() == Intent.ACTION_AIRPLANE_MODE_CHANGED) {
// Launch your app here
}
like image 41
DjHacktorReborn Avatar answered Dec 11 '22 09:12

DjHacktorReborn