4

即使使用前台服务,我的 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的.onCreatefirst activity

请帮助我。

4

2 回答 2

1

您有 2 个单独的进程。您的活动在一个进程中运行,而您的服务在另一个进程中运行。你告诉 Android 你的服务是一个前台服务,应该防止 Android 杀死它。但是,您的活动正在另一个进程中运行,当该进程进入后台时,Android 只会将其杀死。

要解决此问题,请将您的服务和您的活动置于同一流程中。然后,Android 不会终止该进程,因为您在其中运行了一个前台服务。当然,不能保证 Android 无论如何都不会杀死您的进程(如果它需要资源),但是如果您的应用程序不是资源占用者,那么它很有可能会继续存在。

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

    android:process=":my_process"
于 2013-02-05T10:49:34.463 回答
0

不幸的是,没有任何可能保证您的服务不会被 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/

您可以使用一些方法来提高服务的优先级,但在某一时刻,该服务会被杀死。

于 2013-02-05T10:16:24.400 回答