BroadcastReceiver无法使用警报管理器

时间:2014-02-28 19:54:45

标签: android broadcastreceiver

我认为这个问题是自我解释的。 我尝试使用谷歌搜索,并遇到了各种解决方案但没有工作..

这对我有用。 如果我没有设置广播接收器,则警报管理器可以正常工作,但如果我设置广播接收器则不起作用。 我之前曾使用过广播接收器。

我知道有两种设置广播的方法,如此处所述

Android alarm not working

我试过两个......

如果有人想要我可以在这里发布的代码

public class Reminder extends Activity{
    DatePicker dp;int month;
    TimePicker tp;int year;long millisecond;
    Button ok;String data2;int hour;int datee;int minutes;
    BroadcastReceiver alarmReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();          
        }
    };


    public static final String ACTION_NAME = "com.helloworld.MYACTION";
    private IntentFilter myFilter = new IntentFilter(ACTION_NAME);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminder);
        Intent in = getIntent();
         registerReceiver(alarmReceiver, myFilter);

        dp = (DatePicker) findViewById(R.id.datePicker1);
        tp = (TimePicker) findViewById(R.id.timePicker1);
        ok = (Button) findViewById(R.id.ok);
        ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                  year = dp.getYear();
                  datee = dp.getDayOfMonth();
                  month = dp.getMonth();
                  hour = tp.getCurrentHour();
                  minutes = tp.getCurrentMinute();// day//mm//yy//min//hr
            conversion_shit();
            }});        
}

    protected void conversion_shit() {
        int xhour;
        String xx;
        if(hour > 12)
        {
          xhour=     (hour)-12;
          xx="AM";

        }   
        else
        {  xx="PM";  xhour=(hour);
        }
        int monthint=(month);

        String date= String.valueOf(datee)+"/"+monthint+"/"+String.valueOf(year)+ " " +String.valueOf(xhour)+":"+String.valueOf(minutes)+":00 " + xx ;

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
        Date convertedDate = new Date();

            try {
                convertedDate = dateFormat.parse(date);
                  millisecond = convertedDate.getTime();

            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
            Log.i(String.valueOf(millisecond),convertedDate.toString());

             Intent myIntent = new Intent(ACTION_NAME);
             PendingIntent   pendingIntent = PendingIntent.getService(Reminder.this, 0, myIntent, 0);

                        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(System.currentTimeMillis());
                        calendar.add(Calendar.SECOND, 10);
                        alarmManager.set(AlarmManager.RTC_WAKEUP, millisecond, pendingIntent);

                        Toast.makeText(Reminder.this, "Start Alarm", Toast.LENGTH_LONG).show();


    }








    }

1 个答案:

答案 0 :(得分:1)

PendingIntent.getService用于服务,顾名思义。

对于BroadcastReceiver,请使用PendingIntent.getBroadcast