2010-07-17 8 views
36

Cho đến nay và nhờ trang web này, tôi đã có thể thiết lập báo thức sẽ được thiết lập và hoạt động, ngay cả khi tôi bật điện thoại.Tôi làm cách nào để thiết lập nhiều báo thức trong Android?

Bây giờ, tôi thiết lập một báo động để hiển thị một lời nhắc nhở cho sự kiện A và tôi cần các ứng dụng để thiết lập báo động khác để hiển thị khác nhắc nhở cho sự kiện B.

Tôi phải làm gì đó sai, bởi vì nó chỉ kích hoạt lời nhắc cho sự kiện A. Có vẻ như khi đã thiết lập xong, bất kỳ báo thức nào khác đều được hiểu là báo thức giống nhau. :-(

Dưới đây là chi tiết về những gì tôi đang làm theo hai bước:

1) Từ một hoạt động để thiết lập một báo động rằng tại thời điểm nhất định và ngày sẽ gọi một máy thu

   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) Từ nhận tôi gọi một dịch vụ

 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); 

Cảm ơn trước,

monn3t

Trả lời

76

Ok, khi bạn đặt một PendingIntent, bạn phải gán cho nó một ID duy nhất cho nó, trong trường hợp bạn muốn xác định nó sau này (giúp chỉnh sửa/hủy nó)

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(). 

Mã Yêu cầu là ID đó.

Trong mã của bạn, bạn tiếp tục đặt lại SAME PendingIntent, thay vì sử dụng Mã yêu cầu khác nhau mỗi lần.

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

Nó phải là một số nguyên, tôi giả sử bạn có một primaryid (ItemID) có thể xác định Alarm A từ Alarm B.

+0

Cảm ơn bạn đã đánh cắp ... những gì bạn đề nghị đã làm các trick. Tôi đã không trả lời trước bởi vì tôi muốn đảm bảo rằng nó hoạt động đúng ... Một lần nữa cảm ơn bạn, monn3t – monn3t

+0

@stOle: Xin hãy giúp tôi cho điều này cũng: http: // stackoverflow. com/questions/8665021/android-multiple-alarm-not-working/8665978 # 8665978 –

+1

@ st0le: tôi có thể nhận được báo thức cho ngày và giờ khác nhau nhưng tất cả đều được phát sóng cùng một thông điệp. Làm thế nào để xử lý?Tôi muốn đặt thông báo khác nhau cho các báo thức khác nhau. . . –

1

Bạn có thể thiết lập nhiều báo động bằng cách cung cấp đang yêu cầu khác nhau trong pendingIntent.getBroadcast (......)

Cách tiếp cận mà tôi sử dụng để cài đặt nhiều báo thức là tôi đã tạo một báo thức. Tôi đã khởi tạo một số nguyên tĩnh trong lớp cài đặt cảnh báo sẽ được tăng lên mỗi lần từ hoạt động chính của tôi bất cứ khi nào tôi nhấp vào nút "thêm báo thức" trong hoạt động chính của mình. Ví dụ:

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);