首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我怎么才能做一个能在屏幕外工作的Android闹钟应用程序呢?

我怎么才能做一个能在屏幕外工作的Android闹钟应用程序呢?
EN

Stack Overflow用户
提问于 2018-06-14 08:25:18
回答 2查看 108关注 0票数 -2

我可以通过创建一个时间线程(AsyncTask)来创建一个安卓定时器。

然而,当片段被停止/或销毁并重新启动/重新创建时,

时间线程仍在运行,但它将无法动态更改屏幕UI,并且片段页面将被重置。

如果您可以通过创建asyncTask线程来创建计时器,则问题是用户离开屏幕并返回后,时间不会更新。

你怎么才能创建一个以任何方式工作的Android应用程序呢?

EN

回答 2

Stack Overflow用户

发布于 2018-06-14 09:33:21

首先,您必须创建一个广播接收器,该接收器将在闹钟时间更改和电话启动时触发。

AndroidManifest.xml

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <receiver android:name="AlarmBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
        </intent-filter>
    </receiver>
    <receiver android:name="AlertBroadcastReceiver"/>

此广播接收器将触发一个用于计算警报时间的服务,并将在警报时间完成时处理操作。

public class AlarmBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent serviceIntent = new Intent(context, AlarmService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            context.startForegroundService(serviceIntent);
        else
            context.startService(serviceIntent);
    }
}

在服务类中。您应该以毫秒为单位计算报警时间,并使用alarm Manager Class设置它。此外,您还应该设置一个PendingIntent,用于在闹钟时间到来时启动一个操作。

Intent startIntent = new Intent(getBaseContext(), AlertBroadcastReceiver.class);
startIntent.putExtra("alarm", alarmContent);
startIntent.putExtra("type", "start");

PendingIntent pendingIntentStart = PendingIntent.getBroadcast(getBaseContext(), 0, startIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManagerStart = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    alarmManagerStart.set(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
}
else if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT  && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    alarmManagerStart.setExact(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if(getAlarmTime(alarmArrayNd, "start").getTimeInMillis() >= 900000)
        alarmManagerStart.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
    else
        alarmManagerStart.setExact(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
}

在本例中,方法getAlarmtime()是一个用于计算警报时间(以毫秒为单位)的方法,我在intent中创建了一个额外的方法,它是任意的,你可以放入任何你想要的东西。

票数 0
EN

Stack Overflow用户

发布于 2018-06-14 14:50:22

非常感谢我通过不破坏fragment对象来解决这个问题。我认为获取警报管理器事件会产生很多开销。所以这不是最好的方法,但是我编辑了我的源代码,不破坏fragment对象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50848014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档