2017-04-20 50 views
0

我正在开发一个与网络有很强关系的应用程序。它必须连接一个强大的无线信号。当我开发一个应用程序时,我得到了一次应用程序,因为它与网络有着很强的关系,但是由于无线网络没有很好地工作,所以用户指责应用程序并将它删除。哪个可以是monitore Wifi信号强度的最佳方式Android

现在我将开发一个新的应用程序,我想阻止再次发生这种情况。首先,我扩展BroadcastReceiver,以便在连接丢失时通知应用程序。

public class ConnectivityReceiver extends BroadcastReceiver { 
public static ConnectivityReceiverListener connectivityReceiverListener = null; 
@Override 
public void onReceive(Context context, Intent intent) { 
    boolean isConnected = AppUtilities.getWifiConnectionStatus(context); 

    if(connectivityReceiverListener != null){ 
     connectivityReceiverListener.showMessageStatus(isConnected); 
    } 
} 

public interface ConnectivityReceiverListener { 

    void showMessageStatus(boolean status); 
} 


} 

现在我需要监测无线信号强度,以在信号变低时通知用户。 我已经有了一个方法来测量信号强度,并返回一个从0到4的整数,表示从低到高的信号强度。

public static int getWifiStrengh(Context context){ 
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
    int numberOfLevels = 5; 
    WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
    int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels); 
    return level; 
} 

现在,我想知道什么是最好的方法来持续监测信号强度。

我在考虑使用作业调度程序来持续监视信号强度,并在信号强度小于2时更改栏颜色(如红色)以通知用户。但我想知道是否有更好的方法来解决这个问题。

回答

0

对于那些想知道我是如何做到的。我使用Job Scheduler,因为它是需要建立无线连接的任务。

此外,您还可以检查my blog在那里你会发现这个和额外的信息

在年底更多的细节,我得到这个 enter image description here

首先,声明文字上的每个活动视图。

<TextView 
     android:id="@+id/messageLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/wifitoSlow" 
     android:textColor="@color/magnitude0" 
     android:background="@color/magnitude7" 
     android:gravity="center" 
     android:visibility="gone" 
     /> 

然后,我创建一个类WfiJob谁拥有谁需要NETWORK_TYPE_ANY的JobCheduler,它确实是每5秒。

public class WifiJob { 

public void createWifiJob(int jobNumber, Context context){ 
    //We are defining a jobObject that will have a jobNumber and a serviceName that will run only if a network connection exits 
    JobScheduler jobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE); 
    jobScheduler.schedule(new JobInfo.Builder(jobNumber, new ComponentName(context.getApplicationContext(), WifiJobScheduler.class)) 
      .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) 
      .setRequiresDeviceIdle(false) 
      .setPeriodic(5000).build()); 
} 
} 

然后我的WifiJobScheduler从JobService扩展。在这里,我也有WifiStrenghtListener,这是一个接口,将向活动广播消息,以便可以显示文本视图。

public class WifiJobScheduler extends JobService{ 


private static final String TAG = "SyncService"; 
public static WifiStrenghtListener wifiStrenghtListener=null; 
//private boolean messageIsShowed = false; 
//The onStartJob is performed in the main thread, if you start asynchronous processing in this method, return true otherwise false. 
@Override 
public boolean onStartJob(JobParameters params) { 
    Log.i(TAG, "on start job: " + params.getJobId()); 
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
    if(AppUtilities.isInteractive(pm)){ //if the device is active 
     int wifiStrenght = WifiUtilities.getWifiStrengh(getApplicationContext()); 
     Log.i(TAG, "wifi strengh ........... : " + wifiStrenght); 
     if(wifiStrenghtListener!=null){ 
      if(wifiStrenght<4 && !AppUtilities.messageIsShowed){ 
       wifiStrenghtListener.showSlowSignalOnTop(View.VISIBLE); 
       AppUtilities.messageIsShowed = true; 
      }else if(wifiStrenght>3 && AppUtilities.messageIsShowed){ 
       wifiStrenghtListener.showSlowSignalOnTop(View.GONE); 
       AppUtilities.messageIsShowed = false; 
      } 

     } 

    }else{ 
     cancelAllJobs(); 
     Log.i(TAG, "job canceled........"); 
    } 

    return false; // true if we're not done yet and we are going to run this on a thread 
} 

// If the job fails for some reason, return true from on the onStopJob to restart the job. 
@Override 
public boolean onStopJob(JobParameters params) { 

    return true; 
} 

public void cancelAllJobs() { 
    JobScheduler tm = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); 
    tm.cancelAll(); 
} 

public interface WifiStrenghtListener{ 

    void showSlowSignalOnTop(int visible); 
} 
} 

订阅活动的控制器。

public class WifiJobSchedulerController { 

public void setWifiJobSchedulerControllerInstance(WifiJobScheduler.WifiStrenghtListener listener){ 

    WifiJobScheduler.wifiStrenghtListener = listener; 
} 
} 

最后,您需要实现接口并订阅活动。

public class LoginActivity extends AppCompatActivity implements WifiJobScheduler.WifiStrenghtListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login);  
    //register wifistrenght status listener 
    new WifiJobSchedulerController().setWifiJobSchedulerControllerInstance(this); 
} 
    @Override 
public void showSlowSignalOnTop(int visible) { 
    TextView message = (TextView)findViewById(R.id.messageLogin); 
    message.setVisibility(visible); 
} 
}