如果应用程序处于飞行模式,则关闭对话框?

时间:2016-08-29 05:40:28

标签: android

我有一个应用程序,用户在编辑文本中输入他的详细信息并向服务器发送注册请求,在服务器成功响应后,将打开一个对话框并需要用户确认此处我想检查设备是否处于飞行模式是然后移动Dismiss对话框,否则将用户详细信息发送到服务器。

代码: -

private final BroadcastReceiver m_oAIRPLANE_MODE_CHECKER = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        otpRequest();
    }
};
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "onCreate..............");
    setContentView(R.layout.registration_screen);
    defaultConfigration();// defining default configuration
    init();// Initialize controls

    /*Registered Broadcast receiver*/
    IntentFilter m_intentFilter = new IntentFilter();// creating object of Intentfilter class user for defining permission
    m_intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");// action to check Internet connection
    getApplicationContext().registerReceiver(m_oInternetChecker, m_intentFilter);// register receiver....

    m_oAirplaneModeIntent = new IntentFilter();
    m_intentFilter.addAction("android.intent.action.AIRPLANE_MODE");
    getApplicationContext().registerReceiver(m_oAIRPLANE_MODE_CHECKER,m_oAirplaneModeIntent);
}
/*Unregistered broadcast receiver*/
@Override
public void onDestroy() {// unregister broadcast receiver ........
    super.onDestroy();
    Log.i(TAG, "onDestroy.............");
    getApplicationContext().unregisterReceiver(m_oInternetChecker);// unregistaer broadcast receiver.
    getApplicationContext().unregisterReceiver(m_oAIRPLANE_MODE_CHECKER);
}

我想在这里进行检查

String sourceString = "We will send you a verification SMS to this Number.<br/><br/> <b><font color='#2196F3'>" + s_szResponseMobileNum + "</b> <br/><br/>Please ensure that this number is active on this device.";
                        m_OTPAlertBuilder = new AlertDialog.Builder(CRegistrationScreen.this, R.style.AppCompatAlertDialogStyle);
                        m_OTPAlertBuilder.setCancelable(false);
                        m_OTPAlertBuilder.setMessage(Html.fromHtml(sourceString));
                        m_OTPAlertBuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                /*This method send request to server to generate OTP*/
                                /* condition to check whether app is in background*/
                                if (NetworkUtil.isAppIsInBackground(getApplicationContext())) {
                                            /*if app is in background then start service*/
                                    Intent i = new Intent(CRegistrationScreen.this, OtpGenrateService.class);
                                    getApplicationContext().startService(i);
                                } else {
                                            /*if app is in forground the send data to server*/
                                            /*this method genrate otp */
                                    generateOtp();
                                }

                            }
                        });

1 个答案:

答案 0 :(得分:0)

您可以使用此代码段检查设备是否处于飞行模式:

public static boolean isInAirplaneModeOn(Context context){
    return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}

此外,您可以设置广播接收器以接收ACTION_AIRPLANE_MODE_CHANGED广播。编程:

BroadcastReceiver receiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          // do your stuff here
          boolean inAirplaneMode = AirplaneModeHelper.isInAirplaneMode(context);
          Log.i("AIRPLANE", "In airplane mode: " + inAirplaneMode);
      }
};
context.registerReceiver(receiver, new IntentFilter("android.intent.action.ACTION_AIRPLANE_MODE_CHANGED"));

或使用清单:

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