1

我是 Android 新手,如果能提供任何帮助,我将不胜感激。

我正在尝试使用警报管理器,以便它会在未来由和小部件BroadcastReceiver设置的特定时间调用我的类。DatePickerTimePicker

但是,我无法让广播接收器类执行其功能。

下面是我放入广播接收器类的清单文件中的代码,用于从操作系统接收广播消息:

    <receiver android:name="androidapp.scheduler.MyBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.MYBROADCASTRECEIVER" >
            </action>
        </intent-filter>
    </receiver>

下面是我在 Next 按钮的 onclicklistener 中放入的代码,以便通知操作系统通话时间。

            TimePicker myTp = (TimePicker)findViewById(R.id.tpTime);
            cal.set(Calendar.YEAR,myDatePicker.getYear() );
            cal.set(Calendar.MONTH, myDatePicker.getMonth());
            cal.set(Calendar.DAY_OF_MONTH, myDatePicker.getDayOfMonth());
            cal.set(Calendar.HOUR, myTp.getCurrentHour());
            cal.set(Calendar.MINUTE, myTp.getCurrentMinute());
            cal.set(Calendar.SECOND, 0);

            Log.i("SCheduling", cal.getTime().toString());

            Intent intent = new Intent(Scheduling.this, MyBroadcastReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(Scheduling.this, 234324243, intent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

            Intent nextForm = new Intent(this,Sender.class);
            nextForm.putExtra("contacts", contacts);
            startActivity(nextForm);    
            Log.i("SCheduling", "everything set");

最后,MybroadcastReceivers类代码如下所示:

public class MyBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("brreceiver", "BroadcastReciever");
        Toast.makeText(context, "schedule set!.",
                Toast.LENGTH_LONG).show();
    }
}

我在这里想念什么?

4

2 回答 2

0

看来代码是正确的,问题出在模拟器上。我仍然不知道模拟器出了什么问题,但我再次测试了代码并且它有效并且我确实收到了日志的输出。

于 2013-05-22T03:16:47.687 回答
0

在以下语句中将 Scheduling.this 更改为 this.getApplicationContext()

PendingIntent pendingIntent = PendingIntent.getBroadcast(Scheduling.this, 234324243, intent, 0);
于 2013-05-21T09:51:01.713 回答