client_Hale
2018-09-30 c1317cc4eb6f3fbabae40253aae92af93c669b1a
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.secondworld.univeralsdk;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * Created by Administrator on 2018/7/15 0015.
 */
 
public class BatteryUtil
{
    private static BatteryUtil s_Instance = null;
 
    public static BatteryUtil getInstance()
    {
        if (s_Instance == null)
        {
            s_Instance = new BatteryUtil();
        }
        return s_Instance;
    }
 
    private BatteryBroadCastReceiver m_BatteryBroadCastReceiver;
 
    private BatteryUtil() {}
 
    public void start(Context context)
    {
        m_BatteryBroadCastReceiver = new BatteryBroadCastReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        context.registerReceiver(m_BatteryBroadCastReceiver, filter);
    }
 
    public void stop(Context context)
    {
        context.unregisterReceiver(m_BatteryBroadCastReceiver);
        m_BatteryBroadCastReceiver = null;
    }
 
    private class BatteryBroadCastReceiver extends BroadcastReceiver
    {
        private int m_BatteryLevel;
        private int m_Status;
 
        public BatteryBroadCastReceiver()
        {
            m_BatteryLevel = 0;
        }
 
        @Override
        public void onReceive(Context context, Intent intent)
        {
            if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED))
            {
                int _batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
 
                Map<String, Object> _msg = new HashMap<>();
 
                if (_batteryLevel != m_BatteryLevel)
                {
                    try
                    {
                        _msg.put("code", CodeA2U.BatteryLevel);
                        _msg.put("level", _batteryLevel);
 
                        UniversalUtil.sendMessageToUnity(_msg);
 
                        m_BatteryLevel = _batteryLevel;
 
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
 
                int _status = intent.getIntExtra("status", 0);
 
                if (_status != m_Status)
                {
                    _msg.clear();
 
                    int _transCode = 0;
                    if (_status == BatteryManager.BATTERY_STATUS_CHARGING)
                    {
                        _transCode = 2;
                    }
                    else if (_status == BatteryManager.BATTERY_STATUS_NOT_CHARGING ||
                            _status == BatteryManager.BATTERY_STATUS_DISCHARGING)
                    {
                        _transCode = 1;
                    }
                    else if (_status == BatteryManager.BATTERY_STATUS_FULL)
                    {
                        _transCode = 3;
                    }
 
                    _msg.put("code", CodeA2U.BatteryCharging);
                    _msg.put("status", _transCode);
 
                    UniversalUtil.sendMessageToUnity(_msg);
 
                    m_Status = _status;
                }
            }
        }
    }
}