2010-07-17 13 views
36

अभी तक और इस वेबसाइट के लिए धन्यवाद, मैं एक अलार्म स्थापित करने में सक्षम हूं जो स्थापित और सक्रिय होगा, भले ही मैं अपना फोन चालू कर दूं।मैं एंड्रॉइड में एकाधिक अलार्म कैसे सेट कर सकता हूं?

अब, मैं घटना एक के लिए एक चेतावनी दिखाने के लिए एक अलार्म की स्थापना की और मैं सेटअप करने के लिए आवेदन एक और अलार्म घटना बी के लिए एक और अनुस्मारक दिखाने की जरूरत है

मैं कुछ गलत कर किया जाना चाहिए, क्योंकि यह केवल घटना ए के लिए अनुस्मारक को आग लगती है। ऐसा लगता है कि एक बार स्थापित होने पर, कोई अन्य अलार्म एक जैसा समझा जाता है।

1) एक गतिविधि से मैं अलार्म सेट है कि कुछ समय और तिथि पर एक रिसीवर

   Intent intent = new Intent(Activity_Reminder.this, 
         AlarmReceiver_SetOnService.class); 

       intent.putExtra("item_name", prescription 
         .getItemName()); 
       intent 
         .putExtra(
           "message", 
           Activity_Reminder.this 
             .getString(R.string.notif_text)); 
       intent.putExtra("item_id", itemId); 
       intent.putExtra("activityToTrigg", 
         "com.companyName.appName.main.Activity_Reminder"); 

       PendingIntent mAlarmSender; 

       mAlarmSender = PendingIntent.getBroadcast(
         Activity_Reminder.this, 0, intent, 0); 

       long alarmTime = dateMgmt.getTimeForAlarm(pickedDate); 
       Calendar c = Calendar.getInstance(); 
       c.setTimeInMillis(alarmTime); 
       // Schedule the alarm! 
       AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
       am.set(AlarmManager.RTC_WAKEUP, alarmTime + 15000, 
         mAlarmSender); 

कॉल करेगा:

यहाँ :-(क्या मैं दो चरणों में कर रहा हूँ के विस्तार है 2) रिसीवर से मैं एक सेवा को कॉल

 Bundle bundle = intent.getExtras(); 
     String itemName = bundle.getString("item_name"); 
     String reminderOrAlarmMessage = bundle.getString("message"); 
     String activityToTrigg = bundle.getString("activityToTrigg"); 
     int itemId = Integer.parseInt(bundle.getString("item_id")); 
     NotificationManager nm = (NotificationManager) context.getSystemService("notification"); 
     CharSequence text = itemName + " "+reminderOrAlarmMessage; 
     Notification notification = new Notification(R.drawable.icon, text, 
       System.currentTimeMillis()); 
     Intent newIntent = new Intent(); 
     newIntent.setAction(activityToTrigg); 
     newIntent.putExtra("item_id", itemId); 
     CharSequence text1= itemName + " "+reminderOrAlarmMessage; 
     CharSequence text2= context.getString(R.string.notif_Go_To_Details); 
     PendingIntent pIntent = PendingIntent.getActivity(context,0, newIntent, 0); 
     notification.setLatestEventInfo(context, text1, text2, pIntent); 
     notification.flags = Notification.FLAG_AUTO_CANCEL; 
     notification.defaults = Notification.DEFAULT_ALL; 
     nm.notify(itemId, notification); 

अग्रिम धन्यवाद,

monn3t

उत्तर

76

ठीक है, जब आप एक PendingIntent निर्धारित करते हैं, आप इसे यह करने के लिए एक अद्वितीय ID आवंटित करने के लिए (/ इसे रद्द करने से संशोधित करने के लिए) चाहिए रहे हैं, बैठाना आप इसे बाद में की पहचान करना चाहते

static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) 
//Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). 
static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags) 
//Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast(). 

अनुरोध कोड है वह आईडी

अपने कोड में, आप को रीसेट कर रहे हैं लंबित इंटेन्टेंट, इसके बजाय प्रत्येक बार एक अलग अनुरोधकोड का उपयोग करें।

PendingIntent pIntent = PendingIntent.getActivity(context,uniqueRQCODE, newIntent, 0); 

यह एक पूर्णांक हो गया है, मैं तुम्हें एक primaryid (Itemid) कि अलार्म बी से अलार्म एक की पहचान कर सकते हैं

+0

आप चुरा लिया ... आप क्या सुझाव चाल किया धन्यवाद। क्योंकि मुझे यकीन है कि यह ठीक से काम कर रहा था बनाना चाहते थे कि मैंने पहले का जवाब नहीं दिया है और यह है ... फिर से धन्यवाद, monn3t – monn3t

+0

@stOle: http: // stackoverflow कृपया मुझे भी इस एक के लिए मदद करते हैं। कॉम/प्रश्न/8665021/एंड्रॉइड-एकाधिक-अलार्म-काम नहीं कर रहे/8665978 # 8665978 –

+1

@ st0le: मैं अलग-अलग तारीख और समय के लिए अलार्म प्राप्त करने में सक्षम हूं लेकिन वे सभी एक ही संदेश प्रसारित करते हैं। इसे कैसे संभालें?मैं अलग-अलग अलार्म के लिए अलग संदेश सेट करना चाहता हूं। । । –

1

आप में अलग अनुरोध कोड की आपूर्ति करके एकाधिक अलार्म सेट कर सकते हैं लगता है लंबित INTent.getBroadcast (......)

जिस दृष्टिकोण को मैंने कई अलार्म सेट करने के लिए उपयोग किया था वह यह है कि मैंने एक अलार्म बनाया है। मैंने अलार्म सेटिंग क्लास में एक स्थिर पूर्णांक शुरू किया जो हर बार मेरी मुख्य गतिविधि से बढ़ाया जाएगा जब भी मैं अपनी मुख्य गतिविधि में "अलार्म जोड़ें" बटन पर क्लिक करता हूं। ईजी।

MainActivity.java

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

public void addAlarmClick(View v) { 
    AlarmActivity.broadcastCode++; 
    startActivity(new Intent(this, AlarmActivity.class)); 
} 
} 

AlarmActivity.java

public class AlarmActivity extends AppCompatActivity {` 
//........ 
public static int broadcastCode=0; 
//........ 
Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class); 
pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 
          broadcastCode, myIntent, 0);