package com.tejia.lijin.app.service;
|
|
import android.app.Service;
|
import android.content.BroadcastReceiver;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.IntentFilter;
|
import android.net.ConnectivityManager;
|
import android.net.NetworkInfo;
|
import android.os.IBinder;
|
|
import com.wpc.library.util.NetUtils;
|
import com.tejia.lijin.app.ShoppingApplication;
|
|
/**
|
* 网络状态监听,注册京东
|
*/
|
public class NetworkStateService extends Service {
|
|
private static final String tag = "tag";
|
private ConnectivityManager connectivityManager;
|
private NetworkInfo info;
|
|
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
@Override
|
public void onReceive(Context context, Intent intent) {
|
if (NetUtils.isNetConnected(ShoppingApplication.application) && !ShoppingApplication.application.isJDInit) {
|
ShoppingApplication.application.initJd(ShoppingApplication.application);
|
unregisterReceiver(mReceiver);
|
stopSelf();
|
}
|
// String action = intent.getAction();
|
// if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
// Log.d(tag, "网络状态已经改变");
|
// connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
// info = connectivityManager.getActiveNetworkInfo();
|
// if (info != null && info.isAvailable()) {
|
// String name = info.getTypeName();
|
// Log.d(tag, "当前网络名称:" + name);
|
// //doSomething()
|
// } else {
|
// Log.d(tag, "没有可用网络");
|
// //doSomething()
|
// }
|
// }
|
}
|
};
|
|
@Override
|
public IBinder onBind(Intent intent) {
|
return null;
|
}
|
|
@Override
|
public void onCreate() {
|
super.onCreate();
|
IntentFilter mFilter = new IntentFilter();
|
mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
registerReceiver(mReceiver, mFilter);
|
}
|
|
@Override
|
public void onDestroy() {
|
super.onDestroy();
|
try {
|
if (mReceiver != null) {
|
unregisterReceiver(mReceiver);
|
}
|
} catch (Exception e) {
|
}
|
}
|
|
@Override
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
return super.onStartCommand(intent, flags, startId);
|
}
|
|
}
|