Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. public class BackgroundService extends Service {
  2.  
  3. public static Runnable runnable = null;
  4. public Context context = this;
  5. public Handler handler = null;
  6.  
  7.  
  8. @Override
  9. public IBinder onBind(Intent intent) {
  10. return null;
  11. }
  12.  
  13. @Override
  14. public void onCreate() {
  15. final PackageManager manager = getPackageManager();
  16.  
  17.  
  18.  
  19. //Packages instalados no dispositivo
  20. List<ApplicationInfo> packages = manager.getInstalledApplications(PackageManager.GET_META_DATA);
  21. for (ApplicationInfo info : packages) {
  22. Log.i("Info", "Installed package:" + info.packageName);
  23. }
  24.  
  25. for (int i = 0; i < packages.size(); i++) {
  26. if(packages.get(i).sourceDir.startsWith("/data/app/")){
  27. //Non System Apps
  28. Log.i("Info", "Installed package /NON SYSTEM/:" + packages.get(i).packageName);
  29.  
  30. }else{
  31. //system Apps
  32. Log.i("Info", "Installed package !/SYSTEM/!:" + packages.get(i).packageName);
  33.  
  34. }}
  35.  
  36.  
  37.  
  38.  
  39. handler = new Handler();
  40. runnable = new Runnable() {
  41. public void run() {
  42. final ActivityManager am = (ActivityManager) getBaseContext().getSystemService(ACTIVITY_SERVICE);
  43.  
  44. String currentApp ="";
  45.  
  46. // The first in the list of RunningTasks is always the foreground task.
  47. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
  48. UsageStatsManager usm = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
  49. long time = System.currentTimeMillis();
  50. List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
  51. time - 1000 * 1000, time);
  52. if (appList != null && appList.size() > 0) {
  53. SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
  54. for (UsageStats usageStats : appList) {
  55. mySortedMap.put(usageStats.getLastTimeUsed(),
  56. usageStats);
  57. }
  58. if (mySortedMap != null && !mySortedMap.isEmpty()) {
  59. currentApp = mySortedMap.get(
  60. mySortedMap.lastKey()).getPackageName();
  61. }
  62. }
  63. } else {
  64. ActivityManager.RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
  65. currentApp = foregroundTaskInfo.topActivity.getPackageName();
  66. }
  67.  
  68. boolean ApiLigaIsRunning = false;
  69.  
  70. if (currentApp.contains("maps")) {
  71. ApiLigaIsRunning = true;
  72. Log.i("CHOOSEN APP IS RUNNING ","YES!!!!!!!!!!! " + currentApp);
  73. Handler handler2 = new Handler();
  74. final String finalCurrentApp = currentApp;
  75. handler2.postDelayed(new Runnable() {
  76. public void run() {
  77. Intent openMe = new Intent(getApplicationContext(), LoginActivity.class);
  78. openMe.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  79. startActivity(openMe);
  80. am.killBackgroundProcesses(finalCurrentApp);
  81. }
  82. }, 200);
  83.  
  84. }
  85.  
  86. Toast.makeText(context, "Service is running", Toast.LENGTH_LONG).show();
  87. List<ActivityManager.RunningAppProcessInfo> appProcesses = am.getRunningAppProcesses();
  88. for(ActivityManager.RunningAppProcessInfo appProcess : appProcesses){
  89. if(appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
  90. if (ApiLigaIsRunning == true)
  91. Log.i("Foreground App ", appProcess.processName);
  92. else
  93. Log.i("Not Working! ", appProcess.processName);
  94. }
  95.  
  96. handler.postDelayed(runnable,200);
  97.  
  98.  
  99. }
  100. }
  101. };
  102.  
  103. handler.postDelayed(runnable, 200);
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. @Override
  113. public void onDestroy() {
  114. Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
  115. }
  116.  
  117. <?xml version="1.0" encoding="utf-8"?>
  118. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  119. xmlns:tools="http://schemas.android.com/tools"
  120. package="***************">
  121. <uses-permission android:name="android.permission.INTERNET" />
  122. <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
  123. <uses-permission android:name="android.permission.REAL_GET_TASKS" />
  124. <uses-permission android:name="android.permission.GET_TASKS"/>
  125. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  126. <uses-permission android:name="android.permission.GET_TOP_ACTIVITY_INFO"/>
  127. <uses-permission android:name="android.permission.INSTANT_APP_FOREGROUND_SERVICE"/>
  128. <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" />
  129.  
  130.  
  131.  
  132. <application
  133. android:allowBackup="true"
  134. android:icon="@mipmap/icon"
  135. android:label="@string/app_name"
  136. android:theme="@style/AppTheme" >
  137. <activity
  138. android:name=".LoginActivity"
  139. android:theme="@style/AppTheme.Dark">
  140.  
  141. <intent-filter>
  142. <action android:name="android.intent.action.MAIN" />
  143.  
  144. <category android:name="android.intent.category.LAUNCHER" />
  145. </intent-filter>
  146. </activity>
  147. <service
  148. android:name=".BackgroundService"
  149. android:exported="true"
  150. android:enabled="true"
  151. />
  152.  
  153. </application>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement