本文介绍了如何通过报警值,报警接收机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发一个示例应用程序报警。我被搜查谷歌和SC,大部分的例子困惑我。我与我的code做,但为什么会失败报警值传递给报警接收机

请帮助我。感谢您的关注。

下面是我的code。

ReminderFragment.java

 公共类ReminderFragment扩展片段{按钮buttonstartSetDialog;
TextView的txt_time;上下文CTX;最终静态INT RQS_1 = 1;公共ReminderFragment(){}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){    视图V = inflater.inflate(R.layout.fragment_reminder,集装箱,FALSE);    txt_time =(TextView中)v.findViewById(R.id.txt_time);    buttonstartSetDialog =(按钮)v.findViewById(R.id.startSetDialog);
    buttonstartSetDialog.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            //textAlarmPrompt.setText();
            showTimePicker();
        }});    返回伏;
}私人无效showTimePicker()
{
     // DatePickerFragment日期=新DatePickerFragment();
     TimePickerFragment时间=新TimePickerFragment();      台历挂历= Calendar.getInstance();
      日历calSet =(日历)calendar.clone();      捆绑ARGS =新包();      args.putInt(小时,calendar.HOUR_OF_DAY);
      args.putInt(月,calendar.get(的Calendar.MONTH));
      args.putInt(分,calendar.get(Calendar.MINUTE));
      time.setArguments(参数);      time.setCallBack(准时);
      time.show(getFragmentManager(),时间选择器);
}
OnTimeSetListener导通时间=新OnTimeSetListener()
{    公共无效onTimeSet(TimePicker观点,诠释HOUROFDAY,分整型){
        // TODO自动生成方法存根
        txt_time.setText(将String.valueOf(HOUROFDAY)+:+将String.valueOf(分钟));        意向意图=新意图(getActivity(),AlarmReceiver.class);
        的PendingIntent的PendingIntent = PendingIntent.getBroadcast(getActivity(),RQS_1,意向,0);
        AlarmManager alarmManager =(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP,txt_time.getTimeInMillis(),的PendingIntent);    }
}; }

AlarmReceiver.java

 公共类AlarmReceiver扩展广播接收器{
私人MediaPlayer的mMediaPlayer;@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    Toast.makeText(背景下,时间到了!!!!,Toast.LENGTH_LONG).show();
    //震动移动电话
    振动器振动=(振动器)context.getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(2000);
}
}

TimePickerFragment.java

 公共类TimePickerFragment扩展DialogFragment {    OnTimeSetListener onTimeSet;    公共TimePickerFragment(){}
    公共无效setCallBack(OnTimeSetListener准时)
    {
          onTimeSet =准时;
    }
     @燮pressLint(NewApi)
    私人诠释小时,分钟;
    公共无效setArguments(捆绑参数)
    {
          super.setArguments(参数);
          小时= args.getInt(小时);
          分钟= args.getInt(分);     }     公共对话框onCreateDialog(捆绑savedInstanceState)
     {
          返回新TimePickerDialog(getActivity(),onTimeSet,小时,分钟,FALSE);
     }
 }


解决方案

您需要选择的时间转换成long值(毫秒)

使用下面的code做到这一点。

 日历日历= Calendar.getInstance();
calendar.set(Calendar.HOUR,HOUROFDAY);
calendar.set(Calendar.MINUTE,分钟);
calendar.set(Calendar.AM_PM,Calendar.PM); //设置上午或下午
长timeInMillis = calendar.getTimeInMillis();

,然后将该值传递timeInMillis在

  alarmManager.set(AlarmManager.RTC_WAKEUP,timeInMillis,的PendingIntent);

I tried to develop a sample Alarm Application. I was searched Google and SC, most of the examples confusing me. I have done with my code, but why it failed to pass the alarm value to the Alarm receiver.

Please help me. Thank you for your concern.

Here is my code.

ReminderFragment.java

public class ReminderFragment extends Fragment {

Button buttonstartSetDialog;
TextView txt_time;  

Context ctx;

final static int RQS_1 = 1;

public ReminderFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_reminder, container, false);

    txt_time = (TextView) v.findViewById(R.id.txt_time);

    buttonstartSetDialog = (Button) v.findViewById(R.id.startSetDialog);
    buttonstartSetDialog.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            //textAlarmPrompt.setText("");
            showTimePicker();           
        }});

    return v;
}

private void showTimePicker()
{
     //DatePickerFragment date = new DatePickerFragment();
     TimePickerFragment time= new TimePickerFragment();

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

      Bundle args = new Bundle();

      args.putInt("hour", calendar.HOUR_OF_DAY);
      args.putInt("month", calendar.get(Calendar.MONTH));
      args.putInt("minute", calendar.get(Calendar.MINUTE));
      time.setArguments(args);

      time.setCallBack(ontime);
      time.show(getFragmentManager(), "Time Picker");


}


OnTimeSetListener ontime = new OnTimeSetListener() 
{

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub
        txt_time.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));

        Intent intent = new Intent(getActivity(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, txt_time.getTimeInMillis(), pendingIntent);

    }
};

 }

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {
private MediaPlayer mMediaPlayer;

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Time is up!!!!.",Toast.LENGTH_LONG).show();
    // Vibrate the mobile phone
    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(2000);


}
}

TimePickerFragment.java

    public class TimePickerFragment extends DialogFragment {

    OnTimeSetListener onTimeSet;

    public TimePickerFragment() {}


    public void setCallBack(OnTimeSetListener ontime) 
    {
          onTimeSet = ontime;
    }
     @SuppressLint("NewApi")
    private int hour, minute;
    public void setArguments(Bundle args) 
    {
          super.setArguments(args);
          hour = args.getInt("hour");
          minute = args.getInt("minute");

     }

     public Dialog onCreateDialog(Bundle savedInstanceState) 
     {
          return new TimePickerDialog(getActivity(), onTimeSet, hour, minute, false);
     }
 }  
解决方案

You need to convert the selected time in long value (milliseconds)

use the below code to do this.

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.AM_PM, Calendar.PM); // set AM or PM
long timeInMillis = calendar.getTimeInMillis();

and then pass this value timeInMillis in

alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);

这篇关于如何通过报警值,报警接收机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 07:13