【问题标题】:Which can be the best aproach to monitore Wifi signal strength on Android这可能是在 Android 上监控 Wifi 信号强度的最佳方法
【发布时间】:2017-09-17 18:14:19
【问题描述】:

我正在开发一个与网络有密切关系的应用程序。它必须与强大的 wifi 信号连接。我有一次开发应用程序的时候被用户拒绝了,因为它和网络也有很强的关系,但是wifi网络不是很好,所以用户指责应用程序,他们放弃了它。

现在我将开发一个新的应用程序,我想防止这种情况再次发生。首先,我扩展了 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);
}


}

现在我需要监控 wifi 信号强度,以便在信号变低时通知用户。 我已经有一个方法可以测量信号强度并返回一个从 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 时更改条形颜色(如红色)以通知用户。但我想知道是否有更好的方法来解决这个问题。

【问题讨论】:

    标签: android wifi android-wifi


    【解决方案1】:

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

    此外,您可以查看my blog,您可以在其中找到有关此内容的更多详细信息和其他信息

    最后,我得到了这个

    首先,为每个活动声明一个文本视图。

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

    然后是从 JobService 扩展而来的 WifiJobScheduler。在这里,我也有 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);
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 2016-01-03
      • 1970-01-01
      • 2013-10-17
      • 2011-04-14
      相关资源
      最近更新 更多