积跬步

Just do IT Now.


  • Home

  • Tags

  • Categories

  • Archives

  • Search

工具-生命周期管理

Posted on 2021-05-10 | In Android代码片段

监听app切换前后台

ActivityLifecycleCallbacks使用要求API 14+ (Android 4.0+)。

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
114
115
public class ForegroundCallbacks implements Application.ActivityLifecycleCallbacks {
public static final long CHECK_DELAY = 500;
public static final String TAG = ForegroundCallbacks.class.getName();
public interface Listener {
public void onBecameForeground();
public void onBecameBackground();
}
private static ForegroundCallbacks instance;
private boolean foreground = false, paused = true;
private Handler handler = new Handler();
private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
private Runnable check;
public static ForegroundCallbacks init(Application application){
if (instance == null) {
instance = new ForegroundCallbacks();
application.registerActivityLifecycleCallbacks(instance);
}
return instance;
}
public static ForegroundCallbacks get(Application application){
if (instance == null) {
init(application);
}
return instance;
}
public static ForegroundCallbacks get(Context ctx){
if (instance == null) {
Context appCtx = ctx.getApplicationContext();
if (appCtx instanceof Application) {
init((Application)appCtx);
}
throw new IllegalStateException(
"Foreground is not initialised and " +
"cannot obtain the Application object");
}
return instance;
}
public static ForegroundCallbacks get(){
if (instance == null) {
throw new IllegalStateException(
"Foreground is not initialised - invoke " +
"at least once with parameterised init/get");
}
return instance;
}
public boolean isForeground(){
return foreground;
}
public boolean isBackground(){
return !foreground;
}
public void addListener(Listener listener){
listeners.add(listener);
}
public void removeListener(Listener listener){
listeners.remove(listener);
}
@Override
public void onActivityResumed(Activity activity) {
paused = false;
boolean wasBackground = !foreground;
foreground = true;
if (check != null)
handler.removeCallbacks(check);
if (wasBackground){
L.d ("went foreground");

for (Listener l : listeners) {
try {
l.onBecameForeground();


} catch (Exception exc) {
L.d ("Listener threw exception!:"+exc.toString());
}
}
} else {
L.d ("still foreground");
}
}
@Override
public void onActivityPaused(Activity activity) {
paused = true;
if (check != null)
handler.removeCallbacks(check);
handler.postDelayed(check = new Runnable(){
@Override
public void run() {
if (foreground && paused) {
foreground = false;
L.d ("went background");
for (Listener l : listeners) {
try {
l.onBecameBackground();
} catch (Exception exc) {
L.d ("Listener threw exception!:"+exc.toString());
}
}
} else {
L.d ("still foreground");
}
}
}, CHECK_DELAY);
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
@Override
public void onActivityStarted(Activity activity) {}
@Override
public void onActivityStopped(Activity activity) {}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override
public void onActivityDestroyed(Activity activity) {}
}
Read more »

UI相关问题

Posted on 2021-04-30 | In Android

ListView相关

ListView的setOnItemClickListener无效

问题:使用listview的时候遇到了设置item监听事件的时候在没有回调onItemClick 方法的问题

Read more »

安卓问题-未归类

Posted on 2021-04-30 | In Android

功能相关问题

Camera相机相关

关于Camera横竖屏问题

Read more »

Manifest

Posted on 2021-04-30 | In Android

AndroidManifest清单文件

合并优先级(低的合并到高的)【库清单<主清单<flavor的清单】

安卓/清单文件合并优先级

Read more »

EventBus封装

Posted on 2021-04-28 | In Android代码片段

EventBus封装使用

封装

1
implementation 'org.greenrobot:eventbus:3.2.0'
Read more »

唯一设备码

Posted on 2021-02-23 | In Android

Android10(api29)

获取唯一设备码

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
public static String getDeviceId(Context context) {
String deviceId = (String) SharedPreferencesUtil.getParam(SP_NAME_SYSTEM_INFO, SP_KEY_DEVICE_ID, "");
if (TextUtils.isEmpty(deviceId)) {
deviceId = getMD5(getDeviceInfo(context));
SharedPreferencesUtil.setParam(SP_NAME_SYSTEM_INFO, SP_KEY_DEVICE_ID, deviceId);
}
return deviceId;
}

/**
* deviceID的组成为:渠道标志+识别符来源标志+hash后的终端识别符
* 渠道标志为:
* andriod(a_)
* 识别符来源标志:
* 1, wifi mac地址(wifi_);
* 2, IMEI(imei_);
* 3, 序列号(sn_);
* 4, id:随机码。若前面的都取不到时,则随机生成一个随机码,需要缓存。
*
* @param context
* @return
*/
private static String getDeviceInfo(Context context) {
StringBuilder deviceId = new StringBuilder();
// 渠道标志
deviceId.append("a_");
try {
//wifi mac地址
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String wifiMac = info.getMacAddress();
if (!TextUtils.isEmpty(wifiMac)) {
deviceId.append("wifi");
deviceId.append(wifiMac);
return deviceId.toString();
}
//IMEI(imei)
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
if (!TextUtils.isEmpty(imei)) {
deviceId.append("imei_");
deviceId.append(imei);
return deviceId.toString();
}
//AndroidID
if(!TextUtils.isEmpty(Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID))) {
return Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
}
//序列号(sn)
String sn = tm.getSimSerialNumber();
if (!TextUtils.isEmpty(sn)) {
deviceId.append("sn_");
deviceId.append(sn);
return deviceId.toString();
}
//如果上面都没有, 则生成一个id:随机码
String uuid = UUID.randomUUID().toString();
if (!TextUtils.isEmpty(uuid)) {
deviceId.append("id_");
deviceId.append(uuid);
return deviceId.toString();
}
} catch (Exception e) {
e.printStackTrace();
deviceId.append("id_").append(UUID.randomUUID().toString());
}
return deviceId.toString();
}


/**
* 获取String的MD5值
*
* @param info 字符串
* @return 该字符串的MD5值 返回值为十六进制的32位长度的字符串
*/
private static String getMD5(String info) {
try {
//获取 MessageDigest 对象,参数为 MD5 字符串,表示这是一个 MD5 算法(其他还有 SHA1 算法等):
MessageDigest md5 = MessageDigest.getInstance("MD5");
//update(byte[])方法,输入原数据
//类似StringBuilder对象的append()方法,追加模式,属于一个累计更改的过程
md5.update(info.getBytes("UTF-8"));
//digest()被调用后,MessageDigest对象就被重置,即不能连续再次调用该方法计算原数据的MD5值。可以手动调用reset()方法重置输入源。
//digest()返回值16位长度的哈希值,由byte[]承接
byte[] md5Array = md5.digest();
//byte[]通常我们会转化为十六进制的32位长度的字符串来使用,本文会介绍三种常用的转换方法
return new BigInteger(1, md5Array).toString(16);
} catch (NoSuchAlgorithmException e) {
return "";
} catch (UnsupportedEncodingException e) {
return "";
}
}
Read more »

版本相关-Android10

Posted on 2021-02-23 | In Android

Android10

Android 10版本特性

Android 10适配要点,作用域存储

Read more »

版本相关-Android11

Posted on 2021-02-23 | In Android

Android 11版本特性

Android 11 新特性,Scoped Storage又有了新花样

Android 11上强制启用了 Scoped Storage

Read more »

版本相关-Android12

Posted on 2021-02-23 | In Android

Android 12版本特性

Android12正式发布

Android的新UI–Material You(新的设计语言)

Read more »

版本相关-Android6

Posted on 2021-02-23 | In Android

升级API相关问题

升级到安卓6以上的动态权限

permission is only granted to system apps

Read more »

版本相关-Android9

Posted on 2021-02-23 | In Android

升级API相关问题

升级到 Android9.0遇到的问题

java.lang.VerifyError: Verifier rejected class d.w.b.f.a: java.lang.String

Read more »

版本相关

Posted on 2021-02-23 | In Android

链接

  • Android(6-13)适配链接

版本相关

Read more »
<1…101112…23>

265 posts
23 categories
47 tags
E-Mail CSDN
0%
© 2018 — 2025 阿炳
Powered by Hexo
|
Theme — NexT.Gemini v5.1.4