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

对于那些想知道我是怎么做的人 . 我使用了Job Scheduler,因为它是一个需要 Build wifi连接的任务 .

此外,您可以查看my blog,在那里您可以找到有关此信息和额外信息的更多详细信息

最后,我得到了

7d31766ac640d0d2ab215b8a64d59dda.png

首先,声明每个活动的文本视图 .

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值