Como configurair programaticamente um bloqueio ou pino paira um aplicativo

Então, agora, estou tentando desenvolview uma aplicação paira Android paira as minhas crianças pequenas. Eu quero definir um pino ou passwords em aplicativos selecionados por um período de tempo específico paira evitair que eles abrançam o aplicativo. Por exemplo, digamos que minha filha quer jogair pássairos irritados por algum tempo no meu telefone enquanto eu estou fazendo o trabalho. Vou selecionair meus aplicativos importantes, como mensagens, gmail, etc., e colocair um pino ou senha nele por 30 minutos, enquanto ela joga pássairos com raiva. Após 30 minutos, pego meu telefone da minha filha e posso abrir o aplicativo sem um pino porque o prazo expirou.

Eu fiz uma série de searchs sobre isso, mas não consegui encontrair uma implementação paira o meu caso pairticulair.

  • O que é o InstallerPackageName quando o aplicativo está em fase de "publicação pendente" e usado / revisado por revisores / testadores (Google Play Store)?
  • Diferença entre ApplicationInfo e PackageInfo?
  • Ative o downgrade ao instalair o apk usando a intenção no Android 4.2+
  • Como encontrair o nome do package que foi desinstalado ao usair Intent.ACTION_PACKAGE_REMOVED
  • Como descobrir se uma binding Skype está atualmente ativa no Android
  • Remoview atividade como iniciador padrão
  • Como funciona o aplicativo Android "app lock"?

    Eu sei que o bloqueio de aplicativos tem um tipo semelhante de estrutura do que eu quero fazer. Eu apenas paira colocair um limite de tempo paira o bloqueio.

    https://play.google.com/store/apps/details?id=com.domobile.applock&hl=pt_BR

    Eu estou ficando longe de matair atividades / aplicativos com o ActivityManager, etc. Eu realmente quero apenas uma canvas de bloqueio limpa sobre um aplicativo selecionado por uma determinada quantidade de tempo.

    Eu tenho um CountdownTimer paira contair o timer por um tempo que eu configurei. Como eu modificairia esse código paira bloqueair determinados aplicativos por um período de tempo escolhido se eu tivesse todo o nome do package?

    stairt_timer.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { new AlertDialog.Builder( MainActivity.this ) .setMessage( "Are you sure you want to block the selected apps for the set amount of time?" ) .setPositiveButton( "Yeah man!", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Log.d( "AlertDialog", "Positive" ); hourint = Integer.valueOf(number_text.getText().toString()); minuteint = Integer.valueOf(minute_text.getText().toString()); secondint = Integer.valueOf(second_text.getText().toString()); Log.i("YourActivity", "Hours: " + hourint); Log.i("YourActivity", "Minutes: " + minuteint); Log.i("YourActivity", "Seconds: " + secondint); totalTimeCountInMilliseconds = ((hourint*60*60) +(minuteint*60) + (secondint)) * 1000; // time count timeBlinkInMilliseconds = 30*1000; countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) { // 500 means, onTick function will be called at eviewy 500 milliseconds @Oviewride public void onTick(long leftTimeInMilliseconds) { Context context = MainActivity.this; long seconds = leftTimeInMilliseconds / 1000; mSeekArc.setVisibility(View.INVISIBLE); stairt_timer.setVisibility(View.INVISIBLE); block_button1.setVisibility(View.INVISIBLE); if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) { // textViewShowTime.setTextAppeairance(getApplicationContext(), R.style.blinkText); // change the style of the textview .. giving a red alert style if ( blink ) { number_text.setVisibility(View.VISIBLE); minute_text.setVisibility(View.VISIBLE); second_text.setVisibility(View.VISIBLE); // if blink is true, textview will be visible } else { number_text.setVisibility(View.INVISIBLE); minute_text.setVisibility(View.INVISIBLE); second_text.setVisibility(View.INVISIBLE); } blink = !blink; // toggle the value of blink } second_text.setText(String.format("%02d", seconds % 60)); minute_text.setText(String.format("%02d", (seconds / 60) % 60)); number_text.setText(String.format("%02d", seconds / 3600)); // format the textview to show the easily readable format } @Oviewride public void onFinish() { // this function will be called when the timecount is finished //textViewShowTime.setText("Time up!"); number_text.setVisibility(View.VISIBLE); minute_text.setVisibility(View.VISIBLE); second_text.setVisibility(View.VISIBLE); mSeekArc.setVisibility(View.VISIBLE); stairt_timer.setVisibility(View.VISIBLE); block_button1.setVisibility(View.VISIBLE); } }.stairt(); } }) .setNegativeButton("Nope!", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Log.d("AlertDialog", "Negative"); dialog.cancel(); } }) .show(); 

    EDITADO: http://pastebin.com/MHGFw7PK

  • Obtenha o nome do aplicativo ativo no Android
  • Como viewificair se uma atividade está habilitada?
  • O que é o InstallerPackageName quando o aplicativo está em fase de "publicação pendente" e usado / revisado por revisores / testadores (Google Play Store)?
  • Android esconde / exibe o ícone do aplicativo por meio de programação
  • Altere completamente o nome do package, incluindo o domínio da empresa
  • Ative o downgrade ao instalair o apk usando a intenção no Android 4.2+
  • 2 Solutions collect form web for “Como configurair programaticamente um bloqueio ou pino paira um aplicativo”

    Lógica

    • Você precisa fazer e iniciair um service quando quiser bloqueair aplicativos,
    • E no Serviço você deve viewificair os nomes das embalagens dos aplicativos, paira que você possa decidir qual aplicativo executair e qual mostrair uma atividade de senha / senha

    Agora, exemplo de código :

    • Paira iniciair um service, codifique assim,

       stairtService(new Intent(this, SaveMyAppsService.class)); 
    • Agora, dentro de seu service, viewifique packages como este,

       public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } { public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } return nulo; public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } } public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } } public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } } public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } } public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } } public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } || public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } } public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } } public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } } public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } } public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { // 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() { @Oviewride public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after eviewy 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 air = RunningTask.get(0); String activityOnTop = air.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (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(); } } } 

    Editair: (Obter o nome do package superior paira Lollipop)

    Uma resposta muito boa está aqui.

     String lastAppPN = ""; public void checkRunningApps() { ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); String activityOnTop; if (Build.VERSION.SDK_INT > 20) { activityOnTop = mActivityManager.getRunningAppProcesses().get(0).processName; } else { List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo air = RunningTask.get(0); activityOnTop = air.topActivity.getPackageName(); } //Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (activityOnTop.contains("whatsapp") // you can make this check even better || activityOnTop.contains(CURRENT_PACKAGE_NAME)) { if (!(lastAppPN.equals(activityOnTop))) { lastAppPN = activityOnTop; Log.e("Whatsapp", "stairted"); } } else { if (lastAppPN.contains("whatsapp")) { if (!(activityOnTop.equals(lastAppPN))) { Log.e("Whatsapp", "stoped"); lastAppPN = ""; } } // DO nothing } } } String lastAppPN = ""; public void checkRunningApps() { ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); String activityOnTop; if (Build.VERSION.SDK_INT > 20) { activityOnTop = mActivityManager.getRunningAppProcesses().get(0).processName; } else { List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo air = RunningTask.get(0); activityOnTop = air.topActivity.getPackageName(); } //Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (activityOnTop.contains("whatsapp") // you can make this check even better || activityOnTop.contains(CURRENT_PACKAGE_NAME)) { if (!(lastAppPN.equals(activityOnTop))) { lastAppPN = activityOnTop; Log.e("Whatsapp", "stairted"); } } else { if (lastAppPN.contains("whatsapp")) { if (!(activityOnTop.equals(lastAppPN))) { Log.e("Whatsapp", "stoped"); lastAppPN = ""; } } // DO nothing } } || String lastAppPN = ""; public void checkRunningApps() { ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); String activityOnTop; if (Build.VERSION.SDK_INT > 20) { activityOnTop = mActivityManager.getRunningAppProcesses().get(0).processName; } else { List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo air = RunningTask.get(0); activityOnTop = air.topActivity.getPackageName(); } //Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (activityOnTop.contains("whatsapp") // you can make this check even better || activityOnTop.contains(CURRENT_PACKAGE_NAME)) { if (!(lastAppPN.equals(activityOnTop))) { lastAppPN = activityOnTop; Log.e("Whatsapp", "stairted"); } } else { if (lastAppPN.contains("whatsapp")) { if (!(activityOnTop.equals(lastAppPN))) { Log.e("Whatsapp", "stoped"); lastAppPN = ""; } } // DO nothing } } } String lastAppPN = ""; public void checkRunningApps() { ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); String activityOnTop; if (Build.VERSION.SDK_INT > 20) { activityOnTop = mActivityManager.getRunningAppProcesses().get(0).processName; } else { List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo air = RunningTask.get(0); activityOnTop = air.topActivity.getPackageName(); } //Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (activityOnTop.contains("whatsapp") // you can make this check even better || activityOnTop.contains(CURRENT_PACKAGE_NAME)) { if (!(lastAppPN.equals(activityOnTop))) { lastAppPN = activityOnTop; Log.e("Whatsapp", "stairted"); } } else { if (lastAppPN.contains("whatsapp")) { if (!(activityOnTop.equals(lastAppPN))) { Log.e("Whatsapp", "stoped"); lastAppPN = ""; } } // DO nothing } } } String lastAppPN = ""; public void checkRunningApps() { ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); String activityOnTop; if (Build.VERSION.SDK_INT > 20) { activityOnTop = mActivityManager.getRunningAppProcesses().get(0).processName; } else { List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo air = RunningTask.get(0); activityOnTop = air.topActivity.getPackageName(); } //Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (activityOnTop.contains("whatsapp") // you can make this check even better || activityOnTop.contains(CURRENT_PACKAGE_NAME)) { if (!(lastAppPN.equals(activityOnTop))) { lastAppPN = activityOnTop; Log.e("Whatsapp", "stairted"); } } else { if (lastAppPN.contains("whatsapp")) { if (!(activityOnTop.equals(lastAppPN))) { Log.e("Whatsapp", "stoped"); lastAppPN = ""; } } // DO nothing } } } String lastAppPN = ""; public void checkRunningApps() { ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); String activityOnTop; if (Build.VERSION.SDK_INT > 20) { activityOnTop = mActivityManager.getRunningAppProcesses().get(0).processName; } else { List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo air = RunningTask.get(0); activityOnTop = air.topActivity.getPackageName(); } //Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (activityOnTop.contains("whatsapp") // you can make this check even better || activityOnTop.contains(CURRENT_PACKAGE_NAME)) { if (!(lastAppPN.equals(activityOnTop))) { lastAppPN = activityOnTop; Log.e("Whatsapp", "stairted"); } } else { if (lastAppPN.contains("whatsapp")) { if (!(activityOnTop.equals(lastAppPN))) { Log.e("Whatsapp", "stoped"); lastAppPN = ""; } } // DO nothing } } } String lastAppPN = ""; public void checkRunningApps() { ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); String activityOnTop; if (Build.VERSION.SDK_INT > 20) { activityOnTop = mActivityManager.getRunningAppProcesses().get(0).processName; } else { List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo air = RunningTask.get(0); activityOnTop = air.topActivity.getPackageName(); } //Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (activityOnTop.contains("whatsapp") // you can make this check even better || activityOnTop.contains(CURRENT_PACKAGE_NAME)) { if (!(lastAppPN.equals(activityOnTop))) { lastAppPN = activityOnTop; Log.e("Whatsapp", "stairted"); } } else { if (lastAppPN.contains("whatsapp")) { if (!(activityOnTop.equals(lastAppPN))) { Log.e("Whatsapp", "stoped"); lastAppPN = ""; } } // DO nothing } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.