我正在尝试使用应用程序在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状态和应用午餐(没有错误) 这不是午餐。我该怎么办?
答案 0 :(得分:1)
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 ON / OFF(启用/禁用)状态,我们可以实现BroadcastReceiver以使用意图WifiManager.WIFI_STATE_CHANGED_ACTION进行注册。在BroadcastReceiver中,可以使用代码intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,WifiManager.WIFI_STATE_UNKNOWN)检索Wifi状态
答案 1 :(得分:1)
据我记忆,在Google I / O 2014中,他们在操作系统级别引入了新型的Wifi状态监听器。因此,它在电池寿命方面更有效率。
我不记得它是哪个会话。但要么是在" Key Note"或在" Android中的新功能"视频。你可以在YouTube上查看。
当我遇到这个api时,我会更新我的答案。
编辑:
我找到了它。它被称为JobScheduler。我不确定你是否只需要尝试一下。