应用程序崩溃甚至与startforeground

时间:2013-02-05 09:51:08

标签: android background-process foreground

我的Android应用程序即使使用前台服务也在后台被杀死。 这是洞清单:

<?xml version="1.0" encoding="utf-8"?>

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true" >
</supports-screens>

<application
    android:debuggable="true"
    android:icon="@drawable/icon"
    android:label="TritonHK"
    android:theme="@android:style/Theme.NoTitleBar" >
    <activity
        android:name=".LoginActivity"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".MyService"
        android:icon="@drawable/icon"
        android:label="TritonHK"
        android:process=":my_process" >
    </service>

    <activity
        android:name="Setting"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="MenuScreen"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="TaskSheetList"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="TaskDetails"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="CreditsList"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="SectionsList"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="SuperDashboard"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="InspectionActivity"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="Inbox"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="InspectionDetail"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="StaffListView"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="MultiTaskSheetListView"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="CheckListItemDetails"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="OpenJobs"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="GuestDetails"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="MessageList"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="ImageCollectionActivity"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="NewBGMessages"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
    </activity>
</application>
<!-- Added for 4.1 Android os -->
<uses-sdk android:minSdkVersion="7" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.telephony" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE" >
</uses-permission>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" >
</uses-permission>
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这是service

的代码
 MLog.w(getClass().getName(), "TritonHK started");

         Notification note=new Notification(R.drawable.icon,
                                             "TritonHK is running",
                                             System.currentTimeMillis());
         Intent i=new Intent(this, BGMessages.class);

         i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);

         PendingIntent pi=PendingIntent.getActivity(this, 0,
                                                     i, 0);

         note.setLatestEventInfo(this, "TritonHK",
                                 "TritonHK",
                                 pi);
         note.flags|=Notification.FLAG_NO_CLEAR;

         startForeground(1337, note);

以下是我如何开始service

Intent i=new Intent(this, MyService.class);

        startService(i);

这是logcat刚被杀的时候:

02-05 10:04:16.676 D/dalvikvm(20458): GC_CONCURRENT freed 2010K, 54% free 3862K/8327K, external 11961K/14009K, paused 13ms+2ms
02-05 10:04:20.576 D/dalvikvm(19610): GC_CONCURRENT freed 511K, 52% free 2973K/6151K, external 2171K/2711K, paused 2ms+2ms
02-05 10:04:28.256 D/dalvikvm(19610): GC_CONCURRENT freed 449K, 52% free 2966K/6151K, external 2112K/2638K, paused 2ms+11ms
02-05 10:04:32.626 D/dalvikvm(20458): GC_CONCURRENT freed 1970K, 54% free 3862K/8327K, external 11961K/14009K, paused 7ms+2ms
02-05 10:04:43.216 D/dalvikvm(19610): GC_CONCURRENT freed 498K, 52% free 2974K/6151K, external 2140K/2672K, paused 2ms+2ms
02-05 10:04:48.266 D/dalvikvm(20458): GC_CONCURRENT freed 1974K, 54% free 3869K/8327K, external 11961K/14009K, paused 3ms+3ms
02-05 10:04:58.746 D/dalvikvm(19610): GC_CONCURRENT freed 510K, 52% free 2973K/6151K, external 2140K/2672K, paused 1ms+2ms
02-05 10:05:00.176 D/kernel  (  120): [40912.899353] max17040 0-0036: batt:100%, 4126 mV
02-05 10:05:03.686 D/dalvikvm(20458): GC_CONCURRENT freed 1979K, 54% free 3862K/8327K, external 11961K/14009K, paused 7ms+3ms
02-05 10:05:13.616 D/dalvikvm(19610): GC_CONCURRENT freed 510K, 52% free 2973K/6151K, external 2202K/2750K, paused 2ms+2ms
02-05 10:05:19.706 D/dalvikvm(20458): GC_CONCURRENT freed 1970K, 54% free 3862K/8327K, external 11961K/14009K, paused 14ms+2ms
02-05 10:05:21.346 D/dalvikvm(19610): GC_CONCURRENT freed 449K, 52% free 2962K/6151K, external 2112K/2638K, paused 2ms+3ms
02-05 10:05:35.526 D/dalvikvm(20458): GC_CONCURRENT freed 2038K, 54% free 3871K/8327K, external 11961K/14009K, paused 1ms+3ms
02-05 10:05:36.676 D/skia    (20458): purging 194K from font cache [25 entries]
02-05 10:05:36.936 D/dalvikvm(19610): GC_CONCURRENT freed 498K, 52% free 2974K/6151K, external 2202K/2750K, paused 1ms+5ms
02-05 10:05:44.656 D/dalvikvm(  238): GC_CONCURRENT freed 1828K, 42% free 7427K/12743K, external 6677K/8035K, paused 5ms+20ms
02-05 10:05:46.206 I/ActivityManager(  238): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10100000 cmp=com.tritonhk.android/.LoginActivity } from pid 238
02-05 10:05:46.266 I/ActivityManager(  238): Start proc com.tritonhk.android for activity com.tritonhk.android/.TaskDetails: pid=20479 uid=10124 gids={1006, 3003, 1015}
02-05 10:05:46.336 W/InputManagerService(  238): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@2b4a1708

我正在service的{​​{1}}开始onCreate

请帮助我。

2 个答案:

答案 0 :(得分:1)

不幸的是,没有任何可能保证您的服务不会被Android杀死:见http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/

让服务保持无限时间也不是一个好主意:http://www.androidguys.com/2010/03/29/code-pollution-background-control/

您可以使用某些方法来提高服务的优先级,但在某一时刻,此服务将被终止。

答案 1 :(得分:1)

您有2个独立的流程。您的活动在一个进程中运行,而您的服务在另一个进程中运行。您告诉Android,您的服务是一项前台服务,可以防止Android将其杀死。但是,您的活动正在另一个进程中运行,当该进程进入后台时,Android将会将其终止。

要解决此问题,请将您的服务和活动置于同一过程中。然后,Android将不会终止该进程,因为您已经在其中运行了前台服务。当然,无论如何都无法保证Android不会终止您的进程(如果它需要资源),但如果您的应用程序不是资源耗尽它将保持活跃的可能性非常大。

要将服务和活动放在同一个流程中,请从清单中的<service>说明中删除此行:

    android:process=":my_process"