एंड्रॉयड नमूना कोड
में कुछ अच्छे उदाहरण हैं। \ Android-SDK \ नमूने \ एंड्रॉयड-10 \ ApiDemos \ src \ कॉम \ उदाहरण \ एंड्रॉयड \ apis \ एप्लिकेशन
की जाँच करने के लिए इस प्रकार हैं:
- AlarmController.java
- OneShotAlarm.java
सबसे पहले, आपको एक रिसीवर की आवश्यकता होती है, जो आपके ट्रिगर होने पर आपके अलार्म को सुन सकता है। अपनी AndroidManifest.xml फ़ाइल में निम्नलिखित जोड़ें
<receiver android:name=".MyAlarmReceiver" />
फिर, एक अलार्म को गति प्रदान करने, उसके बाद निम्न वर्ग
public class MyAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show();
}
}
बनाने के लिए, (आपका मुख्य गतिविधि में उदाहरण के लिए) निम्नलिखित का उपयोग करें:
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, 30);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
।
या, बेहतर अभी तक, एक वर्ग है कि यह सब संभालती बनाने के लिए और इस
Bundle bundle = new Bundle();
// add extras here..
MyAlarm alarm = new MyAlarm(this, bundle, 30);
इस तरह से, आप इसे एक ही स्थान पर है की तरह उपयोग (मत भूलना AndroidManifest.xml
संपादित करने के लिए)
public class MyAlarm extends BroadcastReceiver {
private final String REMINDER_BUNDLE = "MyReminderBundle";
// this constructor is called by the alarm manager.
public MyAlarm(){ }
// you can use this constructor to create the alarm.
// Just pass in the main activity as the context,
// any extras you'd like to get later when triggered
// and the timeout
public MyAlarm(Context context, Bundle extras, int timeoutInSeconds){
AlarmManager alarmMgr =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyAlarm.class);
intent.putExtra(REMINDER_BUNDLE, extras);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, timeoutInSeconds);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
pendingIntent);
}
@Override
public void onReceive(Context context, Intent intent) {
// here you can get the extras you passed in when creating the alarm
//intent.getBundleExtra(REMINDER_BUNDLE));
Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show();
}
}
हैलो फिर से। उत्तर के लिए धन्यवाद। अगर मैं आपकी पुस्तक खरीदता हूं तो क्या यह समझाता है कि अलार्म मैनेजर को पूरी तरह से कैसे कार्यान्वित किया जाए? – Tom
उन्नत एंड्रॉइड पुस्तक (संस्करण 0.9) में ~ 9 पेज हैं जो अलार्ममेनगर, वेक लॉक, और बाकी के उदाहरण को कवर करते हैं। यह शायद संस्करण 1.0 में थोड़ा विस्तार करेगा क्योंकि मैं उपरोक्त मेरे उत्तर में जो फिक्स मैंने उल्लेख किया है उसे ठीक करता हूं। और यदि आपके पास पुस्तक या उसके नमूना कोड के बारे में कोई प्रश्न है, तो http://groups.google.com/group/cw-android पर जाएं और मुझे जवाब देने में खुशी होगी। – CommonsWare
किसी भी एंड्रॉइड डेवलपर के पास मार्क की किताबों के लिए सदस्यता होनी चाहिए :) कम से कम एक बार – Bostone