Android 为什么系统进程第二次终止我的服务?

Android 为什么系统进程第二次终止我的服务?,android,Android,我有一个带有服务的android应用程序。第一次运行该服务时效果很好,但第二次在同一个模拟器中运行时,会收到此消息 04-17 18:27:15.808: D/PackageManager(61): Services: es.myapp.PhoneService 04-17 18:27:15.817: D/PackageManager(61): Receivers: es.myapp.ScreenReceiver es.myapp.Receiver 04-17 18:27:15.817:

我有一个带有服务的android应用程序。第一次运行该服务时效果很好,但第二次在同一个模拟器中运行时,会收到此消息

04-17 18:27:15.808: D/PackageManager(61):   Services: es.myapp.PhoneService
04-17 18:27:15.817: D/PackageManager(61):   Receivers: es.myapp.ScreenReceiver es.myapp.Receiver
04-17 18:27:15.817: D/PackageManager(61):   Activities: es.myapp.Login es.myapp.TakePhoto
04-17 18:27:16.157: D/dalvikvm(155): GC_EXTERNAL_ALLOC freed 238K, 49% free 3007K/5895K, external 6685K/6692K, paused 67ms
04-17 18:27:16.207: I/installd(34): move /data/dalvik-cache/data@app@es.myapp-1.apk@classes.dex -> /data/dalvik-cache/data@app@es.myapp-1.apk@classes.dex
04-17 18:27:16.207: D/PackageManager(61): New package installed in /data/app/es.myapp-1.apk
04-17 18:27:16.377: D/dalvikvm(155): GC_EXPLICIT freed 99K, 51% free 2921K/5895K, external 4655K/5618K, paused 54ms
04-17 18:27:17.427: I/ActivityManager(61): Force stopping package es.myapp uid=10035
我在模拟器上有足够的内存。
因此,每次我运行正常时,我都必须销毁机器并重新创建它。原因是什么

代码如下:

public class PhoneService extends Service{

    private final IBinder mBinder = new LocalBinder();  

    public class LocalBinder extends Binder {
        PhoneService getService() {
            return PhoneService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate(){

        this.setForeground(true);

        try {
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            filter.addAction(Intent.ACTION_USER_PRESENT);
            BroadcastReceiver mReceiver = new ScreenReceiver();
            registerReceiver(mReceiver, filter);
       } catch (Exception e) {

       }    
    }

       @Override
        public void onDestroy() {
        }


       @Override
       public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStart(intent, startId);
            return START_STICKY;
    }
}





public class Receiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {

            Intent serviceIntent = new Intent();
            serviceIntent.setAction("es.myapp.PhoneService");
            context.startService(serviceIntent);
    }
}




public class ScreenReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {         

        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
            Intent photoIntent = new Intent(context, TakePhoto.class);
            photoIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(photoIntent);
        }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){

        }
        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){

        }
    }
}
Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="es.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity
            android:name=".Login"

            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity> 

        <activity
            android:name=".TakePhoto"
            android:theme="@android:style/Theme.NoDisplay"
            android:label="@string/app_name" >

        </activity> 

        <service android:name=".PhoneService">
            <intent-filter>
                <action android:name="es.myapp.PhoneService"/>
            </intent-filter>
        </service> 

        <receiver android:name=".ScreenReceiver">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_ON" /> 
                <action android:name="android.intent.action.SCREEN_OFF" />
                <action android:name="android.Intent.ACTION_USER_PRESENT" />
            </intent-filter>
        </receiver>

        <receiver android:name=".Receiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

    </application>


</manifest>


谢谢。

活动结束后,您是否在活动中停止服务?因为第二次运行(从IDE)会重新安装应用程序,而旧的应用程序必须停止…服务始终在后台运行,等待事件屏幕打开。另外,在第二次启动应用程序之前,我试图手动停止服务,但仍然存在相同的问题…还抛出此错误:04-17 21:51:07.552:I/ActivityManager(60):强制停止包es.myapp uid=10034 04-17 21:51:07.552:W/ActivityManager(60):计划在5000ms 04-17 21:51:07.562:I/进程(60):发送信号中重新启动崩溃的服务es.myapp/.PhoneService。PID:298 SIG:9I在手机上测试了该应用程序,但未运行,原因是该应用程序安装在SD卡上,而不是安装在手机上,SD卡是在启动完成后安装的,因此未收到事件。谢谢你的回答