Android “设置”、“应用程序”和“正在运行”下的0个进程和1个服务

Android “设置”、“应用程序”和“正在运行”下的0个进程和1个服务,android,Android,如果我在活动中使用startService启动服务,我会得到: 1 processes and 1 service 如果我现在刷掉那个活动。即移除它,我得到: 0 processes and 1 service 为什么会这样?在Android世界中,什么是流程,什么是服务 我使用START_STICKY,如果我通过设置、应用程序和运行停止服务,它不会再次启动,为什么 更新1部分代码: Activity: startService(new Intent(getApplicationContex

如果我在活动中使用startService启动服务,我会得到:

1 processes and 1 service
如果我现在刷掉那个活动。即移除它,我得到:

0 processes and 1 service
为什么会这样?在Android世界中,什么是流程,什么是服务

我使用START_STICKY,如果我通过设置、应用程序和运行停止服务,它不会再次启动,为什么

更新1部分代码:

Activity:
startService(new Intent(getApplicationContext(), MyService.class));

Service:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "Starting service");


    return(START_STICKY);
}

在android世界中,进程的定义是什么?与在任何操作系统上定义的相同-从系统的角度来看,您的应用程序是“活动的”,它具有活动内存分配堆栈,并且可以运行或不运行活动、服务等

我认为你的“怎么可能运行进程=0”,而服务=1没有制作场景,你是对的

“设置”应用程序中显示的正在运行的应用程序不仅是为开发人员设计的,也是为用户设计的,我想这就是为什么大多数供应商决定将活动任务显示为进程的原因。基本上,在这个显示中-running process=running task

大多数应用程序只启动一个任务(带有启动器标志的主活动在该模式下自动启动)。只有当其他活动以该标志明确开始时,才会有更多的任务

所以,如果你的应用程序有两个活动开始于-你会看到“2进程”


如果你的应用程序根本没有运行(你的进程真的没有激活),那么你将无法在运行应用程序屏幕中看到该应用程序。

结果证明是KitKat中的一个bug。 (有时我觉得在Android中完成任何事情都是一件大麻烦!)

正在使用的修复程序:

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartService = new Intent(getApplicationContext(), this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(
        getApplicationContext(), 1, restartService,
        PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
}

在你的情况下,主要的问题是你无法在应用程序关闭时启动服务,此时android操作系统将终止服务,如果你无法重新启动服务,请致电alam Manager,像这样启动接收器

public class BackgroundService extends Service {
    private String LOG_TAG = null;

    @Override
    public void onCreate() {
        super.onCreate();
        LOG_TAG = "app_name";
        Log.i(LOG_TAG, "service created");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(LOG_TAG, "In onStartCommand");
        //ur actual code
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Wont be called as service is not bound
        Log.i(LOG_TAG, "In onBind");
        return null;
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Log.i(LOG_TAG, "In onTaskRemoved");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(LOG_TAG, "In onDestroyed");
    }
}
清单是

         <service
            android:name=".BackgroundService"
            android:description="@string/app_name"
            android:enabled="true"
            android:label="Notification" />
        <receiver android:name="AlarmReceiver">
            <intent-filter>
                <action android:name="REFRESH_THIS" />
            </intent-filter>
        </receiver>
这将调用reciver,reciver是

public class AlarmReceiver extends BroadcastReceiver {
    Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;

        System.out.println("Alarma Reciver Called");

        if (isMyServiceRunning(this.context, BackgroundService.class)) {
            System.out.println("alredy running no need to start again");
        } else {
            Intent background = new Intent(context, BackgroundService.class);
            context.startService(background);
        }
    }

    public static boolean isMyServiceRunning(Context context, Class<?> serviceClass) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

        if (services != null) {
            for (int i = 0; i < services.size(); i++) {
                if ((serviceClass.getName()).equals(services.get(i).service.getClassName()) && services.get(i).pid != 0) {
                    return true;
                }
            }
        }
        return false;
    }
}

介意发布一些关于你到底在做什么的代码吗?@zgc7009什么不清楚?你确定它的
0流程和1服务
?我认为只要您的服务在运行,就必须始终有一个流程。你刷新屏幕了吗?我正在试图找出你的代码中有什么东西会告诉你你有0个进程,而我认为如果不做大量的工作,这是不可能的。@zgc7009刷走,即在任务列表中杀死/删除它。据我所知,START_STICKY将再次启动该服务,但会使用一个空的意图对象。我不知道你说的0进程是什么意思。您更改了注释:-)关于这一点,如果应用程序在堆栈上分配了一些空间,则进程就是正在运行的应用程序本身。服务是在该流程后台运行的应用程序的一部分。因此,在活动任务结束后,我应该手动绑定到该服务,还是在应用程序再次启动时发布新的startService?我问题的第二部分,为什么我的服务在手动停止后没有重新启动(在我从任务列表中删除活动后,我停止了服务)?很好的解释+1!这个代码是做什么的?我试图在我的服务课上每小时显示一次通知,有时我会收到通知,但单击通知会使我的应用程序崩溃,然后它也会停止我的服务,这是因为我的应用程序已从最近的应用程序列表中清除了吗?这是通知工作,但点击通知是打开应用程序,但崩溃的应用程序
public class BackgroundService extends Service {
    private String LOG_TAG = null;

    @Override
    public void onCreate() {
        super.onCreate();
        LOG_TAG = "app_name";
        Log.i(LOG_TAG, "service created");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(LOG_TAG, "In onStartCommand");
        //ur actual code
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Wont be called as service is not bound
        Log.i(LOG_TAG, "In onBind");
        return null;
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Log.i(LOG_TAG, "In onTaskRemoved");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(LOG_TAG, "In onDestroyed");
    }
}