飞行模式接收机在清单? [英] Airplane Mode Receiver in Manifest?

查看:185
本文介绍了飞行模式接收机在清单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序将被用作在工业环境下的手持控制器。为了使平板电脑不太理想的带回家,我要去尝试和编程所有的时间打开飞行模式,但是那是在4.2 pcated德$ P $,这也是我们正在使用才能有Android的版本打开附件。但是我在想,如果我可以在清单内等注册一个接收器,将推出我的应用程序随时随地飞行模式被改变,然后检查是否处于关闭状态,并提示用户重新打开它。

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.

我曾尝试这种解决方案之后,还发现作为答案,<一个href=\"http://stackoverflow.com/questions/2294971/intent-action-for-network-events-in-android-sdk\">this问题。

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>

我也试过这code:

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>

还发现由saxos一个aswer <一个href=\"http://stackoverflow.com/questions/4319212/how-can-one-detect-airplane-mode-on-android\">here.

现在这些方法都导致飞行模式改变了我的应用程序推出。

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

是否有可能在manifest文件中注册飞行模式一BroadcaseReceiver,并有意向推出的应用程序?

编辑1:我确实有广播接收器在java code范围内工作,这样我可以检测到这种情况,而我的应用程序被激活。这不是我所期待的,但。谢谢你。

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.

编辑2:这是我的整个清单文件目前:

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>

USB_ACCESSORY 发射工作得很好,从的onCreate 点。然而, AIRPLANE_MODE 似乎并没有做任何事情。

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.

和这里是我的BroadcastReceiver这是java code内:

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.

推荐答案

通过更好地理解意图和XML定义接收我能想出解决办法的。这一问题已经在只有17 API测试。这里是code代表清单:

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>

然后创建一个名为Java类文件的 AirplaneModeReceiver 的,并把下面的吧:

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);
   }    
}

现在这将启动每当有飞行模式的改变的活动,无论是打开或关闭。我没有追求写作code的检查飞行模式是否被关闭,然后开始活动的任何进一步的,因为切换到主屏幕启动风格的应用程序来代替。

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.

这篇关于飞行模式接收机在清单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆