Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(activityrunning == activity1)
- //do this
- else if (activityrunning == activity2)
- //do something else
- class MyActivity extends Activity {
- static boolean active = false;
- @Override
- public void onStart() {
- super.onStart();
- active = true;
- }
- @Override
- public void onStop() {
- super.onStop();
- active = false;
- }
- }
- public boolean isRunning(Context ctx) {
- ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
- List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
- for (RunningTaskInfo task : tasks) {
- if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
- return true;
- }
- return false;
- }
- public class example extends Activity {
- @Override
- protected void onStart() {
- super.onStart();
- // Store our shared preference
- SharedPreferences sp = getSharedPreferences("OURINFO", MODE_PRIVATE);
- Editor ed = sp.edit();
- ed.putBoolean("active", true);
- ed.commit();
- }
- @Override
- protected void onStop() {
- super.onStop();
- // Store our shared preference
- SharedPreferences sp = getSharedPreferences("OURINFO", MODE_PRIVATE);
- Editor ed = sp.edit();
- ed.putBoolean("active", false);
- ed.commit();
- }
- }
- public static boolean isServiceRunning(Context context) {
- Log.i(TAG, "Checking if service is running");
- ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
- List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
- boolean isServiceFound = false;
- for (int i = 0; i < services.size(); i++) {
- if (Constants.PACKAGE.equals(services.get(i).service.getPackageName())){
- if (Constants.BACKGROUND_SERVICE_CLASS.equals(services.get(i).service.getClassName())){
- isServiceFound = true;
- }
- }
- }
- Log.i(TAG, "Service was" + (isServiceFound ? "" : " not") + " running");
- return isServiceFound;
- }
- protected Boolean isActivityRunning(Class activityClass)
- {
- ActivityManager activityManager = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
- List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
- for (ActivityManager.RunningTaskInfo task : tasks) {
- if (activityClass.getCanonicalName().equalsIgnoreCase(task.baseActivity.getClassName()))
- return true;
- }
- return false;
- }
- Activity#getWindow().getDecorView().getRootView().isShown()
- public boolean isServiceRunning() {
- ActivityManager activityManager = (ActivityManager)Monitor.this.getSystemService (Context.ACTIVITY_SERVICE);
- List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
- isServiceFound = false;
- for (int i = 0; i < services.size(); i++) {
- if (services.get(i).topActivity.toString().equalsIgnoreCase("ComponentInfo{com.lyo.AutoMessage/com.lyo.AutoMessage.TextLogList}")) {
- isServiceFound = true;
- }
- }
- return isServiceFound;
- }
- <uses-permission android:name="android.permission.GET_TASKS"/>
- @protected
- void doSomething() {
- }
- private static boolean mainActivityIsOpen;
- public static boolean mainActivityIsOpen() {
- return mainActivityIsOpen;
- }
- public static void mainActivityIsOpen(boolean mainActivityIsOpen) {
- DayView.mainActivityIsOpen = mainActivityIsOpen;
- }
- if (MainActivity.mainActivityIsOpen() == false)
- {
- //do something
- }
- else if(MainActivity.mainActivityIsOpen() == true)
- {//or just else. . . ( or else if, does't matter)
- //do something
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
- // Activity is being brought to front and not being created again,
- // Thus finishing this activity will bring the last viewed activity to foreground
- finish();
- }
- }
- class MyActivity extends Activity {
- static int activeInstances = 0;
- static boolean isActive() {
- return (activeInstance > 0)
- }
- @Override
- public void onStart() {
- super.onStart();
- activeInstances++;
- }
- @Override
- public void onStop() {
- super.onStop();
- activeInstances--;
- }
- }
Add Comment
Please, Sign In to add comment