【问题标题】:Dismiss Dialog if App is in Airplane mode?如果应用程序处于飞行模式,则关闭对话框?
【发布时间】:2016-08-29 13:56:40
【问题描述】:

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

代码:-

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

                            }
                        });

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以使用这个 sn-p 检查设备是否处于飞行模式:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 2021-03-15
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多