如何保持报警管理运行的应用程序关闭后也?

问题描述 投票:0回答:1

我想在我的MainActivity.java文件的AlarmManager这将触发IntentService每一分钟。

我写的代码是:

public void scheduleAlarm() {
    Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
    final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, MyAlarmReceiver.REQEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    long firstMillis = System.currentTimeMillis();
    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 60000, pendingIntent);
}

这是我的IntentService:

public class MyTestService extends IntentService {

    public MyTestService() {
        super("MyTestService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("tag_tag_tag", "Service running");

        String filename = "example.txt";
        String fileContents = "\ndone ";
        FileOutputStream fos = null;

    try {
            fos = openFileOutput(filename, MODE_APPEND);
            fos.write(fileContents.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
       }

    }
}

在这里,我只是追加到一个文件只是为了检查,如果应用程序被关闭后运行服务,而事实并非如此。

这是我的接收器:公共类MyAlarmReceiver扩展广播接收器{

    public static final int REQEST_CODE = 12345;
    public static final String ACTION = "com.always_in_beta.bekar";

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MyTestService.class);
        i.putExtra("foo", "bar");
        context.startService(i);
    }
}

我想这个服务被触发在应用程序关闭后连背景的每一分钟。

android alarmmanager intentservice
1个回答
0
投票

AlarmManager的使用是非常不可靠的,尤其是从版本的Android M起。报警事件将通过电话打盹模式忽略。我用火力地堡JobDispatcher https://github.com/firebase/firebase-jobdispatcher-android,为了这个目的,提供很大的。代码:摇篮:

implementation 'com.firebase:firebase-jobdispatcher:0.8.5'

工作类:

import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;

public class MyTestService extends JobService {
@Override
public boolean onStartJob(JobParameters job) {
    // something like
 Log.d("tag_tag_tag", "Service running");

    String filename = "example.txt";
    String fileContents = "\ndone ";
    FileOutputStream fos = null;

try {
        fos = openFileOutput(filename, MODE_APPEND);
        fos.write(fileContents.getBytes());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
   }

    return false; // Answers the question: "Is there still work going on?"
}

@Override
public boolean onStopJob(JobParameters job) {
    return false; // Answers the question: "Should this job be retried?"
}

}

表现:

<service
android:exported="false"
android:name=".MyTestService">
<intent-filter>
    <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>

在您的活动,创建调度:

// Create a new dispatcher using the Google Play driver.
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));

然后安排工作:

Job myJob = dispatcher.newJobBuilder()
.setService(MyTestService.class) // the JobService that will be called
.setTag("my-unique-tag")        // uniquely identifies the job
.setRecurring(true)
.setTrigger(Trigger.executionWindow(1*60-10,1*60+10))//1 minute +/- 10 seconds
.build();

dispatcher.mustSchedule(myJob);

这应该工作,至少它为我工作。

© www.soinside.com 2019 - 2024. All rights reserved.