android获取app父进程ID
2017-09-15 17:15
120 查看
在android.os.Process中可以直接获取当前应用的进程号:
在Process中是有获取父进程ID方法的,但是不能用:
通过反射来实现:
主要看看invoke方法:
第一个参数receiver是调用该方法的对象,就是之前创建的process对象,第二个参数是对应方法的参数,由于myPpid()没有参数就不用传了。
关于反射更详细的介绍看:
http://blog.csdn.net/liujiahan629629/article/details/18013523
int pid = android.os.Process.myPid();
在Process中是有获取父进程ID方法的,但是不能用:
通过反射来实现:
//获取对应的类 Class<Process> processClass = android.os.Process.class; //创建一个对象 Process process = null; try { process = processClass.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } int ppid = 0; try { //创建方法process中myPpid方法 Method method = processClass.getMethod("myPpid"); //调用方法,传入对象 ppid = (int) method.invoke(process); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }
主要看看invoke方法:
* @param receiver * the object on which to call this method (or null for static methods) * @param args * the arguments to the method * @return the result * * @throws NullPointerException * if {@code receiver == null} for a non-static method * @throws IllegalAccessException * if this method is not accessible (see {@link AccessibleObject}) * @throws IllegalArgumentException * if the number of arguments doesn't match the number of parameters, the receiver * is incompatible with the declaring class, or an argument could not be unboxed * or converted by a widening conversion to the corresponding parameter type * @throws InvocationTargetException * if an exception was thrown by the invoked method */ public native Object invoke(Object receiver, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException;
第一个参数receiver是调用该方法的对象,就是之前创建的process对象,第二个参数是对应方法的参数,由于myPpid()没有参数就不用传了。
关于反射更详细的介绍看:
http://blog.csdn.net/liujiahan629629/article/details/18013523
相关文章推荐
- android 获取APP的唯一标识applicationId的实例
- 【Android】Facebook 登陆APP ID获取 android hash key获取,development/ release
- 如何使用appium desktop 获取Android APP 控件的id
- Android之adb jdwp获取debug版本app的进程Id
- android通过反射根据包名和id或name获取resource ID
- android安卓APP获取手机设备信息和手机号码的代码示例
- android.view.View 中获取 android.app.Activity
- Appium自动获取 Android 设备 id 和包名等信息(python)
- android有没有获取google play上app的版本号的api
- Android获取手机型号,系统版本,App版本号等信息
- Android:资源 id 及资源 id 的动态获取
- Android获取App版本号和版本名
- Android通过文件名动态获取id
- android 通过字符串来获取R下面资源的ID 值
- Android如何在一个布局(Layout)中获取另一个布局中空间的id
- Android app接入百度地图:禁用手势、模式切换、根据经纬度获取位置信息
- Android获取设备ID、型号及其它信息
- android 安卓APP获取手机设备信息和手机号码的代码示例
- Android获取SharedPreferences失败,且App无法启动
- android 安卓APP获取手机设备信息和手机号码的代码示例