1

我的程序有问题。我创建了一个 PendingIntent 并通过 AlarmManager 广播它。现在我想从第二次打开程序时传递给 PendingIntent 的意图中获取额外内容。所以请帮助我得到这个。这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.example.silentmodeapp.R.layout.fajar);
    tpStartTime = (TimePicker) findViewById(com.example.silentmodeapp.R.id.tpStartTime);
    tpEndTime = (TimePicker) findViewById(com.example.silentmodeapp.R.id.tpEndTime);
    setPrayerTime = (Button) findViewById(com.example.silentmodeapp.R.id.btsetTime);
    cancelPrayerTime = (Button) findViewById(com.example.silentmodeapp.R.id.btcancelTime);
    rbCheck=(RadioButton) findViewById(com.example.silentmodeapp.R.id.rbCheck);

//Here I want to get the Intent to use info in it.///////////////////////////////////////////////

    if((PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_NO_CREATE) != null)){
        PendingIntent startIntent=(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_NO_CREATE));
        rbCheck.setChecked(true);
    }else {
        rbCheck.setChecked(false);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(
            com.example.silentmodeapp.R.menu.activity_main, menu);
    return true;
}

public void setPrayerTime(View v) {
    try {

        Calendar calendar = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();

        // Set Time For Ending And Starting Time.
        calendar.set(Calendar.HOUR_OF_DAY, tpStartTime.getCurrentHour());
        calendar.set(Calendar.MINUTE, tpStartTime.getCurrentMinute() - 1);
        calendar2.set(Calendar.HOUR_OF_DAY, tpEndTime.getCurrentHour());
        calendar2.set(Calendar.MINUTE, tpEndTime.getCurrentMinute() - 1);

        intent.setClass(this, SetSilentService.class);
        intent.putExtra("start", "Silent Mode Started!");
        intent2.setClass(this, RemoveSilentService.class);
        intent2.putExtra("ended", "Silent Mode Removed!");

        mp = MediaPlayer.create(Fajar.this,
                com.example.silentmodeapp.R.raw.set_time);
        mp.start();
        pendingIntent = PendingIntent.getService(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        pendingIntent2 = PendingIntent.getService(this, 1, intent2,
                PendingIntent.FLAG_UPDATE_CURRENT);

        alarmManager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager1.set(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), pendingIntent);
        alarmManager1.set(AlarmManager.RTC_WAKEUP,
                calendar2.getTimeInMillis(), pendingIntent2);

        toast.makeText(
                this,
                "Time is set to " + tpStartTime.getCurrentHour() + ":"
                        + tpStartTime.getCurrentMinute() + " To "
                        + tpEndTime.getCurrentHour() + ":"
                        + tpEndTime.getCurrentMinute(), Toast.LENGTH_SHORT)
                .show();
        rbCheck.setChecked(true);

        // startService(intent2);
    } catch (Exception e) {
        toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();

    }

}
4

0 回答 0