2017-04-20 30 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); 
} 
} 
相關問題