Android 如何制作监控wifi状态的后台服务
我想创建一个始终在后台服务中运行的后台服务。当手机通过WI-FI连接到互联网时,请注意我。所以请帮帮我。。我是新手。 谢谢 这是我的密码-Android 如何制作监控wifi状态的后台服务,android,android-service,android-wifi,Android,Android Service,Android Wifi,我想创建一个始终在后台服务中运行的后台服务。当手机通过WI-FI连接到互联网时,请注意我。所以请帮帮我。。我是新手。 谢谢 这是我的密码- public class MyService extends Service { private static final String TAG = "My Service Demo"; Context _context; @Override public IBinder onBind(Intent intent) { return null; }
public class MyService extends Service {
private static final String TAG = "My Service Demo";
Context _context;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
//为返回网络的ConnectionManager类创建对象
相关信息
//如果连接对象不为空
//查看设备当前是否连接到WIFI网络
}服务不应该一直运行。首先,你的问题在哪里?我的意思是,你告诉我们你想要实现什么,但不是在你不知道该做什么的地方,我懒得阅读全部代码,自己解决问题。实际上,你需要一个接收器来实现这一点。见谢谢joao2fast4u的推荐
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
//Get network info - WIFI internet access
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (info != null) {
if (info.isConnected()) {
Toast.makeText(_context,"wi fi connected", Toast.LENGTH_SHORT).show();
}
}
}
}