逻辑
- 当您要阻止应用时,您必须制作并启动一项服务,
- 在“服务”中,您必须检查应用程序的程序包名称,以便可以决定要运行哪个应用程序以及哪个应用程序显示密码/密码活动
现在的代码示例 :
要启动服务,请使用以下代码,
startService(new Intent(this, SaveMyAppsService.class));
现在,在您的服务内部,检查像这样的包裹,
public class SaveMyAppsService extends android.app.Service
{
String CURRENT_PACKAGE_NAME = {your this app packagename};String lastAppPN = "";boolean noDelay = false;public static SaveMyAppsService instance;@Overridepublic IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub scheduleMethod(); CURRENT_PACKAGE_NAME = getApplicationContext().getPackageName(); Log.e("Current PN", "" + CURRENT_PACKAGE_NAME); instance = this; return START_STICKY;}private void scheduleMethod() { // TODO Auto-generated method stub ScheduledExecutorService scheduler = Executors .newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after every 100ms if(30 minutes spent){ stop(); }else{ checkRunningApps();} } }, 0, 100, TimeUnit.MILLISECONDS);}public void checkRunningApps() { ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo ar = RunningTask.get(0); String activityonTop = ar.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activityif (activityOnTop.contains("whatsapp") // you can make this check even better || activityOnTop.contains(CURRENT_PACKAGE_NAME)) { // Show Password Activity } else { // DO nothing } }public static void stop() { if (instance != null) { instance.stopSelf(); }}}



