应用程序在启动时崩溃 - “系统服务在 onCreate() 之前对 Activity 不可用”

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

我想检查一个应用程序是否是 运行 下面的代码。但是,我的应用程序在启动时崩溃了。 我正在使用 ActivityManager 来查找应用程序的存在。 isAppRunning 方法采用应用程序的名称,如果 ActivityManager 认为它当前是 运行.

,则 returns 为真
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.access0(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)

第 1 步:删除 MainActivity M = new MainActivity();。切勿手动创建 Activity(或 ServiceContentProvider)的实例。

第 2 步:将 M.isAppRunning("com.mcent.app"); 更改为 isAppRunning("com.mcent.app");