Android 闹钟管理器

发布于 2024-09-13 23:08:35 字数 1919 浏览 13 评论 0原文

好的,我已经尝试了两个 AlarmManager 示例 - 一个来自 commonsware 网站,另一个来自 manning 网站。 我目前正在使用的代码来自 manning 网站: [http://unlocking-android.googlecode.com/svn/chapter8/trunk/SimpleAlarm/][1]

有两个类:AlarmReceiver 和GenerateAlarm。有人知道为什么 toast 不会在模拟器中显示吗?我以为这是因为我位于东部时区并且使用 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) {

}
}

Ok, I've tried two examples of AlarmManager- one from the commonsware website, and one from the manning website.
The code I am currently working with is from the manning website : [http://unlocking-android.googlecode.com/svn/chapter8/trunk/SimpleAlarm/][1]

There are two classes, AlarmReceiver and GenerateAlarm. Anyone have any idea why the toast will not display in the emulator? I was thinking that it was because I am located in the Eastern Time Zone and it uses UTC, but I have fiddled with different things and none of them seem to work.

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

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2024-09-20 23:08:35

您必须在 onReceived 方法中添加 toast,您应该在其中添加对收到的意图的处理。

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, R.string.app_name, Toast.LENGTH_SHORT).show();
}

onReceiveIntent 不是 broadcastreceiver 的方法

公共摘要无效 onReceive
(Context上下文,Intent意图)

自:API 级别 1 此方法是
当广播接收器被调用时
接收 Intent 广播。

You have to add your toast in the onReceived method where you should add the handling of the intent received.

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, R.string.app_name, Toast.LENGTH_SHORT).show();
}

onReceiveIntent is not a method of broadcastreceiver

public abstract void onReceive
(Context context, Intent intent)

Since: API Level 1 This method is
called when the BroadcastReceiver is
receiving an Intent broadcast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文