日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android监听WIFI信号,这可能是Android上monitore Wifi信号强度的最佳方法

發布時間:2023/12/15 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android监听WIFI信号,这可能是Android上monitore Wifi信号强度的最佳方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

對于那些想知道我是怎么做的人 . 我使用了Job Scheduler,因為它是一個需要 Build wifi連接的任務 .

此外,您可以查看my blog,在那里您可以找到有關此信息和額外信息的更多詳細信息

最后,我得到了

首先,聲明每個活動的文本視圖 .

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"

/>

然后我創建了一個具有JobCheduler的Class WfiJob,它需要NETWORK_TYPE_ANY,并且它每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,它是一個接口,它將向活動廣播一條消息,以便顯示textview .

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

}

}

總結

以上是生活随笔為你收集整理的Android监听WIFI信号,这可能是Android上monitore Wifi信号强度的最佳方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。