android - 如何提供检查wifi状态的服务?

标签 android android-service

我正在尝试制作一个应用程序,以便在 wifi 打开时为我的主要 Activity 提供午餐。我发现我应该使用服务来执行它。我找到了这段代码,但不起作用。

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.widget.Toast;

public class MyServer extends IntentService {
    public Boolean has_launched = false;
    public MyServer() {
        super("MyServer");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(MyServer.this, "service started", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        wificheck();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return android.app.Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(MyServer.this, "service stoped!", Toast.LENGTH_SHORT).show();
    }

    public void wificheck() {
        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (wifi.isWifiEnabled() && has_launched == false) {
            has_launched = true;
            //startActivity(new Intent(MyServer.this, MainActivity.class));
            Toast.makeText(MyServer.this, "Wifi is on!", Toast.LENGTH_SHORT).show();
        }
        else if (wifi.isWifiEnabled() == false) {
            has_launched = false;
        }
    }
}

我有权正常读取 wifi 状态和应用午餐(没有错误) 它不会午餐 wificheck。我该怎么办?

最佳答案

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidWifiStateChangedDetect extends Activity {

TextView WifiState;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      WifiState = (TextView)findViewById(R.id.wifistate);

      this.registerReceiver(this.WifiStateChangedReceiver,
              new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
  }

  private BroadcastReceiver WifiStateChangedReceiver
  = new BroadcastReceiver(){

 @Override
 public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub

  int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE ,
    WifiManager.WIFI_STATE_UNKNOWN);

  switch(extraWifiState){
  case WifiManager.WIFI_STATE_DISABLED:
   WifiState.setText("WIFI STATE DISABLED");
   break;
  case WifiManager.WIFI_STATE_DISABLING:
   WifiState.setText("WIFI STATE DISABLING");
   break;
  case WifiManager.WIFI_STATE_ENABLED:
   WifiState.setText("WIFI STATE ENABLED");
   break;
  case WifiManager.WIFI_STATE_ENABLING:
   WifiState.setText("WIFI STATE ENABLING");
   break;
  case WifiManager.WIFI_STATE_UNKNOWN:
   WifiState.setText("WIFI STATE UNKNOWN");
   break;
  }

 }};
}

要检测 Android Wifi 开/关(启用/禁用)状态,我们可以实现 BroadcastReceiver 以注册 WifiManager.WIFI_STATE_CHANGED_ACTION Intent 。在 BroadcastReceiver 中,可以使用代码 intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN) 检索 Wifi 状态

关于android - 如何提供检查wifi状态的服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24827470/

相关文章:

java.lang.NullPointerException : Attempt to invoke virtual method on a null object reference Retrofit

android - 访问服务的变量

Android:滚动到 View 底部

android - 适用于手机的 Android 操作系统是否与 Android Things 共享相同的代码库?

android - 我应该在android服务的onDestroy方法中调用onCreate方法吗?

Android 应用程序不启动服务

java - IntentService onCreate() 已调用但 onHandleIntent() 未调用

android - 无法启动服务 Intent : not found

安卓 Intent 服务

java - 底部导航栏在 fragment 内部不起作用