Guest User

Untitled

a guest
Nov 20th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. if(activityrunning == activity1)
  2. //do this
  3. else if (activityrunning == activity2)
  4. //do something else
  5.  
  6. class MyActivity extends Activity {
  7. static boolean active = false;
  8.  
  9. @Override
  10. public void onStart() {
  11. super.onStart();
  12. active = true;
  13. }
  14.  
  15. @Override
  16. public void onStop() {
  17. super.onStop();
  18. active = false;
  19. }
  20. }
  21.  
  22. public boolean isRunning(Context ctx) {
  23. ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
  24. List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
  25.  
  26. for (RunningTaskInfo task : tasks) {
  27. if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
  28. return true;
  29. }
  30.  
  31. return false;
  32. }
  33.  
  34. public class example extends Activity {
  35.  
  36. @Override
  37. protected void onStart() {
  38. super.onStart();
  39.  
  40. // Store our shared preference
  41. SharedPreferences sp = getSharedPreferences("OURINFO", MODE_PRIVATE);
  42. Editor ed = sp.edit();
  43. ed.putBoolean("active", true);
  44. ed.commit();
  45. }
  46.  
  47. @Override
  48. protected void onStop() {
  49. super.onStop();
  50.  
  51. // Store our shared preference
  52. SharedPreferences sp = getSharedPreferences("OURINFO", MODE_PRIVATE);
  53. Editor ed = sp.edit();
  54. ed.putBoolean("active", false);
  55. ed.commit();
  56.  
  57. }
  58. }
  59.  
  60. public static boolean isServiceRunning(Context context) {
  61.  
  62. Log.i(TAG, "Checking if service is running");
  63.  
  64. ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
  65.  
  66. List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
  67.  
  68. boolean isServiceFound = false;
  69.  
  70. for (int i = 0; i < services.size(); i++) {
  71.  
  72. if (Constants.PACKAGE.equals(services.get(i).service.getPackageName())){
  73.  
  74. if (Constants.BACKGROUND_SERVICE_CLASS.equals(services.get(i).service.getClassName())){
  75. isServiceFound = true;
  76. }
  77. }
  78. }
  79.  
  80. Log.i(TAG, "Service was" + (isServiceFound ? "" : " not") + " running");
  81.  
  82. return isServiceFound;
  83.  
  84. }
  85.  
  86. protected Boolean isActivityRunning(Class activityClass)
  87. {
  88. ActivityManager activityManager = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
  89. List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
  90.  
  91. for (ActivityManager.RunningTaskInfo task : tasks) {
  92. if (activityClass.getCanonicalName().equalsIgnoreCase(task.baseActivity.getClassName()))
  93. return true;
  94. }
  95.  
  96. return false;
  97. }
  98.  
  99. Activity#getWindow().getDecorView().getRootView().isShown()
  100.  
  101. public boolean isServiceRunning() {
  102.  
  103. ActivityManager activityManager = (ActivityManager)Monitor.this.getSystemService (Context.ACTIVITY_SERVICE);
  104. List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
  105. isServiceFound = false;
  106. for (int i = 0; i < services.size(); i++) {
  107. if (services.get(i).topActivity.toString().equalsIgnoreCase("ComponentInfo{com.lyo.AutoMessage/com.lyo.AutoMessage.TextLogList}")) {
  108. isServiceFound = true;
  109. }
  110. }
  111. return isServiceFound;
  112. }
  113.  
  114. <uses-permission android:name="android.permission.GET_TASKS"/>
  115.  
  116. @protected
  117. void doSomething() {
  118. }
  119.  
  120. private static boolean mainActivityIsOpen;
  121.  
  122. public static boolean mainActivityIsOpen() {
  123. return mainActivityIsOpen;
  124. }
  125.  
  126. public static void mainActivityIsOpen(boolean mainActivityIsOpen) {
  127. DayView.mainActivityIsOpen = mainActivityIsOpen;
  128. }
  129.  
  130. if (MainActivity.mainActivityIsOpen() == false)
  131. {
  132. //do something
  133. }
  134. else if(MainActivity.mainActivityIsOpen() == true)
  135. {//or just else. . . ( or else if, does't matter)
  136. //do something
  137. }
  138.  
  139. @Override
  140. protected void onCreate(Bundle savedInstanceState) {
  141. super.onCreate(savedInstanceState);
  142. if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
  143. // Activity is being brought to front and not being created again,
  144. // Thus finishing this activity will bring the last viewed activity to foreground
  145. finish();
  146. }
  147. }
  148.  
  149. class MyActivity extends Activity {
  150.  
  151. static int activeInstances = 0;
  152.  
  153. static boolean isActive() {
  154. return (activeInstance > 0)
  155. }
  156.  
  157. @Override
  158. public void onStart() {
  159. super.onStart();
  160. activeInstances++;
  161. }
  162.  
  163. @Override
  164. public void onStop() {
  165. super.onStop();
  166. activeInstances--;
  167. }
  168. }
Add Comment
Please, Sign In to add comment