好的,我已经尝试了 AlarmManager 的两个示例——一个来自 commonsware 网站,一个来自 manning 网站。我目前正在使用的代码来自曼宁网站:[ http://unlocking-android.googlecode.com/svn/chapter8/trunk/SimpleAlarm/][1]
有两个类,AlarmReceiver 和 GenerateAlarm。任何人都知道为什么吐司不会在模拟器中显示?我在想这是因为我位于东部时区并且它使用 UTC,但是我摆弄了不同的东西,但它们似乎都不起作用。
public class GenerateAlarm extends Activity {
Toast mToast;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.set_alarm_button);
button.setOnClickListener(this.mOneShotListener);
}
private OnClickListener mOneShotListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(GenerateAlarm.this, AlarmReceiver.class);
PendingIntent appIntent = PendingIntent.getBroadcast(GenerateAlarm.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// calendar.add(Calendar.MINUTE, 1);
calendar.add(Calendar.SECOND, 10);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), appIntent);
if (GenerateAlarm.this.mToast != null) {
GenerateAlarm.this.mToast.cancel();
}
GenerateAlarm.this.mToast = Toast.makeText(GenerateAlarm.this, R.string.alarm_message, Toast.LENGTH_LONG);
//GenerateAlarm.this.mToast.show();
}
};
}
public class AlarmReceiver extends BroadcastReceiver {
public void onReceiveIntent(Context context, Intent intent) {
Toast.makeText(context, R.string.app_name, Toast.LENGTH_SHORT).show();
}
@Override
public void onReceive(Context context, Intent intent) {
}
}