我需要了解当前哪个应用程序在屏幕上的信息。例如,我已经按Facebook和Facebook在屏幕上,我按下主页按钮,屏幕上没有任何内容。我已经按谷歌Chrome浏览器获取谷歌浏览器位于屏幕顶部的信息。更准确地获取应用程序的包名称。看到一些实现,但它们都给了我相同的结果。
public class MyService extends Service {
private static MyService sMyService;
private static String foregroundPackageName;
public IBinder onBind(Intent intent) {
sMyService = this;
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
foregroundPackageName = componentInfo.getPackageName();
System.out.println("THIS IS THE FOREGROUND
::::::::::::::::"+foregroundPackageName);
return null;
}
public static String getForegroundPackageName() {
return foregroundPackageName;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这只是一个测试,看看我是否会获得屏幕上的活动但是所有时间都只是打印:
com.sec.android.app.launcher.activities.LauncherActivity。
答案 0 :(得分:0)
此代码可以帮助您
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
PackageManager packageManager = getApplicationContext().getPackageManager();
ApplicationInfo appInfo;
try {
appInfo = packageManager.getApplicationInfo( componentInfo.getPackageName(), 0);
} catch (final PackageManager.NameNotFoundException e) {
appInfo = null;
}
String appName = (String) (appInfo != null ? packageManager.getApplicationLabel(appInfo) : "(unknown)");
Log.e("App Name",""+appName);
答案 1 :(得分:0)
import android.app.ActivityManager;
import android.app.Service;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
public class MyBackGroundService extends Service {
private static MyBackGroundService sMyService;
private Handler handler;
@Override
public void onCreate() {
super.onCreate();
sMyService = this;
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
printCurrentTask();
handler.postDelayed(this,60);
}
},60);
}
public IBinder onBind(Intent intent) {
return null;
}
public static String getForegroundPackageName() {
return foregroundPackageName;
}
private String printCurrentTask() {
String currentApp = "NULL";
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager usm = (UsageStatsManager)this.getSystemService("usagestats");
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*1000, time);
if (appList != null && appList.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
for (UsageStats usageStats : appList) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
} else {
ActivityManager am = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
currentApp = tasks.get(0).processName;
}
Log.d("Top App ******", "Current Top Running App is: " + currentApp);
return currentApp;
}
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(null);
}
}
为使用情况统计信息添加此权限
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions"/>
即使你要求用户启用permision,如果没有启用它也无法正常工作
import android.app.AppOpsManager;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Intent backGroundService;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
backGroundService = new Intent(MainActivity.this, MyBackGroundService.class);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkIfAppUsageAccess();
}
});
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
void checkIfAppUsageAccess(){
boolean granted = false;
AppOpsManager appOps = (AppOpsManager) this
.getSystemService(Context.APP_OPS_SERVICE);
int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
android.os.Process.myUid(), getPackageName());
if (mode == AppOpsManager.MODE_DEFAULT) {
granted = (checkCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) == PackageManager.PERMISSION_GRANTED);
} else {
granted = (mode == AppOpsManager.MODE_ALLOWED);
}
if (Build.VERSION.SDK_INT >= 21 && !granted) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time);
if (stats == null || stats.isEmpty()) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
}
} else {
startService(backGroundService);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
stopService(backGroundService);
}
}
此代码适用于android lollipop以及以下版本