admin
2021-07-06 abce02c7a61820f5d580f87364d542e817be429c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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);
    }
 
}