我想创建一个始终在后台服务中运行的后台服务。当移动通过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;
}
@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");
//为返回网络的ConnectivityManager类创建对象 相关信息
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) {
//查看设备当前是否已连接到WIFI网络
if (info.isConnected()) {
Toast.makeText(_context,"wi fi connected", Toast.LENGTH_SHORT).show();
}
}
}
}
}