0

我已经编写了某些代码,这样我的 android 应用程序应该进入飞行模式并在一段时间内回到网络连接模式,直到我强行关闭应用程序。它使用我编写的代码,但问题是屏幕或显示器没有显示飞行符号。当我进入设置时,我可以看到代码正在正确启用和禁用飞行模式。

任何人都可以给我一个解决方案。

我的代码如下

public class Airplane_ModeActivity extends Activity implements Runnable{

    private static final int SLEEP_TIME_VALUE = 10000;
    private static Context context;
    private static ContentResolver contentResolver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  

        context =  getApplicationContext();
        contentResolver = context.getContentResolver();

        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", false);
        sendBroadcast(intent);
        Runnable runnable = new Airplane_ModeActivity();
        Thread thread = new Thread(runnable);
        thread.start();
        }

        public void run(){
        while(true)     {

            while(0==Settings.System.getInt(contentResolver, Settings.System.AIRPLANE_MODE_ON, 0) ) {

                Settings.System.putInt(contentResolver, Settings.System.AIRPLANE_MODE_ON, 1);

                try {
                    Thread.sleep(SLEEP_TIME_VALUE);

                }
                catch (InterruptedException ie) {

                }
            }
            try {
                Thread.sleep(SLEEP_TIME_VALUE);
            }
            catch (InterruptedException ie) {
            }

        while(1==Settings.System.getInt(contentResolver, Settings.System.AIRPLANE_MODE_ON, 1) ) {


            Settings.System.putInt(contentResolver, Settings.System.AIRPLANE_MODE_ON, 0);

            try {

                Thread.sleep(SLEEP_TIME_VALUE);
            }
            catch (InterruptedException ie) {

            }

        }
        try {
            Thread.sleep(SLEEP_TIME_VALUE);

        }
        catch (InterruptedException ie) {

        }
    }
        }   
    }
4

1 回答 1

0
 boolean isEnabled = Settings.System.getInt(thisActivity.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) == 1;
                        Settings.System.putInt(thisActivity.getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnabled ? 0 : 1);
 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
 intent.putExtra("state", !isEnabled);
 sendBroadcast(intent);

检查这个。,我认为这会对你有所帮助..

于 2012-04-06T10:01:24.547 回答