gpt4 book ai didi

android - 即使有 startforeground 应用程序崩溃

转载 作者:太空狗 更新时间:2023-10-29 13:31:55 29 4
gpt4 key购买 nike

即使有前台服务,我的 Android 应用程序也会在后台被杀死。这是漏洞 list :

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

下面是我如何启动我的服务:

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

startService(i);

这是它被杀死时的日志:

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

我在第一个 Activity onCreate中启动服务

请帮助我。

最佳答案

您有 2 个独立的进程。您的 Activity 在一个进程中运行,而您的服务在另一个进程中运行。您已经告诉 Android 您的服务 是一个前台服务,应该防止 Android 终止它。但是,您的 Activity 正在另一个进程中运行,当该进程进入后台时,Android 会直接将其杀死。

要解决此问题,请将您的服务和您的 Activity 放入同一个进程中。然后,Android 不会终止该进程,因为您已经在其中运行了前台服务。当然,不能保证 Android 无论如何都不会终止您的进程(如果它需要资源),但如果您的应用程序不是资源大户,它很有可能会继续存在。

要将服务和 Activity 放在同一进程中,请从 <service> 中删除此行 list 中的描述:

    android:process=":my_process"

关于android - 即使有 startforeground 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14704501/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com