Application error even at startup

My Android app got killed in the background even when using the foreground. Here is the manifestation of the hole:

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

      

and here is the code for 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);

      

and this is how I start mine service

:

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

        startService(i);

      

Here is the logarithm of the moment he was killed:

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

      

I start service

in onCreate

mine first activity

.

Please help me with this.

+3


source to share


2 answers


You have two separate processes. Your actions are performed in one process, and your service is running in another. You told Android that your service is a foreground feature that should prevent Android from killing it. However, your actions are taking place in a different process, and when that process goes into the background, Android will simply kill it.

To fix this, put your service and your actions in the same process. Then Android will not kill the process because the foreground service is running in it. Of course, there is no guarantee that Android will not kill your process anyway (if it needs resources), but the chances are pretty good if your application is not resource intensive that it will stay alive.



To put the service and activities in the same process, remove this line from the description <service>

in the manifest:

    android:process=":my_process"

      

+1


source


Unfortunately, there is no way to guarantee that your service won't get killed by android: see http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/

Keeping a service indefinitely is also not such a good idea: http://www.androidguys.com/2010/03/29/code-pollution-background-control/



You can use some methods to increase the priority of a service, but at one point this service will be killed.

+1


source







All Articles