2011-05-14 56 views
6

我想從我的應用程序創建鬧鐘對象。我正在寫一個待辦事項應用程序,它可以在手機上設置鬧鐘。從我的應用程序設置鬧鐘

我想設置日期和時間以及警報的標籤。

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

它需要的權限在manifest文件?因爲我沒有在logCat中得到任何錯誤,你可以指出我應該給予什麼准許 – 2011-05-14 10:41:58

+0

如果你不設置年,月和日,此代碼有效。也許在那裏的問題。 – Sver 2011-05-14 10:47:37

+0

@Sver can not set date?報警? – 2011-05-14 10:53:07

回答

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

了不起的第一篇文章。謝謝。 – 2013-05-10 05:19:04

+1

不要使用此代碼它有很多錯誤,我浪費了我的時間 – 2015-01-07 16:23:06

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); 
    } 
} 
+1

對不起 - 爲什麼你在_AlarmManager_接收器中使用喚醒鎖? – 2013-05-09 21:27:25