在活动android studio之间发送数据的问题

时间:2016-01-30 15:51:45

标签: android alarm timepicker

我尝试写出报警应用程序,我在活动之间发送数据时遇到问题。在主要活动中,我打开下一个设置闹钟的活动。设置闹钟后,我希望发回主活动字符串,其中包含警报将在什么时间开始的信息。这是我的代码:

开业活动:

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

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

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函数中(应该从日志中获取信息,我什么也得不到)。

1 个答案:

答案 0 :(得分:0)

以下是一个完整的例子:

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

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

计时器类:

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

}