2011-03-15 34 views
3

在我的android應用程序中,我想以編程方式設置alaram。 設置鬧鐘工作正常,但我如何應用通知來接收鬧鐘。在鬧鐘接收器上設置通知android

我已經通過開發人員指南中的通知。

請找到代碼。

Calendar cal=Calendar.getInstance(); 
    Intent alaram=new Intent(Alarmmanager.this,GroupsCheckAlarmReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarmmanager.this, 0, alaram,0); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent); 
    cal.setTimeInMillis(System.currentTimeMillis()); 

    sendBroadcast(alaram,"setalaram"); 

,並在廣播接收器

public class GroupsCheckAlarmReceiver extends BroadcastReceiver{ 

@Override 
public void onReceive(final Context context, Intent intent) { 
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show(); 

} 

它工作正常,但設置通知我需要設置通知管理器僅在活動擴展分類工作正常,我該如何使用它接收並通知。

請分享您寶貴的建議。

感謝提前:)

回答

6

我想你可以使用你的onReceivecontext找到NotificationManager,並呼籲notify

@Override 
public void onReceive(Context context, Intent intent) { 
    NotificationManager notifications = (NotificationManager) 
     context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notifications.notify(...); 
} 

如果你有一個Context你可以做任何事情! :)

+0

感謝您的回覆,讓我試試看 – Remmyabhavan

+1

爲什麼不能?你不能做context.getSystemService(...)? –

+1

您可以使用'Activity'完成大部分事情,您還可以使用'Context'完成。 –

0

您可能會發現僅使用BuzzBox SDK安排任務和創建通知很有用。它支持一個crontab字符串來安排重複執行的任務。 BuzzBox SDK Website