2012-04-06 91 views
0

我已經寫了某些代碼,以便我的android應用程序應該轉到飛行模式並在網絡上回到連接模式一段時間,直到我強行關閉應用程序爲止。 它與我寫的代碼一起工作,但問題是屏幕或顯示屏沒有顯示飛行符號。 當我進入設置時,我可以看到代碼正在啓用​​和禁用飛機模式。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) { 

     } 
    } 
     } 
    } 
+0

被它影響了你想要達到什麼樣的?除了頂部的符號。有時也會發生這種情況,無線也可以,但它不會以任何方式影響我的輸出 – 2012-04-06 07:46:53

+0

是的。當運行應用程序,如果已經去了設置,並看到我可以清楚地看到,飛機模式的複選框正在啓用和禁用..,但通知欄中的我沒有顯示手機在飛機上模式。 – 2012-04-06 08:29:27

回答

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

檢查這一個,我認爲這將幫助你..

+0

我不認爲這符合我的要求...檢查我的要求再發布您的看法哥們,因爲你只是讓我的應用程序設置爲飛行模式。但我想回到連接模式,並再次飛行模式(飛行模式)連續。 – 2012-04-06 11:18:15