时间事件监听器 [英] Time Event Listener

查看:149
本文介绍了时间事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:如何我有我的应用程序做一些在特定的时间?特别是在它没有技术上运行时的情况(是可能的)。最佳实践?事件监听器?等等...

My Question: How do I have my application do something at a certain time? Especially in a case when it's not technically running (is that possible). Best practices? Event listener? Etc...

扩展:我是pretty的新Android开发。我想建立一个调度应用程序,它会在pre预定的时间控制手机的某些功能。所以我们可以说我在应用程序安排,从环来改变振动在3:00而现在它是2:00。那么,如何捕捉系统的时候,它变成3点运行的变化?

Expansion: I'm pretty new to Android development. I want to build a scheduling application which would control some features of the phone at pre-scheduled times. So let's say I schedule in the app to change from ring to vibrate at 3:00 and right now it's 2:00. So how do I capture the system time when it turns 3:00 to run that change?

我不知道任何信息是否有用。我只是想我们设计这个之前,我真正进入开发它,所以我在寻找的最佳实践,以及如何有一个事件监听器运行应用程序时,或者是在后台运行或根本不运行(如果这是可能的)。

I'm not sure whether any more information would be useful. I'm just trying to design this right now before I really get into developing it, so I'm looking for best practices and how to have an event listener run when the application is either running in the background or not running at all (if that's possible).

推荐答案

您可以使用 pendingIntent 的BroadcastReceiver 是这样的:

You can use a pendingIntent with a BroadCastReceiver like this:

Calendar cal = Calendar.getInstance();
Intent activate = new Intent(context, Alarm.class);
AlarmManager alarms ;
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, activate, 0);
alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 00);
alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);

然后广播接收器:

Then the BroadCast Receiver:

public class Alarm extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
      MediaPlayer mp = MediaPlayer.create(context, R.raw.ferry_sound);
      mp.start();
      PowerManager pm = (PowerManager)     context.getSystemService(Context.POWER_SERVICE);
      PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
      wl.acquire();
      Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
      long[] s = { 0, 100, 10, 500, 10, 100, 0, 500, 10, 100, 10, 500 };
      vibrator.vibrate(s, -1);
   }
}

不要忘记,包括在这些权限的的Andr​​oidManifest.xml

....
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
....

和应用标签广播接收器:

and broadcast receiver in application tag:

<receiver  android:process=":remote" android:name="Alarm" />

这篇关于时间事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆