4

num> 50の場合、機内モードをオフにしたいので、このコードを実装しました(Androidの機内モードの切り替えから)が、実行すると、力が近づきます。誰かがここで助けてくれますか?

                if(num>50){
                    // read the airplane mode setting
                    boolean isEnabled = Settings.System.getInt(
                          getContentResolver(), 
                          Settings.System.AIRPLANE_MODE_ON, 0) == 1;

                    // toggle airplane mode
                    Settings.System.putInt(
                          getContentResolver(),
                          Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

                    // Post an intent to reload
                    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
                    intent.putExtra("state", !isEnabled);
                    sendBroadcast(intent);



                }

わかりました、予感を実装しましたが、ifステートメントを変更したいと思います:

if num>=50 and airplane mode=on toggle it off 
if  airplane mode=off and num<50 toggle it on

誰かが私が新しいコードを書くのを手伝ってくれる?(私は初心者です)

4

3 に答える 3

8

あなたはおそらくあなたの:にWRITE_SETTINGパーミッションを追加しませんでしたAndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

また、次のコードにも注意してください。

 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
 intent.putExtra("state", !isEnabled);
 sendBroadcast(intent);

上のドキュメントによると、動作するはずではありませんACTION_AIRPLANE_MODE_CHANGED

これは、システムによってのみ送信できる保護されたインテントです。

現在、このブロードキャストはシステム権限なしで送信できますが、Androidの将来のリリースで変更される可能性があります。

于 2011-08-15T15:00:49.540 に答える
3

Androidマニフェストで機内モードを切り替えるための権限を設定してください。

こちらをご覧くださいAndroidで機内モードを切り替えます

于 2011-08-15T14:56:34.653 に答える
1

次のコードを参照してください。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");

    BroadcastReceiver receiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
                Log.d("AirplaneMode", "Service state changed");
                Toast.makeText(getApplicationContext(), "Service state changed", Toast.LENGTH_LONG).show();
                boolean isEnabled = isAirplaneModeOn(context);
            /*   setSettings(context, isEnabled?1:0);
                Intent intent_mode = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
               intent_mode.putExtra("state", !isEnabled);
                context.sendBroadcast(intent_mode);*/

                if(isEnabled==true)
                { setSettings(context, isEnabled?1:0);
                    Toast.makeText(getApplicationContext(), "Flight mode on", Toast.LENGTH_LONG).show();
                    Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
                    Intent newIntent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
                    newIntent.putExtra("state", false);
                    sendBroadcast(newIntent);
                }
                else
                { setSettings(context, isEnabled?1:0);
                    Toast.makeText(getApplicationContext(), "Flight mode off", Toast.LENGTH_LONG).show();
                }

          }

        @SuppressLint("NewApi")
        private void setSettings(Context context, int value) {
            // TODO Auto-generated method stub

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
                Settings.System.putInt(
                          context.getContentResolver(),
                          Settings.System.AIRPLANE_MODE_ON, value);
            } else {
                Settings.Global.putInt(
                          context.getContentResolver(),
                          Settings.Global.AIRPLANE_MODE_ON, value);
            }       

        }

        @SuppressLint("NewApi")
        public boolean isAirplaneModeOn(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return Settings.System.getInt(context.getContentResolver(), 
                    Settings.System.AIRPLANE_MODE_ON, 0) != 0;          
        } else {
            return Settings.Global.getInt(context.getContentResolver(), 
                    Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
        }       
    }
    };

    registerReceiver(receiver, intentFilter);


}

//permissions needed:

////

于 2013-12-31T06:07:24.913 に答える