Those Pits Needing Attention in Android Development

  • 2021-07-03 00:51:10
  • OfStack

This is a problem found when I was looking at Zhihu. I felt very interesting, so I recorded the pit I encountered.

1. Andorid L theme colorPrimary cannot use the color value with alpha, otherwise an exception will be thrown, and whether alpha is equal to 0 or 255 will be directly judged, and others will be abnormal


@Override
protected void onApplyThemeResource(Resources.Theme theme, int resid,
boolean first) {
if (mParent == null) {
super.onApplyThemeResource(theme, resid, first);
} else {
try {
theme.setTo(mParent.getTheme());
} catch (Exception e) {
// Empty
}
theme.applyStyle(resid, false);
}

// Get the primary color and update the TaskDescription for this activity
if (theme != null) {
TypedArray a = theme.obtainStyledAttributes(com.android.internal.R.styleable.Theme);
int colorPrimary = a.getColor(com.android.internal.R.styleable.Theme_colorPrimary, 0);
a.recycle();
if (colorPrimary != 0) {
ActivityManager.TaskDescription v = new ActivityManager.TaskDescription(null, null,
colorPrimary);
setTaskDescription(v);
}
}
}

/**
* Creates the TaskDescription to the specified values.
*
* @param label A label and description of the current state of this task.
* @param icon An icon that represents the current state of this task.
* @param colorPrimary A color to override the theme's primary color. This color must be opaque.
*/
public TaskDescription(String label, Bitmap icon, int colorPrimary) {
if ((colorPrimary != 0) && (Color.alpha(colorPrimary) != 255)) {
throw new RuntimeException("A TaskDescription's primary color should be opaque");
}

mLabel = label;
mIcon = icon;
mColorPrimary = colorPrimary;
}

2. android 5.0 screen, due to over-drawing, turns off hardware acceleration, especially after using webview, which may have a high probability.

3. Huawei mobile phones are subject to KILL1 series problems

Users can set whether an application is protected in the background. According to Huawei's functional instructions, it is understood that if it is not protected, the program will not keep running after locking the screen, that is, the process may be KILL

After the new application is installed, Huawei will give an option, whether to keep it or not. There is a problem with this default option. Some applications are not allowed by default, while others are allowed by default.

On the problem of high power consumption KILL.

On the problem that the network is cut off after screen locking. Even if the lock screen is protected, the network or SOCKET may be actively cut off.

Huawei has given its own BASTET system solution, which is not specifically developed.
4. The same color value is the same in the whole world. If the obtained colorDrawable value is changed, all other places used will change, which can be avoided by mutable. In fact, this can't be counted as a pit, but my code didn't look carefully.

5. Huawei p8 mobile phone, if service and ui are not in the same process, BroadcastReciver, which monitors the network in service, will not receive the broadcast of network connection, but can receive the broadcast of disconnection. This should also be Huawei's own optimization, but both connection and disconnection in ui can receive the broadcast.

6: Android updated webview kernel after 4.4. Before 5.0, cookie set by other domains can be read by unused domains in webview, but since 5.0, the system default value has been changed to false. This will lead to the previous use of the old method can not be obtained. (In fact, in my opinion, it is really not safe to read cookie across domains.)


  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
    }

The above is the whole content of this paper, hoping to help everyone's study.


Related articles: