代码之家  ›  专栏  ›  技术社区  ›  Dinamo

Android服务不再运行(已销毁)

  •  0
  • Dinamo  · 技术社区  · 1 年前

    我写了一个前台服务,等待 BroadcastReceiver 消息,并且需要一直运行,突然它停止在上列出 正在运行服务 列表来自 开发人员选项 菜单

    我试着看看它是否崩溃了 logcat -d ,但我没有看到任何与车祸有关的东西, 我列出了正在运行的服务,使用 adb shell dumpsys activity services 我看到它被“摧毁”了:

      * Destroy ServiceRecord{6b2e72 u0 com.example.app/.sService}
        intent={cmp=com.example.app/.sService}
        packageName=com.example.app
        processName=com.example.app
        baseDir=/data/app/~~wscZKhRzebY3lv8rAajnEg==/com.example.app-4lu3b318SWPa20lceICa4w==/base.apk
        dataDir=/data/user/0/com.example.app
        app=null
        allowWhileInUsePermissionInFgs=false
        recentCallingPackage=com.example.app
        recentCallingUid=10715
        allowStartForeground=DENIED
        startForegroundCount=1
        infoAllowStartForeground=null
        createTime=-1d13h45m41s79ms startingBgTimeout=--
        lastActivity=-4h2m40s651ms restartTime=-- createdFromFg=false
        startRequested=false delayedStop=false stopIfKilled=false callStart=false lastStartId=3
        destroying=true destroyTime=-4h2m40s647ms
        restartCount=0 restartDelay=-4h2m40s742ms nextRestartTime=-4h2m40s742ms crashCount=0
    

    为什么它被摧毁,或者我如何才能弄清楚它是如何被摧毁的?如何防止它被破坏?

    谢谢

    更新 -注释相关代码:

    public void onCreate(){
        super.onCreate();
        ...
        ...
        startForeground(102, notification);
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        registerReceiver(smsReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
    
        return START_STICKY;
    }
    
    0 回复  |  直到 1 年前