带有睡眠手机的Android服务从5.1和8.0开始,但不是从7.0开始

时间:2018-07-16 17:07:19

标签: java android alarmmanager background-service

我开发了一个带有AlarmManager的应用程序,每天运行8.30,以激活地理定位服务几个小时。 在配备Android 5.1的手机中以及在配备Android 8.0的手机中均可完美激活 但是在使用Android 7.0的系统中,它仅在屏幕打开时启动服务。否则,它将无法打开,也不会启动服务。 我尝试了很多替代方法,但没有收到错误消息。它只有在打开屏幕后才能开始。 感谢您的帮助!!

清单

<service android:name=".Services.LocationService" android:icon="@drawable/pin" android:label="@string/service_name"> </service> <receiver android:name=".MyWakefulReceiver" /> <receiver android:name=".AlarmReceiver" />

public class AlarmReceiver extends BroadcastReceiver {
        private static final String TAG = "AlarmReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
            //Toast.makeText(context, "AlarmReceiver extends BroadcastReceiver" , Toast.LENGTH_LONG).show();
            Log.i("TAG","inicializado");
            Intent serviceIntent = new Intent(context, LocationService.class);
            serviceIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
            LocationService.IS_SERVICE_RUNNING = true;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(serviceIntent);
            } else {
                context.startService(serviceIntent);
            }

 public class MyWakefulReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Toast.makeText(context, " MyWakefulReceiver extends WakefulBroadcastReceiver  " , Toast.LENGTH_LONG).show();
            Log.i("TAG","inicializado");
            Intent servicea = new Intent(context, LocationService.class);
            servicea.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
            startWakefulService(context, servicea);

和Gradle

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "sistema.com.aps"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:26.+'
    compile 'com.google.android.gms:play-services-location:11.0.4'
    compile 'com.squareup.okhttp3:okhttp:3.9.0'
    compile 'com.android.support:cardview-v7:26.0.0-alpha1'
    compile 'com.android.support:support-v4:26.0.0-alpha1'
    compile 'com.android.support:design:26.0.0-alpha1'
}

0 个答案:

没有答案