在 Activities android studio 之间发送数据时出现问题

Problems with sending data between Activities android studio

我尝试编写关闭警报应用程序,但在活动之间发送数据时遇到问题。在 main activity 中,我打开 next activity 来设置闹钟。设置闹钟后,我想发送回主 activity 字符串,其中包含闹钟何时开始的信息。这是我的代码:

开幕式Activity:

  public void addActivity(View view) {
                intent = new Intent(this, Time_Picker.class);
                startActivityForResult(intent, REQUEST_GET_TIME);
                }

** 设置闹钟(我发送变量 intent_string,意图转到 AlarmReciver class)**

alarm_on.setOnClickListener(new View.OnClickListener(){
            @TargetApi(Build.VERSION_CODES.M)
            @Override
            public void onClick(View v){

                ind = true;

                calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
                calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());

                int hour = timePicker.getCurrentHour();
                int minute = timePicker.getCurrentMinute();

                String hourString = String.valueOf(hour);
                String minuteString = String.valueOf(minute);

                if(minute<10){
                    minuteString="0"+String.valueOf(minute);
                }

                intent.putExtra("extra", "on");

                temp = select_item +" - " +hourString+":"+minuteString;

                intent_string = getIntent();

                intent_string.putExtra("temp", temp);
                setResult(RESULT_OK, intent_string);





                pendingIntent = PendingIntent.getBroadcast(Time_Picker.this,
                        0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                        pendingIntent);



                finish();

    }
});

接收字符串:

 @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.e("dziala:", "");

        if (requestCode == 1){
            if(resultCode == RESULT_OK){

                String text = data.getStringExtra("temp");


                Log.e("dziala:", text);
            }
        }
    }

在 运行 之后,这个警报开始工作,但我似乎应用程序从未在 onActivityResult 函数中(应该从日志中获取信息,但我什么也没得到)。

这是一个完整的例子:

这是打开定时器的代码,并根据所选时间设置闹钟:

 TimePickerDialog basic_timer_timePickerDialog;


private void Timer(boolean is24r){
    Calendar calendar = Calendar.getInstance();
    Log.d(TAG, "EnableTimer Called");
    basic_timer_timePickerDialog2 = new TimePickerDialog(
            getActivity(),
            onTimeSetListener2,
            calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE),
            is24r);
    basic_timer_timePickerDialog2.setTitle(getResources().getString(R.string.enable_alarm));
    basic_timer_timePickerDialog2.show();

}



TimePickerDialog.OnTimeSetListener onTimeSetListener2
    = new TimePickerDialog.OnTimeSetListener(){

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();

    calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calSet.set(Calendar.MINUTE, minute);
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);

    if(calSet.compareTo(calNow) <= 0){
        //Today Set time passed, count to tomorrow
        calSet.add(Calendar.DATE, 1);
    }

    Timer alarm = new Timer();
    alarm.setAlarm(calSet,getActivity());
}};

计时器Class:

    public void setAlarm(Calendar targetCal, Context context){

    String AlarmSetToText = context.getResources().getString(R.string.AlarmSetToText);

    String TimeSetTo = AlarmSetToText + " " + targetCal.getTime();

    Toast.makeText(context, TimeSetTo, Toast.LENGTH_LONG).show();

    Intent intent = new Intent(context, TimerEnable.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_INT, intent, 0);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

}