1. =======不要在子线程中更新ui,不要再子线程中调用toast,例如:
private void loadData() {
if (isRunningFindTask) {
//下面这个toast会报崩溃:
/**
* java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:209)
at android.os.Handler.<init>(Handler.java:123)
at android.widget.Toast$TN.<init>(Toast.java:349)
at android.widget.Toast.<init>(Toast.java:105)
at android.widget.Toast.makeText(Toast.java:263)
at shu.shi.com.scquan.utils.ToastUitl.initToast(ToastUitl.java:27)
at shu.shi.com.scquan.utils.ToastUitl.showShort(ToastUitl.java:41)
at shu.shi.com.shici.activity.scmain.ScTuijianFragment.loadData(ScTuijianFragment.java:121)
*/
// ToastUitl.showShort("正在刷新...");
}
}
2. ==============Android8.0,应用管理需要进行权限check,代码如下:
在activity中,调用如下代码:
PermissionCheckManager.checkInstallAppAndroid_O(getActivity());
PermissionCheckManager类的代码:
public static void checkInstallAppAndroid_O(Context activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean hasInstallPermission = isHasInstallPermissionWithO(activity);
if (!hasInstallPermission) {
startInstallPermissionSettingActivity(activity);
return;
}
}
}
@RequiresApi (api = Build.VERSION_CODES.O)
public static boolean isHasInstallPermissionWithO(Context context){
if (context == null){
return false;
}
return context.getPackageManager().canRequestPackageInstalls();
}
/**
* 开启设置安装未知来源应用权限界面
* @param context
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private static void startInstallPermissionSettingActivity(Context context) {
if (context == null){
return;
}
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
((Activity)context).startActivityForResult(intent,REQUEST_CODE_APP_INSTALL);
}