Android Airplane模式连续切换

时间:2012-04-06 05:16:52

标签: android airplane

我编写了某些代码,以便我的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) {

        }
    }
        }   
    }

1 个答案:

答案 0 :(得分: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);

查看这个。我认为这会对你有帮助..