Search code examples
javaandroidandroid-activityoncreate

App Crashes At Launch - “System services not available to Activities before onCreate()”


I want to check whether an app is running with the code below. But, my app crashes on launch. I am using ActivityManager to find the presence of the app. The isAppRunning method takes the name of an application and returns true if the ActivityManager thinks it is currently running.

package com.mavenmaverick.process;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MainActivity M = new MainActivity();
    M.isAppRunning("com.mcent.app");

}

public boolean isAppRunning (String aApplicationPackageName)
{
        ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
            if (activityManager == null)
            {       
                    Toast.makeText(this, "Not Running", Toast.LENGTH_SHORT).show();
                    return false;
            }
            List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
            for(int idx = 0; idx < procInfos.size(); idx++)
                {
                    if(procInfos.get(idx).processName.equals(aApplicationPackageName))
                        {
                            Toast.makeText(this, "Running", Toast.LENGTH_SHORT).show();
                            return true;
                        }
                }
            return false;
}



LogCat

01-14 01:19:45.695: E/AndroidRuntime(3335): FATAL EXCEPTION: main
01-14 01:19:45.695: E/AndroidRuntime(3335): java.lang.RuntimeException: Unable to start activity       ComponentInfo{com.mavenmaverick.process/com.mavenmaverick.process.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
01-14 01:19:45.695: E/AndroidRuntime(3335):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at android.os.Looper.loop(Looper.java:137)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at java.lang.reflect.Method.invokeNative(Native Method)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at java.lang.reflect.Method.invoke(Method.java:525)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at dalvik.system.NativeStart.main(Native Method)
01-14 01:19:45.695: E/AndroidRuntime(3335): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
01-14 01:19:45.695: E/AndroidRuntime(3335):     at android.app.Activity.getSystemService(Activity.java:4492)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at com.mavenmaverick.process.MainActivity.isAppRunning(MainActivity.java:28)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at com.mavenmaverick.process.MainActivity.onCreate(MainActivity.java:22)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at android.app.Activity.performCreate(Activity.java:5133)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-14 01:19:45.695: E/AndroidRuntime(3335):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

Solution

  • Step #1: Delete MainActivity M = new MainActivity();. Never manually create an instance of Activity (or Service or ContentProvider, for that matter).

    Step #2: Change M.isAppRunning("com.mcent.app"); to isAppRunning("com.mcent.app");