How to keep an Android app running indefinitely?

2020-02-08 18:42发布

I am writing an Android app which must always be running in the background until the user exits it from the app's menu. But now I notice that in Android after some time my app is stopped by itself without user intervention.

Any idea how to make sure my app will always be running in the background?

5条回答
趁早两清
2楼-- · 2020-02-08 18:49

“While the app development documentation does explain the role of android:persistent, the use of that attribute is reserved for apps that are built within the AOSP.”

– Embedded Android

查看更多
冷血范
3楼-- · 2020-02-08 18:55

AndroidMainfest.xml looks like this with 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>
查看更多
Deceive 欺骗
4楼-- · 2020-02-08 19:00

For your activity, in the manifest xml, put:

android:persistent="true"

查看更多
唯我独甜
5楼-- · 2020-02-08 19:06

If you need to run at all times, look into a Service and startForeground. If you can let your Service die but get restarted, look into onStartCommand and START_STICKY.

查看更多
登录 后发表回答