wenmingvs / androidprocess Goto Github PK
View Code? Open in Web Editor NEW判断App位于前台或者后台的6种方法
License: Apache License 2.0
判断App位于前台或者后台的6种方法
License: Apache License 2.0
appCount甚至会出现负数
Process: com.wenming.androidprocess, PID: 19784
android.app.RemoteServiceException: Bad notification for startForeground
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2313)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8178)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
环境
compileSdkVersion 28
targetSdkVersion 28
RT
public static boolean getApplicationValue(Context context) {
return ((MyApplication) ((Service) context).getApplication()).getAppCount() > 0;
}
在5.0中方法2和方法6都可以,但是5.0之后都只会返回自己的进程信息,其他的应用信息不可查看。不知道没有人想出其他解决方案。
如何在使用方法三的情况下,让栈顶的activity做出弹窗操作?
demo无法查看到信息
public static boolean isAppForegroundRunning(final Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager == null) return false;
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
AndroidProcesses.getRunningForegroundApps(context);
AndroidProcesses.getRunningForegroundApps(context);
中的foreground==true,
而以前使用的代码返回值为false,建议发布到jcenter或者maven仓库
建议发布到jcenter或者maven仓库
建议发布到jcenter或者maven仓库
如题
建议加入到.gitignore文件中
这个方法也有问题,当我横竖屏切换 重新实例化时,生命周期会先调用 自己的onStop ,再去调用 onStart会出现误判断 appCount =0; 而正常的 activity 跳转(A -》B)是 B Activity 先 onStart() 然后 A Activity 才 onStop()
打开百度地图后,切换到后台,
打开我的demo程序调用BackgroundUtil.getLinuxCoreInfo(context, "com.baidu.BaiduMap")
其返回结果一直都是true,什么鬼?我的demo程序不是在最顶层吗?
ProcessManager.getRunningForegroundApps(context) 这个方法返回的是一个前台app列表
在这个列表中我怎么知道哪个是真的处于前台(位于手机界面最顶层)?
我这样改了下就好了。
原:
/*
if (usageStats == null || usageStats.size() == 0) {
if (HavaPermissionForTest(context) == false) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(context, "权限不够\n请打开手机设置,点击安全-高级,在有权查看使用情况的应用中,为这个App打上勾", Toast.LENGTH_SHORT).show();
}
//发现造成这里的false 是因为 usageStats.size() == 0,即空集合(实际手机有任务存在)
// boolean r = (usageStats == null);
//android.util.Log.e("Current","usageStats:null:"+r);
return false;
}*/
改后:
//改一下上面的
if (usageStats == null || usageStats.size() == 0) {
if (HavaPermissionForTest(context) == false) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
//1.我是子线程里使用Toast
Looper.prepare();
Toast.makeText(context, "权限不够\n请打开手机设置,点击安全-高级,在有权查看使用情况的应用中,为这个App打上勾", Toast.LENGTH_SHORT).show();
Looper.loop();// 进入loop中的循环,查看消息队列
//2.添加这里,
return false;
}
//3.一定得返回,不然后面会下标越界,估计返回true,一直检查的话,马上就会有真实情况
return true;
}
方法六:我在一个服务中开线程一直调用getRunningForegroundApps()方法,会导致应用调不起支付宝,这是什么原因呀?
如何在不root的情况下,获取正在运行的所有app,因为android 7.0以后androidprocess库不能用了,现在的我很是苦恼啊?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.