Skip to content

Instantly share code, notes, and snippets.

@iflove
Last active July 12, 2022 09:13
Show Gist options
  • Save iflove/a6bc1ee5c611358126d01b89ebac837e to your computer and use it in GitHub Desktop.
Save iflove/a6bc1ee5c611358126d01b89ebac837e to your computer and use it in GitHub Desktop.
@iflove
Copy link
Author

iflove commented Mar 28, 2021

加载dex

val dexClassLoader = DexClassLoader(
                "/sdcard/KHello.apk:/sdcard/KHello1.dex", //需要加载的文件列表,文件可以是包含了 classes.dex 的 JAR/APK/ZIP,也可以直接使用 classes.dex 文件,多个文件用 “:” 分割
                /*externalCacheDir?.absolutePath*/null, //optimizedDirectory: 存放优化后的 dex,可以为空 Android 8.0后强制null
                null, //存放需要加载的 native 库的目录
                classLoader     //父 ClassLoader
            )


val dexClassLoader =
                PathClassLoader("/sdcard/KHello.apk:/sdcard/KHello1.dex", classLoader) //PathClassLoader 传入的optimizedDirectory是 null

@iflove
Copy link
Author

iflove commented Apr 26, 2021

pm(Package Manager)

判断安装包已安装并可用

public static boolean isAppInstalledAndEnabled(Context context, String packageName) {
    PackageInfo packageInfo;
    ApplicationInfo applicationInfo = null;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
    } catch (PackageManager.NameNotFoundException e) {
        packageInfo = null;
        e.printStackTrace();
    }
    return packageInfo != null && applicationInfo != null && applicationInfo.enabled;
}

@iflove
Copy link
Author

iflove commented Apr 29, 2021

GridLayout

layout = new GridLayout(getActivity());
layout.setPrinter(null); //GridLayout日志开关

@iflove
Copy link
Author

iflove commented Apr 29, 2021

网络相关

HttpURLConnection

记得释放:getInputStream

ssl

javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x69002d40: Failure in SSL library, usually a protocol error
    error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x67024d74:0x00000000)
	

https单向认证问题
android 4.4没有启用TLSv1.1 和 TLSv1.2 传输层安全协议
https://developer.android.com/reference/javax/net/ssl/SSLSocket

@iflove
Copy link
Author

iflove commented May 7, 2021

屏幕适配

Configuration config = getResources().getConfiguration();
int smallestScreenWidth = config.smallestScreenWidthDp;
Log.i("SW","smallest width : "+ smallestScreenWidth);

sw = 屏幕宽度 / (dpi/160)

{1.0 dualscreenflag=DISABLE ?mcc?mnc [zh_CN_#Hans] ldltr sw800dp w800dp h1208dp 160dpi xlrg port -touch qwerty/v/h -nav/h s.4}
/**
	 * 得到屏幕的物理尺寸,由于该尺寸是在出厂时,厂商写死的,所以仅供参考
	 * 计算方法:获取到屏幕的分辨率:point.x和point.y,再取出屏幕的DPI(每英寸的像素数量),
	 * 计算长和宽有多少英寸,即:point.x / dm.xdpi,point.y / dm.ydpi,屏幕的长和宽算出来了,
	 * 再用勾股定理,计算出斜角边的长度,即屏幕尺寸。
	 * @param context
	 * @return
	 */
	public static double getPhysicsScreenSize(Context context){
		WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Point point = new Point();
		manager.getDefaultDisplay().getRealSize(point);
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		int densityDpi = dm.densityDpi;//得到屏幕的密度值,但是该密度值只能作为参考,因为他是固定的几个密度值。
		double x = Math.pow(point.x / dm.xdpi, 2);//dm.xdpi是屏幕x方向的真实密度值,比上面的densityDpi真实。
		double y = Math.pow(point.y / dm.ydpi, 2);//dm.xdpi是屏幕y方向的真实密度值,比上面的densityDpi真实。
		double screenInches = Math.sqrt(x + y);
		return screenInches;
	}

@iflove
Copy link
Author

iflove commented Jun 27, 2021

调试相关

程序是否调试
if (!Debug.isDebuggerConnected()) {//判断当前应用有没有被调试
//超时操作
}

@iflove
Copy link
Author

iflove commented Feb 14, 2022

Application hook

 Application get() {
        try {
            @SuppressLint("PrivateApi") final Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
            final Method method = activityThreadClass.getMethod("currentApplication");
            return (Application) method.invoke(null, (Object[]) null);
        } catch (final ClassNotFoundException | NoSuchMethodException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
            // handle exception
        }
        return null;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment