androidactivity-manager

How to know my android app is running in the receiver?


I try wrote some code to get SMS and when the SMS come from special number do something. I define a receiver to my app then i used below code to determine if app is running go ahead otherwise show a notification.

ActivityManager activityManager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
        for(int i = 0; i < procInfos.size(); i++){
            if(procInfos.get(i).processName.equals(app)){
                // set running
                appState.isRunning = true;
                break;
            }
        }

I test this code by emulator and it always return true. i trace my app in the DDMS and find out when i send SMS the app start to run but without UI.

How can I do this?


Solution

  • Your application is automatically created before its receiver can run (where would it run otherwise?).

    If you want to check whether there is any activity in your application, there is no API to do that but you can just have a counter that is increased in onCreate and decreased in onDestroy.