2013-04-01 27 views
0

我想在我的wifi信號低於20%時打開我的應用程序 我如何實現這個?下面 是WiFi信號強度的代碼關於wifi信號強度變化的意圖

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
    final WifiInfo connectionInfo = wifiManager.getConnectionInfo(); 
     int rssi = connectionInfo.getRssi(); 
     int level = WifiManager.calculateSignalLevel(rssi, 10); 
     int percentage = (int) ((level/10.0)*100); 

回答

0

一種解決方案是寫一個小服務,定期檢查信號強度,一旦20%的水平越過開啓應用程式。謹防使用類似這樣的東西過度使用電池。

+0

u能請與代碼幫助? – shantanu

+0

在ApiDemos/src/com/example/android/apis/app/LocalService.java和ApiDemos/src/com/example/android/apis/app/AlarmService.java中提供了一些很好的服務示例代碼,以及服務和一些代碼片段在http://developer.android.com/guide/components/services.html。我建議從那裏開始,然後在寫入 – levis501

+0

謝謝..之後再寫下其他問題。levis501 – shantanu

1

現在,當我運行這個什麼都不會發生

公共類MYSERVICE延伸服務{

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 
//@SuppressWarnings("null") 
public int onStartCommand(Intent intent, int flags, int startId){ 

//Context context = null; 
Toast.makeText(this, "service started", Toast.LENGTH_LONG).show(); 

startservice(); 


return Service.START_STICKY; 
} 
public void onDestroy(){ 

    super.onDestroy(); 
    Toast.makeText(this, "service stopped function", Toast.LENGTH_LONG).show(); 
} 
private void startservice() { 
    // TODO Auto-generated method stub 
    //Toast.makeText(this, "service started function", Toast.LENGTH_LONG).show(); 

       try 
       { 
        WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
        final WifiInfo connectionInfo = wifiManager.getConnectionInfo(); 
        int rssi = connectionInfo.getRssi(); 
        int level = WifiManager.calculateSignalLevel(rssi, 10); 
        int percentage = (int) ((level/10.0)*100); 
        Toast.makeText(this, percentage, Toast.LENGTH_SHORT).show(); 
        if(percentage<20){ 
         Toast.makeText(this, "logout starting", Toast.LENGTH_LONG).show(); 
         Intent uplIntent = new Intent(this,com.cyberoam.logout.class); 
         uplIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         this.startActivity(uplIntent); 
        } 

       } 
       catch (Exception e) 
       { 
        return ; 
       } 

     return ; 
} 
} 
+0

百分比檢查必須定期執行,而不是一次。另外,你可能想開始一個新的問題。 – levis501

+0

我做了一個while循環來檢查百分比,我做了百分比<90來檢查我的代碼bt新的活動沒有開始。 – shantanu

相關問題