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

Android 如何提供检查wifi状态的服务?,android,android-service,Android,Android Service,当wifi开启时,我正在尝试将午餐应用程序作为我的主要活动。我发现我应该使用服务来执行它。我找到了这个代码,但不起作用 import android.app.IntentService; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.wifi.WifiManager; import android.w

当wifi开启时,我正在尝试将午餐应用程序作为我的主要活动。我发现我应该使用服务来执行它。我找到了这个代码,但不起作用

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状态和应用程序午餐(无错误) 它不是午餐威菲切克。我该怎么办

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来注册intent WifiManager.Wifi\u state\u CHANGED\u操作。在BroadcastReceiver中,可以使用code intent.getIntExtra(WifiManager.EXTRA_Wifi_state,WifiManager.Wifi_state_UNKNOWN)检索Wifi状态。

据我所知,在Google I/O 2014中,他们在操作系统级别引入了新型Wifi状态监听器。因此,在电池寿命方面,它的效率更高

我不记得是哪节课了。但它要么出现在“关键提示”中,要么出现在“安卓新功能”视频中。你可以在YouTube上查看

当我遇到这个api时,我会更新我的答案

编辑:


我找到了。它叫作业调度器。我不确定这是否是你所需要的,但值得一试。

在提问之前,你需要做一些研究。这个问题已经解决了很多次了。请注意:
has_午餐==false
可以重写为
!已将
wifi.isWifiEnabled()==false
插入
!wifi.isWifiEnabled()
。另外,
午餐
启动
有不同的含义:)请在发布任何问题之前做一些研究-亲爱的@MobileMon我的问题是在服务中使用wifichecker。我已经搜索了很多,我找到了这个代码。但似乎onHandleIntent并没有启动@我从某处复制了这段代码!我希望我启动的服务在打开wifi AdapterWiFi Manager.wifi\u STATE\u启用时启动我的应用程序此案例对您有用