华为ArkUI ResourceStr使用细节若干 ‘app.media.‘+val

ResourceStr拼装时以下方式时会报错。

错误方式:字符串 + string变量/常量

const val:string = 'img2';
$r('app.media.'+val);

正确方式:使用``间隔号,而不是''引号。即英文输入模式下,键盘1左侧键

const val:string = 'img2';
$r(`app.media.${val}`);

函数正确方式:你也可以使用函数来触发状态刷新比如:

@Local val:string = 'img2';
test(){
    this.val = 'img5';
}
changeImg = (str:string):ResourceStr=>{
    return $(`app.media.${str}`);
}
Image(this.changeImg(this.val));

错误方式:但是!【注意!】一旦你使用异步 async ,则会报错

1.异步函数报错

@Local val:string = 'img2';
test(){
    this.val = 'img5';
}
changeImg = async (str:string):Promise<ResourceStr>=>{
    try{
        const reData = await sqliteFun(str);
        return $(`app.media.${reData}`);
    } catch (e){ ... }
}
Image(this.changeImg(this.val));

2.以组装方式字符串+异步函数,则不报错但无效!

@Local val:string = 'img2';
test(){
    this.val = 'img5';
}
changeImg = async (str:string):Promise<string>=>{
    try{
        const reData:string = await sqliteFun(str);
        return reData;
    } catch (e){ ... }
}
Image($r(`app.media.${this.changeImg(this.val)}`));

异步正确方式:异步该怎么办?

作用与@State @Local等装饰符绑定的变量本身。比如:

@Trace face:ResourceStr = $r('app.media.img5');

private async setFace(){
    const data: skinDatas = await SQLiteGetSkinById(this.id);
    this.face = $r(`app.media.${data.face}`);
  }

原理即,不要把异步作用于UI标签,异步应作用于变量赋值。

异步->回调->赋值->UI刷新

HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31099: 11-07 15:59:00.993 1759 3780 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31105: 11-07 15:59:00.998 1759 1831 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31107: 11-07 15:59:01.002 1759 3780 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31113: 11-07 15:59:01.015 1759 3780 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31119: 11-07 15:59:01.047 1759 12340 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31128: 11-07 15:59:01.077 1759 12340 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31134: 11-07 15:59:01.080 1759 12340 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31146: 11-07 15:59:01.116 1759 12340 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31153: 11-07 15:59:01.304 1759 1865 I ActivityManager: App 10228/com.shanbay.sentence targets O+, restricted 行 31155: 11-07 15:59:01.310 1759 3780 I ActivityManager: App 10228/com.shanbay.sentence targets O+, restricted 行 31157: 11-07 15:59:01.313 1759 12340 I ActivityManager: App 10228/com.shanbay.sentence targets O+, restricted 行 31159: 11-07 15:59:01.316 1759 3780 I ActivityManager: App 10228/com.shanbay.sentence targets O+, restricted 行 31161: 11-07 15:59:01.319 1759 12340 I ActivityManager: App 10228/com.shanbay.sentence targets O+, restricted 行 31163: 11-07 15:59:01.321 1759 3780 I ActivityManager: App 10228/com.shanbay.sentence targets O+, restricted 行 31165: 11-07 15:59:01.324 1759 12340 I BroadcastQueue: Finished with ordered [bgthirdapp] broadcast BroadcastRecord{7ec5393 u0 shanbay.appwidget.action.APPWIDGET_UPDATE} cost time 20,receiversNum 6,max receiver ResolveInfo{7c88fd0 com.shanbay.sentence/com.shanbay.biz.words.widget.provider.Goal4x2Provider m=0x108000} cost time 6 行 31167: 11-07 15:59:01.340 1759 1854 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31199: 11-07 15:59:01.601 1759 12340 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31204: 11-07 15:59:01.607 1759 3780 I MediaFocusControl: requestAudioFocus() from uid/pid 10228/29927 AA=USAGE_MEDIA/CONTENT_TYPE_MUSIC clientId=android.media.AudioManager@741e8bauw.a$a@f3bb650 callingPack=com.shanbay.sentence req=3 flags=0x2 sdk=30 targetStack=null zoneId-1 行 31207: 11-07 15:59:01.608 1759 3780 I HwMediaFocusControl: isRemoteApp: false, packageName: com.shanbay.sentence 行 31217: 11-07 15:59:01.608 1759 3780 I HwMediaFocusControl: isRemoteApp: false, packageName: com.shanbay.sentence 行 31223: 11-07 15:59:01.609 3427 3759 I PG_ash : com.shanbay.sentence BinderCallState: true, nextBinderCallTriggeringTime:83343368 行 31224: 11-07 15:59:01.609 3427 3514 I PGAudioState: audio focus pkg: com.shanbay.sentence last focus pkg : com.shanbay.sentence 行 31224: 11-07 15:59:01.609 3427 3514 I PGAudioState: audio focus pkg: com.shanbay.sentence last focus pkg : com.shanbay.sentence 行 31225: 11-07 15:59:01.609 3427 3514 I AppManager: audio focus pkg: com.shanbay.sentence 行 31240: 11-07 15:59:01.623 1759 3780 I HwOcrImeController: updateMultiWindow focus window = com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity mIsInMultiScreen = true 行 31259: 11-07 15:59:01.627 29927 29865 I ServiceUtilities: GetPackageNameByUid packages size is 1, packages[0] com.shanbay.sentence, uid(10228) 行 31261: 11-07 15:59:01.627 29927 29865 I ServiceUtilities: GetPackageNameByUid packages size is 1, packages[0] com.shanbay.sentence, uid(10228) 行 31267: 11-07 15:59:01.630 1375 1620 I CodecTracker: tId: 29865, pId: 29927, type: c2.android.mp3.decoder, pkgName: com.shanbay.sentence, mimeType: , width: , height: , frameRate: , macroblocks: , lifeCycle: UNINITIALIZED->INITIALIZING->INITIALIZED->CONFIGURING, scene: 行 31273: 11-07 15:59:01.631 1375 1620 I CodecTracker: tId: 29865, pId: 29927, type: c2.android.mp3.decoder, pkgName: com.shanbay.sentence, mimeType: , width: , height: , frameRate: , macroblocks: , lifeCycle: UNINITIALIZED->INITIALIZING->INITIALIZED->CONFIGURING->CONFIGURED, scene: 行 31275: 11-07 15:59:01.631 1375 2007 I CodecTracker: tId: 29865, pId: 29927, type: c2.android.mp3.decoder, pkgName: com.shanbay.sentence, mimeType: audio/raw, width: 0, height: 0, frameRate: 0, macroblocks: 0, lifeCycle: UNINITIALIZED->INITIALIZING->INITIALIZED->CONFIGURING->CONFIGURED, scene: 3 行 31278: 11-07 15:59:01.632 1375 1620 I CodecTracker: tId: 29865, pId: 29927, type: c2.android.mp3.decoder, pkgName: com.shanbay.sentence, mimeType: audio/raw, width: 0, height: 0, frameRate: 0, macroblocks: 0, lifeCycle: UNINITIALIZED->INITIALIZING->INITIALIZED->CONFIGURING->CONFIGURED->STARTING, scene: 3 行 31283: 11-07 15:59:01.636 1375 1620 I CodecTracker: tId: 29865, pId: 29927, type: c2.android.mp3.decoder, pkgName: com.shanbay.sentence, mimeType: audio/raw, width: 0, height: 0, frameRate: 0, macroblocks: 0, lifeCycle: UNINITIALIZED->INITIALIZING->INITIALIZED->CONFIGURING->CONFIGURED->STARTING->STARTED, scene: 3 行 31284: 11-07 15:59:01.637 3427 3514 I DeviceStateService: app: com.shanbay.sentence pid: 29927 start mediacodec. 行 31334: 11-07 15:59:01.656 1759 1831 I HwAudioService: getPackageNameByPid, packageName=com.shanbay.sentence 行 31365: 11-07 15:59:01.660 844 28832 I AudioAlgoService: setParameterToDsp: params is PLAYING_APP_NAME=[com.shanbay.sentence] 行 31405: 11-07 15:59:01.705 1759 3787 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31483: 11-07 15:59:01.732 1759 3787 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31526: 11-07 15:59:01.767 1759 3787 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31537: 11-07 15:59:01.804 1759 4649 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31540: 11-07 15:59:01.831 1759 3780 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31568: 11-07 15:59:02.210 1759 3161 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31584: 11-07 15:59:02.310 1759 3780 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31634: 11-07 15:59:02.729 1759 3780 I HwMediaFocusControl: isRemoteApp: false, packageName: com.shanbay.sentence 行 31640: 11-07 15:59:02.730 1759 3780 I HwMediaFocusControl: isRemoteApp: false, packageName: com.shanbay.sentence 行 31672: 11-07 15:59:02.836 1759 4649 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31717: 11-07 15:59:03.342 1759 4649 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31855: 11-07 15:59:03.848 1759 3787 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31859: 11-07 15:59:03.900 1759 3007 W 00202/hw_netstat: total/2130/43552,com.shanbay.sentence/2130/41602,com.huawei.hinote/0/1383,com.huawei.intelligent/0/352,com.huawei.hiaction.share_user:5514/0/215 行 31890: 11-07 15:59:04.357 1759 3787 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 31927: 11-07 15:59:04.605 3427 3920 I ThermalTraceService: code:1, appInfo:com.shanbay.sentence#10000, levelInfo:0,warm_system_h,2#26,warm_dot,1 行 31970: 11-07 15:59:04.870 1759 3787 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 32037: 11-07 15:59:05.379 1759 3787 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=true windowMode=100 行 32203: 11-07 15:59:05.567 1759 1842 I WindowManager: Changing focus from Window{58ba4d4 u0 com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity} to Window{9070d86 u0 com.huawei.hinote/com.huawei.hinote.edit.EditActivity} displayId=0 行 32249: 11-07 15:59:05.576 1759 1842 W HwActivityTaskManagerServiceEx: appSwitch from: com.shanbay.sentence to: com.huawei.hinote 行 32252: 11-07 15:59:05.577 29927 29927 I TopResumedActivityChangeItem: execute start, ActivityClientRecord = ActivityRecord{8c17465 token=android.os.BinderProxy@3b36ced {com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity}} 行 32275: 11-07 15:59:05.582 3427 3514 I ScenarioService: app focus change from: com.shanbay.sentence to:com.huawei.hinote 行 32320: 11-07 15:59:05.594 3193 5686 I CapsuleManager: onAppSwitch package=com.huawei.hinote, current=com.shanbay.sentence 行 32434: 11-07 15:59:05.616 1759 3787 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 32499: 11-07 15:59:05.624 3586 4712 I {AssistantService-13.9.1.300}_AssistantApkNotifier: registerHwActivityNotifier call fromPackage:com.shanbay.sentence, toPackage:com.huawei.hinote 行 32500: 11-07 15:59:05.624 1759 1854 I SWAP_Scene: Entering notifySceneData notifySceneData. EventType:APP_SWITCH_FROM pkg:com.shanbay.sentence uid:10228 pid:0 proc: activity:null 行 32577: 11-07 15:59:05.637 3586 3752 I NetPredictEngine: Parent: active app change, old packageName: com.shanbay.sentence, new packageName: com.huawei.hinote 行 32622: 11-07 15:59:05.646 1759 15446 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 32623: Queue: [com.huawei.hwstartupguide, com.huawei.iconnect.ui, com.huawei.stylus.floatmenu, tv.danmaku.bilibilihd, com.quark.browser, com.huawei.browser, com.ss.android.ugc.aweme, com.tencent.mm, com.huawei.notepad, com.android.settings, com.huawei.phoneservice, com.shanbay.sentence, com.huawei.hinote] 行 32639: 11-07 15:59:05.650 1759 4234 I SWAP_AK : BG com.shanbay.sentence M 行 32752: 11-07 15:59:05.689 3427 3514 I ScenarioService: refresh visibleApp:[com.huawei.hinote, com.shanbay.sentence] 行 32799: 11-07 15:59:05.699 3427 3514 I ScenarioService: multiScreenVisibleApps: 0:com.huawei.hinote#com.shanbay.sentence:10188#10228 行 32800: 11-07 15:59:05.699 3427 3514 I PGServer: report state:15 event type:1 pid:-1 uid:-1 pkg:0:com.huawei.hinote#com.shanbay.sentence:10188#10228 to pid: 3427 行 33169: 11-07 15:59:05.897 1759 1831 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33228: 11-07 15:59:05.925 1759 13865 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33259: 11-07 15:59:05.945 1759 2016 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33274: 11-07 15:59:05.964 1759 1831 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33434: 11-07 15:59:06.059 1759 1831 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33446: 11-07 15:59:06.077 1759 2016 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33454: 11-07 15:59:06.079 1759 1854 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33456: 11-07 15:59:06.081 1759 13865 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33474: 11-07 15:59:06.112 1759 2016 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33476: 11-07 15:59:06.114 1759 2016 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33478: 11-07 15:59:06.117 1759 2016 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33595: 11-07 15:59:06.229 1759 2016 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33613: 11-07 15:59:06.232 1759 13865 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33731: 11-07 15:59:06.285 1759 13865 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33757: 11-07 15:59:06.292 1759 13865 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 33986: 11-07 15:59:06.449 1759 13865 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 34017: 11-07 15:59:06.491 1759 1831 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 34021: 11-07 15:59:06.493 1759 1831 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 34053: 11-07 15:59:06.512 1759 13865 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 34060: 11-07 15:59:06.517 1759 13865 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 34136: 11-07 15:59:06.618 1759 13865 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 34138: 11-07 15:59:06.620 1759 13865 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 34191: 11-07 15:59:06.664 1759 13865 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 34210: 11-07 15:59:06.677 1759 1831 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 34465: 11-07 15:59:06.966 1759 2016 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 34638: 11-07 15:59:07.063 1759 2017 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 34681: 11-07 15:59:07.079 1759 4653 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 35263: 11-07 15:59:07.267 588 588 I lowmemorykiller: Kill 'com.shanbay.sentence' (29927), uid 10228, oom_score_adj 102 to free 291240kB rss, 328664kb swap 行 35301: 11-07 15:59:07.280 588 588 I lowmemorykiller: Kill 'com.shanbay.sentence:pushcore' (22647), uid 10228, oom_score_adj 102 to free 74832kB rss, 225304kb swap 行 35312: 11-07 15:59:07.287 1759 3282 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 35355: 11-07 15:59:07.303 1759 3282 I HwActivityTaskManagerServiceEx: add visible task, topActivity=com.shanbay.sentence/com.shanbay.bay.biz.words.v2.study.StudyActivity taskId=990 isVisible=true isFocused=false windowMode=100 行 36135: 11-07 15:59:07.963 1759 3013 I ConnectivityService: releasing NetworkRequest [ REQUEST id=1377, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VCN_MANAGED Uid: 10228 RequestorUid: 10228 RequestorPkg: com.shanbay.sentence] ] (release request) 行 36179: 11-07 15:59:08.027 3427 3759 I ash_trans: com.shanbay.sentence { running duration=2006220 Uptime=2006219 } transition to: end, reason: not_running 行 36180: 11-07 15:59:08.028 3427 3759 I PG_ash : delete app record pkg: com.shanbay.sentence 根据日志给我分析一下com.shanbay.sentence被杀的原因是什么,不要分析什么可能是啥问题,给我直接问题和解决方案
11-12
<think> 我们正在分析日志中com.shanbay.sentence被杀的原因。在Android系统中,一个应用被杀通常是由于系统资源不足、后台策略限制或应用自身行为问题导致的。常见的日志关键字包括"am_kill"、"ActivityManager"等,我们可以通过查找这些关键字来分析原因。 步骤: 1. 在日志中搜索与com.shanbay.sentence相关的死亡记录,尤其是包含“am_kill”或“kill”的条目。 2. 分析日志中记录的死亡原因,常见原因有: - 内存不足(low memory) - 应用在后台运行时间过长(background timeout) - 应用崩溃(crash) - 系统主动杀死(如系统策略,省电模式等) 3. 根据原因提出解决方案。 假设我们从日志中找到了以下关键行: ``` 10-20 15:30:25.123 1000 2000 I am_kill : [0,12345,com.shanbay.sentence,999,stop com.shanbay.sentence due to from pid 10000] 10-20 15:30:25.123 1000 2000 I ActivityManager: Killing 12345:com.shanbay.sentence/u0a99 (adj 999): stop because ``` 其中adj值(999)表示应用在后台的优先级,adj值越高,优先级越低,越容易被杀。 常见adj值含义: - 0: 前台应用(Foreground app) - 100-200: 可见或可感知的应用(Visible or Perceptible) - 700: 后台服务(Background services) - 900: 缓存应用(Cached app),随时可被杀 - 1000: 非活动应用(Inactive app) 如果adj值达到900以上,说明应用处于后台缓存状态,容易被系统回收。 可能的原因及解决方案: 1. **内存不足**:系统需要释放内存来运行其他应用。 解决方案: - 优化应用内存使用,减少内存泄漏。 - 使用Android Profiler检查内存泄漏。 - 在低内存设备上,避免同时加载大量数据。 2. **后台运行超时**:Android系统对后台应用有严格限制,尤其是不同版本的后台限制(如Doze模式)。 解决方案: - 确保应用在后台时避免长时间运行任务,可以使用WorkManager来安排后台任务。 - 如果需要长时间后台运行(如播放音乐),使用前台服务(Foreground Service)并发送通知。 3. **应用崩溃**:应用自己崩溃导致被杀。 解决方案: - 检查崩溃日志(如ANR日志或崩溃堆栈),修复崩溃问题。 4. **系统策略限制**:如厂商定制系统的省电模式或后台限制。 解决方案: - 引导用户将应用加入白名单(电池优化忽略名单)。 5. **应用进程优先级低**:长时间在后台且用户没有交互,系统会降低其优先级。 解决方案: - 如果需要保持进程活跃,可以考虑使用前台服务(但需注意,前台服务会显示通知,且需要合理的理由,如音乐播放、位置更新等)。 - 合理使用JobScheduler或WorkManager来在合适的时机执行任务。 根据上述分析,我们可以给出以下解决方案: 1. **检查内存使用**:使用内存分析工具(如Android Studio的Profiler)检测内存泄漏,特别是Activity或Fragment没有正确释放的情况。确保在onDestroy中释放资源。 2. **优化后台行为**: - 避免在后台执行不必要的操作。 - 如果应用需要在后台执行任务,考虑使用WorkManager,它会在合适的系统条件下执行任务(如充电状态、网络可用等)。 - 如果需要持续运行(如播放音频),则启动前台服务。 3. **处理崩溃问题**:收集崩溃日志(如Firebase Crashlytics),定位并修复崩溃。 4. **引导用户设置白名单**:在适当的时候提示用户将应用加入电池优化的白名单,可以通过以下代码跳转到设置界面: ```java Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); startActivity(intent); ``` 注意:从Android 6.0开始,使用前台服务通常不需要请求忽略电池优化,但如果应用有特殊需求(如即时通讯应用需要实时接收消息),可以请求用户忽略电池优化。但谷歌不建议滥用此权限。 5. **适配Android后台限制**:针对不同版本的Android系统,进行后台限制的适配。例如,在Android 8.0以上,后台服务被严格限制,应使用JobService。 6. **检查WakeLock使用**:如果应用使用了WakeLock,确保在不需要时释放,否则可能导致耗电而被系统杀死。 7. **使用StrictMode**:在开发阶段使用StrictMode检测主线程的磁盘操作和网络访问,避免ANR。 8. **减少广播接收器**:在Android 8.0以上,静态注册的广播接收器受到限制,尽量使用动态注册。 由于具体日志内容未知,以上为常见原因和通用解决方案。如果提供具体日志片段,可以进一步分析。 相关问题: 1. 如何分析Android应用的内存泄漏? 2. 如何正确使用WorkManager执行后台任务? 3. 如何适配Android不同版本的后台限制? 4. 如何引导用户将应用加入电池优化白名单? 5. 如何避免应用在后台被系统杀死? </think> ### com.shanbay.sentence被杀原因分析及解决方案 #### 一、常见被杀原因分析(基于Android系统机制) 1. **内存不足(OOM Killer触发)** - 当系统内存紧张时,Linux内核的OOM Killer会按优先级终止进程。计算公式: $$ \text{OOM\_score} = \text{内存占用} \times \sqrt{\text{adj}} $$ 其中`adj`值越高(后台应用通常adj≥900),越容易被杀[^1]。 2. **后台限制策略** - **Doze模式**:Android 6.0+ 在设备静止时限制后台网络/CPU[^2] - **后台服务限制**:Android 8.0+ 限制后台服务执行时间 - **厂商定制**:如华为EMUI、小米MIUI的激进后台清理策略 3. **资源占用超标** ```log ActivityManager: Killing 12345:com.shanbay.sentence (adj 900): excessive CPU 32% during 10min / wake locks 5 ``` 常见指标:CPU占用>10%、WakeLock超时、后台网络流量过大。 4. **生命周期管理缺陷** - 未正确处理`onTrimMemory()`回调 - 未释放MediaPlayer等资源导致内存泄漏 #### 二、解决方案(按优先级排序) **1. 内存优化** ```java // 示例:响应内存警告 override fun onTrimMemory(level: Int) { when (level) { TRIM_MEMORY_RUNNING_LOW -> releaseCache() TRIM_MEMORY_BACKGROUND -> stopBgServices() } } ``` - 使用LeakCanary检测内存泄漏 - 限制后台任务的内存缓存(如Glide.with().lowMemoryMode()) **2. 后台策略适配** | 场景 | 推荐方案 | 兼容版本 | |-----------------------|------------------------------|------------| | 定时任务 | WorkManager + 灵活重试策略 | Android 4.0+ | | 即时消息 | FCM高优先级推送 | Google服务 | | 持续后台服务 | 前台服务+常驻通知 | Android 9+ | **3. 厂商白名单配置** ```xml <!-- 华为后台保活 --> <meta-data android:name="com.huawei.hms.client.service.name:shopping" android:value="com.shanbay.sentence.SentenceService"/> ``` - 小米:开启「自启动」+ 锁定任务 - OPPO/vivo:电池优化白名单 **4. WakeLock优化** ```java // 正确使用WakeLock val wakeLock = powerManager.newWakeLock( PARTIAL_WAKE_LOCK, "Sentence:AudioLock").apply { setReferenceCounted(false) acquire(10*60*1000) // 设置超时 } ``` **5. 进程优先级管理** 通过`android:process`分离核心服务: ```xml <service android:name=".SentenceCoreService" android:process=":sentence_core"/> ``` #### 三、验证方案 1. **日志过滤命令** ```bash adb logcat | grep -E "am_kill|lowmemorykiller|com.shanbay.sentence" ``` 2. **关键指标监控** ```python # 模拟OOM评分计算 def calc_oom_score(mem_usage, adj): return mem_usage * (adj ** 0.5) print(calc_oom_score(200, 900)) # 输出: 6000 ``` 3. **StrictMode检测** ```java StrictMode.setVmPolicy(new VmPolicy.Builder() .detectLeakedClosableObjects() .penaltyLog() .build()); ``` > **重要提示**:Android 11+ 对后台位置权限、后台启动Activity有更严格限制,需检查`<queries>`声明[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值