【问题标题】:How to keep an Android app running indefinitely?如何让 Android 应用程序无限期运行?
【发布时间】:2016-02-10 11:41:50
【问题描述】:

我正在编写一个必须始终在后台运行的 Android 应用程序,直到用户从应用程序的菜单中退出它。但现在我注意到,在 Android 中一段时间​​后,我的应用会在没有用户干预的情况下自行停止。

知道如何确保我的应用始终在后台运行吗?

【问题讨论】:

  • 当内存用完时它会停止它并且它需要它用于另一个应用程序
  • 请考虑用可接受的答案标记您的一些问题,以便其他人可以从中受益。

标签: android service


【解决方案1】:

【讨论】:

    【解决方案2】:

    如果您需要一直运行,请查看ServicestartForeground。如果您可以让您的Service 死掉但重新启动,请查看onStartCommandSTART_STICKY

    【讨论】:

    • 我也在做同样的事情。在nexus 5中工作正常。但是在小米中,只要我的应用程序被杀死,服务就会被杀死。
    【解决方案3】:

    对于您的活动,在清单 xml 中,输入:

    android:persistent="true"

    【讨论】:

    • 来自documentation:应用程序通常不应设置此标志;持久性模式仅适用于某些系统应用程序。
    • 我的回答在技术上是准确的,符合发帖人“必须始终在后台运行”的要求。
    • 非常好。 “某些系统应用程序”不再与该信息含糊不清。感谢您清理它 - 我今天学到了一些新东西。
    【解决方案4】:

    AndroidMainfest.xml 在 presistent=true 时看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.test"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="7"
            android:targetSdkVersion="7" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" android:persistent="true">
            <activity
                android:name="com.example.test.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    【讨论】:

    【解决方案5】:

    “虽然应用程序开发文档确实解释了 android:persistent,该属性的使用是为应用程序保留的 是在 AOSP 中构建的。”

    – 嵌入式安卓

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2020-04-26
      • 1970-01-01
      • 2017-07-01
      相关资源
      最近更新 更多