Advertisement
nigatigga

Untitled

Aug 24th, 2014
2,175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. package com.spicycurryman.getdisciplined10.app;
  2.  
  3. import android.app.ActivityManager;
  4. import android.app.Service;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.os.IBinder;
  8. import android.util.Log;
  9. import android.widget.Toast;
  10.  
  11. import java.util.List;
  12. import java.util.concurrent.Executors;
  13. import java.util.concurrent.ScheduledExecutorService;
  14. import java.util.concurrent.TimeUnit;
  15.  
  16. /**
  17.  * Created by Spicycurryman on 8/21/14.
  18.  */
  19. public class SaveMyAppsService extends Service{
  20.  
  21.  
  22.  
  23.     String CURRENT_PACKAGE_NAME = "com.spicycurryman.getdisciplined10.app.dev";
  24.     String lastAppPN = "";
  25.     boolean noDelay = false;
  26.     public static SaveMyAppsService instance;
  27.  
  28.     @Override
  29.     public IBinder onBind(Intent intent) {
  30.         // TODO Auto-generated method stub
  31.         return null;
  32.     }
  33.  
  34.     @Override
  35.     public int onStartCommand(Intent intent, int flags, int startId) {
  36.         // TODO Auto-generated method stub
  37.  
  38.         scheduleMethod();
  39.         CURRENT_PACKAGE_NAME = getApplicationContext().getPackageName();
  40.         Log.e("Current PN", "" + CURRENT_PACKAGE_NAME);
  41.  
  42.         instance = this;
  43.  
  44.         return START_STICKY;
  45.     }
  46.  
  47.     private void scheduleMethod() {
  48.         // TODO Auto-generated method stub
  49.  
  50.         ScheduledExecutorService scheduler = Executors
  51.                 .newSingleThreadScheduledExecutor();
  52.         scheduler.scheduleAtFixedRate(new Runnable() {
  53.  
  54.             @Override
  55.             public void run() {
  56.                 // TODO Auto-generated method stub
  57.  
  58.                 // This method will check for the Running apps after every 100ms
  59.                 if(29==30 ) //check if the time is spent
  60.                 {
  61.                     stop();
  62.                 }
  63.                 else{
  64.                     checkRunningApps();
  65.                 }
  66.             }
  67.         }, 0, 100, TimeUnit.MILLISECONDS);
  68.     }
  69.  
  70.     public void checkRunningApps() {
  71.         ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  72.         List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager
  73.                 .getRunningTasks(1);
  74.         ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
  75.         String activityOnTop = ar.topActivity.getPackageName();
  76.  
  77.  
  78.  
  79.         Toast.makeText(SaveMyAppsService.this,
  80.                 "Your Message", Toast.LENGTH_LONG).show();
  81.  
  82.         Log.e("activity on TOp", "" + activityOnTop);
  83.  
  84.  
  85. // Provide the packagename(s) of apps here, you want to show password activity
  86.         if (activityOnTop.contains("com.android.camera")  // you can make this check even better
  87.                 || activityOnTop.contains(CURRENT_PACKAGE_NAME)) {
  88.             while(true) {
  89.                 Toast.makeText(SaveMyAppsService.this,
  90.                         "Your Message", Toast.LENGTH_LONG).show();
  91.             }
  92.  
  93.         } else {
  94.             // DO nothing
  95.         }
  96.     }
  97.  
  98.     public static void stop() {
  99.         if (instance != null) {
  100.             instance.stopSelf();
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement