为什么system_process第二次杀了我的服务?

时间:2012-04-17 19:44:24

标签: 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: 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 Manifest:

<?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>

感谢。

0 个答案:

没有答案