2010-08-20 26 views
10

我正在編寫一個Android應用程序,它必須始終在後臺運行,直到用戶從應用程序的菜單中退出。但現在我注意到,在一段時間後,我的應用在沒有用戶干預的情況下自行停止。如何讓Android應用程序無限期運行?

任何想法如何確保我的應用程序將始終在後臺運行?

+1

它停止它的時候內存已經結束,它需要另一個應用程序 – 2010-08-20 09:59:38

+2

請考慮用已接受的答案標記一些問題,以便其他人可以從中受益。 – 2011-05-05 20:46:08

回答

0

爲了您的活動,清單中的XML,放:

機器人:執着= 「真」

+1

從[documentation](http://developer.android.com/guide/topics/manifest/application-element.html#persistent): 應用程序通常不應該設置此標誌;持久性模式僅用於某些系統應用程序。 – 2011-05-05 20:44:55

+1

我的答案在技術上是準確的,符合海報對「必須始終在後臺運行」的要求。 – 2011-05-05 23:58:04

+12

設置持久性[僅適用於系統映像附帶的應用程序](https://groups.google.com/d/msg/android-developers/2E10ZSnaK2Q/csXFYPchqcIJ)。 – 2011-05-06 04:17:53

7

如果需要始終保持運行,尋找到一個ServicestartForeground。如果您可以讓Service死亡但重新啓動,請查看onStartCommandSTART_STICKY

+0

我正在做同樣的事情。在nexus 5中工作良好。但是,在我的應用程序正在查殺時,xiaomi服務正在被殺死。 – Abhi 2016-04-07 09:58:37

0

AndroidMainfest.xml看起來是這個presistent =真:

<?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> 
+1

僅適用於系統映像附帶的應用程序。請參閱http://stackoverflow.com/questions/3529834/how-to-keep-a-android-app-always-running#comment6799178_5811609 – 2014-05-22 15:01:25

0

「雖然應用程序開發文檔不解釋 機器人的角色:執着,使用該屬性預留給應用程序, 是在AOSP內建成「

- 嵌入式Android的

相關問題