到目前为止,感谢这个网站,即使我转向我的手机,我已经能够设置一个闹钟。

现在,我设置了一个警报,向事件A显示一个提醒,我需要应用程序来设置另一个警报以显示另一个提醒,以便为事件b。

我必须做错事,因为它只对事件A发射提醒。似乎一旦设置,任何其他警报都被理解为同一个。: - (

这里是我在两个步骤中做的详细信息:

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

有帮助吗?

解决方案

好的,当您设置一个挂起时,您应该将其分配一个唯一的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().
.

请求代码是该ID。

在代码中,您继续重置相同的 pendingintent,而是每次使用不同的求取代码。

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

必须是一个整数,我假设您有一个原寸( itemid ),可以识别来自agall b的警报a。

其他提示

您可以通过在PendingIntent.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));
}
}
.

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top