意图在wifi信号强度变化

时间:2013-04-01 17:20:56

标签: android android-intent android-wifi

当我的wifi信号低于20%时,我想打开我的应用 我该如何实现? 下面是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);

2 个答案:

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

答案 1 :(得分:0)

一种解决方案是编写一个定期检查信号强度的小型服务,并在超过20%级别后打开您的应用程序。请小心使用过多的电池。