我正在尝试创建一项服务,当它检测到一个不在白名单上的新应用程序已启动时,它会打开一个活动或某种窗口。通过这种方式,我想防止人们访问像浏览器这样的应用程序,他们无法通过我的自定义启动器访问,但使用应用程序,他们可以从启动器访问(例如,应用程序中应该允许他们使用的链接)。
如果不应该启动的应用程序本身是我的代码(我的自定义启动器),我的代码工作正常,但如果有另一个应用程序不在白名单上并且我启动它,它不会做任何事情。
public class AppLockerService extends Service {
public AppLockerService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final List whitelist = Arrays.asList(R.array.WhitelistedApps);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
try {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
String openedApp = (String) appProcess.pkgList[0];
if (whitelist.contains(openedApp)) {
Intent launchIntent = new Intent(AppLockerService.this, ForbiddenApp.class);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launchIntent);
}
}
} catch (Exception e) {
}
}
}
}, 0, 1000);
return START_STICKY;
}
@Override
public void onCreate() {
// The service is being created
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Intent intent = new Intent(this, AppLockerService.class);
startService(intent);
}
}
我猜,这个错误与 Intent 不正确有关,但我不知道它是怎么回事。我还尝试打开一个完全其他的应用程序而不是forbiddenapp.class,但它仍然只有在坏应用程序是该服务所属的应用程序时才有效。
提前非常感谢!
PS.:也欢迎完全不同的方法!