我想从我的应用程序创建一个警报对象。我正在写一个To-Do应用程序,它可以选择在手机上设置闹钟。
我想设置日期和时间以及警报的标签。
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);
我尝试使用上面的代码设置闹钟,但我无法。我也没有得到任何错误:(
答案 0 :(得分:12)
正如@stealthcopter所说,AlarmManager用于引发应用程序可以捕获的警报,然后执行某些操作。这是我从其他帖子,教程和我已完成的工作中汇总的一个小例子。
<强> 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秒后在状态栏上创建通知。
希望它有所帮助。
也是先发布在这里:))
哦差点忘了,
<强>的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" />
答案 1 :(得分:0)
这是有效的代码:
添加到Manifest.xml:
...
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
...
<receiver android:process=":remote" android:name="Alarm"></receiver>
...
代码:
public class Alarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show();
wl.release();
}
public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi);
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}