从我的应用程序设置警报

从我的应用程序设置警报

本文介绍了从我的应用程序设置警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建从我的应用程序中的报警对象。我写一个待办事项应用程序,这将有一个选项,以在手机上设定闹铃。

I want to create an alarm object from my application. I am writing a To-Do application which will have an option to set an Alarm on the phone.

我想设定日期和时间,也为报警的标签。

I wanna set the Date and Time and also the Label for the alarm.

Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
        c.clear();
        c.set(Calendar.YEAR, mYear);
        c.set(Calendar.MONTH, mMonth);
        c.set(Calendar.DAY_OF_MONTH, mDay);
        c.set(Calendar.HOUR, mHour);
        c.set(Calendar.MINUTE, mMinute);
        Intent activate = new Intent(this, alaram.class);
        AlarmManager alarams ;
        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, activate, 0);
        alarams = (AlarmManager) getSystemService(this.ALARM_SERVICE);
        alarams.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), alarmIntent);

我尝试使用上面的code设置报警,但我不能。我没有得到任何错误之一:(

I tried using the above code to set the alarm but am not able to. I dont get any error either :(

推荐答案

由于@stealthcopter所说,AlarmManager用来发出警报您的应用程序可以捕获,然后做一些事情。这里是一个小例子,我扔了来自其他职位,教程和工作,我已经做到了。

As @stealthcopter said, the AlarmManager is used to raise an Alarm your application can catch and then do something. Here is a little example I threw together from other posts, tutorials, and work I've done.

Main.java

Intent i = new Intent(this, OnAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i,
                                                     PendingIntent.FLAG_ONE_SHOT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + 10);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);

OnAlarmReceiver.java

public class OnAlarmReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    WakeIntentService.acquireStaticLock(context);
    Intent i = new Intent(context, AlarmService.class);
    context.startService(i);
}}

WakeIntentService.java

public abstract class WakeIntentService extends IntentService {

    abstract void doReminderWork(Intent intent);

    public static final String LOCK_NAME_STATIC = "com.android.voodootv.static";
    private static PowerManager.WakeLock lockStatic = null;

    public static void acquireStaticLock(Context context) {
        getLock(context).acquire();
    }

    synchronized private static PowerManager.WakeLock getLock(Context context) {
        if (lockStatic == null) {
            PowerManager powManager = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);
            lockStatic = powManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    LOCK_NAME_STATIC);
            lockStatic.setReferenceCounted(true);
        }
        return (lockStatic);
    }

    public WakeIntentService(String name) {
        super(name);
    }

    @Override
    final protected void onHandleIntent(Intent intent) {
        try {
            doReminderWork(intent);
        } finally {
            getLock(this).release();
        }
    }}

AlarmService.java

public class AlarmService extends WakeIntentService {

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

    @Override
    void doReminderWork(Intent intent) {
        NotificationManager manager = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(this, Main.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0,
                notificationIntent, PendingIntent.FLAG_ONE_SHOT);
        Notification note = new Notification(R.drawable.icon, "Alarm",
                System.currentTimeMillis());
        note.setLatestEventInfo(this, "Title", "Text", pi);
        note.defaults |= Notification.DEFAULT_ALL;
        note.flags |= Notification.FLAG_AUTO_CANCEL;
        int id = 123456789;
        manager.notify(id, note);
    }
}

这个例子将在10秒后创建状态栏上的通知。

This example will create a notification on the status bar after 10 seconds.

希望它帮助。

下面还有第一篇文章:)

Also First post here :)

哦差点忘了,

AndroidManifest.xml中

<receiver android:name="com.android.alarmmanagertest.OnAlarmReceiver" />
<service android:name="com.android.alarmmanagertest.AlarmService" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />

这篇关于从我的应用程序设置警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:39