Android永无止境的服务

时间:2018-10-19 12:22:17

标签: android background task

我知道关于此主题有很多问题和答案,但是尽管如此,我仍无法实现在Android 7.0设备上运行的永无止境的任务。它可以在模拟器上(使用API​​级别24)完美运行,但不能在真实设备(小米Redmi Note 4,Android 7.0 NRD90M)上正常运行。例如,在模拟器上,当我关闭应用程序时,将按预期正确调用LocationUpdateService.onDestroy和AutoStartLocationUpdate.onReceive方法。但是在真实设备上却没有。

还是应该改用其他方法,例如“警报管理器”?

有人可以给我一些提示吗?谢谢

这是我的实现方式:

服务:

package com.ivan.archangel;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

public class LocationUpdateService extends Service {

    private Timer timer;

    @Override
    public void onCreate() {
        super.onCreate();
        timer = new Timer();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.i("Ivan", "tick");
            }

        }, 0, 1000);
        Log.i("Ivan", "Timer started");
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
        Log.i("Ivan", "Timer stopped");

        Intent broadcastIntent = new Intent("Hahaha");
        getApplicationContext().sendBroadcast(broadcastIntent);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

BroadcastReceiver

package com.ivan.archangel;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AutoStartLocationUpdate extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("Ivan", "Restarting LocationUpdateService...");
        context.startService(new Intent(context, LocationUpdateService.class));
        Log.i("Ivan", "Restarted LocationUpdateService");
    }

}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ivan.archangel">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Home"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
        <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="@string/fb_login_protocol_scheme" />
            </intent-filter>
        </activity>

        <service
            android:name=".LocationUpdateService"
            android:enabled="true"
            android:stopWithTask="false"/>

        <receiver
            android:name=".AutoStartLocationUpdate"
            android:enabled="true"
            android:exported="true"
            android:label="RestartServiceWhenStopped">
            <intent-filter>
                <action android:name="Hahaha" />
                <!--<action android:name="android.intent.action.BOOT_COMPLETED" />-->
            </intent-filter>
        </receiver>
        <receiver
            android:name=".SMSReceiver"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter android:priority="5822">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps"></activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

您还可以通过调用startForeground()的{​​{1}}中的onStartCommand来实现 Forground Service

Service class

链接

Service Limitation DOC

Google Developer Link

Foreground Service Implementation