繁体   English   中英

在我关闭应用程序或使用 WorkManager、AlarmManager 或 Jobscheduler 重新启动后,任务永远不会执行

[英]Tasks never gets executed after i close the app or reboot using WorkManager, AlarmManager or Jobscheduler

我想要实现的目标:

  1. 无论应用程序被杀死或设备重新启动,该任务都将在后台持续运行。 假设我正在将我的联系人同步到服务器,并且我需要每分钟检查一次是否有新联系人,这对我的任务来说是一个非常重要的要求。
  2. 如果我们 go 到开发人员设置然后“运行服务”这些服务永远不会被杀死。 并在设备重启后不断重启。 例如facebookServicesWhatsApp

我正在使用的物理设备的信息:

Android 10. 
API Level 29

我尝试过的事情:

JobScheduler
WorkManager
Service (startService())
JobIntentService
AlarmManager

我面临的问题:

我已经阅读了很多文章、博客并观看了很多视频,但仍然无法实现我真正想要的。 我也知道如果我将foregroundService 与通知通道一起使用,我的服务将不会被杀死并持续存在,但我不希望这样,因为 facebookServices 必须在没有任何通知通道的情况下运行它们即使在 API 级别 29 上也只是运行后台服务Android O 之后的所有背景限制。

  1. 使用了 AlarmManager 与 BroadcastReceiver 的组合,它尝试在特定时间启动服务。 但是有连续的异常抛出java.lang.IllegalStateException: Not allowed to start service Intent
  2. 试过workManager,它只在应用程序打开而不是当我关闭应用程序时才完成这项工作。 如果我重新启动设备,它也不会重新开始工作。
  3. JobScheduler 也无法准确获取我想要的内容。

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.basicplayer">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyAlarmService"
            android:enabled="true"
            android:exported="true"
            />
        <receiver android:name=".MyAlarmReceiver"
            android:enabled="true"
            android:exported="true"
            >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

MyAlarmReceiver.java

package com.example.basicplayer;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.RequiresApi;

public class MyAlarmReceiver extends BroadcastReceiver {


    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onReceive(Context context, Intent intent) {

        context.startService(new Intent(context, MyAlarmService.class));

    }
}

MyAlarmService.java

package com.example.basicplayer;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.Settings;

import androidx.annotation.Nullable;

public class MyAlarmService extends Service {

    private MediaPlayer mediaPlayer;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mediaPlayer = MediaPlayer.create(this,
                Settings.System.DEFAULT_ALARM_ALERT_URI);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try{
            startService(new Intent(this, MyAlarmService.class));
        }catch (Exception e){
            e.printStackTrace();
        }


    }

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

MainActivity.java

package com.example.basicplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setAlarm();

    }


    private void setAlarm() {
        //getting the alarm manager
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        //creating a new intent specifying the broadcast receiver
        Intent i = new Intent(this, MyAlarmReceiver.class);

        //creating a pending intent using the intent
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);

        //setting the repeating alarm that will be fired every day
        am.setRepeating(AlarmManager.RTC, 10000, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
        Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();
    }

}

你们能帮忙吗?

android新版本对后台运行进程进行了限制。 他们这样做的原因是为了提供电池优化。 我认为你应该使用 workmanager 库。 这是一个很棒的后台进程库。 此外,当设备重新启动时,您可以使用 workmanager 开始工作。 我为您找到了答案: PeriodicWorkRequest not working after device reboot in android oreo

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM