AlarmMananger 似乎没有执行 - 为什么?

发布于 2024-12-20 15:21:58 字数 2727 浏览 4 评论 0原文

我有以下代码来触发警报,当警报触发时,意味着启动另一个基本上是午睡警报的活动。经过谷歌和SO的大量搜索后,我不知所措。我相信我对 AlarmManager 有正确的设置,但它应该启动的 Activity 从未启动。

顺便说一句,最终我想使用 napTime 变量来设置 AlarmManager 触发的时间,但出于测试目的,我只是试图让它触发正道。

这是设置小睡的代码。 Log.e("Nap time", "" + napTime); 行始终显示为napTime 选择的正确数字,因此我知道该方法正在执行。

private void setNap(int napTime) {
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);       
        Intent i = new Intent(getApplicationContext(), NapAlarm.class);
//      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);     
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);

        Log.e("Nap time", "" + napTime);

        TextView nap = (TextView) findViewById(R.id.nap);
        nap.setText("nap set");
    }

以及启动的活动的代码:

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NapAlarm extends Activity implements OnClickListener {

    MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm_layout);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

        Button dButton = (Button) findViewById(R.id.dismissButton);
        dButton.setOnClickListener(this);

        Button sButton = (Button) findViewById(R.id.snoozeButton);
        sButton.setOnClickListener(this);

        new Thread(new Runnable() {

            public void run() {
                try {

                    Log.e("Got this far", "In the alarm");

                    mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
                    mp.start();
                    mp.setLooping(true);

                } catch (Exception e) {
                }
            }
        }).start();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.dismissButton:
            mp.stop();
            this.finish();
            break;

        case R.id.snoozeButton:
            Button sButton = (Button) findViewById(R.id.snoozeButton);
            sButton.setText("I'm sorry, this app currently does not support snoozing, you must wake up.");
            break;
        }
    }
}

如果我单独运行第二段代码,它会工作并且会播放声音,等等。但是如果我从 AlarmMananger 的较大项目中运行它> 应该创建一个警报来触发它,但它从未发生。

有什么我忽略的事情吗?

I have the following code to trigger an alarm, which when it fires is meant to launch another activity that is basically a nap alarm. After much searching through Google and SO, I am at a loss. I believe I have the correct setup for the AlarmManager, but the Activity that it is supposed to launch never gets launched.

By the way, eventually I want to use the napTime variable to set the time at which the AlarmManager will fire, but for testing purposes I am just trying to get it to fire right way.

Here is the code to set the nap. The Log.e("Nap time", "" + napTime); line always shows the correct number that was selected for the napTime, so I know that the method is executing.

private void setNap(int napTime) {
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);       
        Intent i = new Intent(getApplicationContext(), NapAlarm.class);
//      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);     
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);

        Log.e("Nap time", "" + napTime);

        TextView nap = (TextView) findViewById(R.id.nap);
        nap.setText("nap set");
    }

And the code for the activity that is launched:

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NapAlarm extends Activity implements OnClickListener {

    MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm_layout);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

        Button dButton = (Button) findViewById(R.id.dismissButton);
        dButton.setOnClickListener(this);

        Button sButton = (Button) findViewById(R.id.snoozeButton);
        sButton.setOnClickListener(this);

        new Thread(new Runnable() {

            public void run() {
                try {

                    Log.e("Got this far", "In the alarm");

                    mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
                    mp.start();
                    mp.setLooping(true);

                } catch (Exception e) {
                }
            }
        }).start();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.dismissButton:
            mp.stop();
            this.finish();
            break;

        case R.id.snoozeButton:
            Button sButton = (Button) findViewById(R.id.snoozeButton);
            sButton.setText("I'm sorry, this app currently does not support snoozing, you must wake up.");
            break;
        }
    }
}

If I run the second piece of code by itself, it works and the sound plays, etc. But if I run it from within the larger project from which the AlarmMananger is supposed to create an alarm to trigger it, it never happens.

Is there something that I am overlooking?

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

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

发布评论

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

评论(1

找个人就嫁了吧 2024-12-27 15:21:58

您的 NapTime 活动应该像这样扩展广播接收器。

public class NapAlarm extends BroadcastReceiver {

@Override
 public void onReceive(Context context, Intent intent) {
//Here do your naptime code.

//YOu could actually have your NapTime class launch from this reciever if you want.
}

}

确保您像这样注册接收器。

<receiver  android:process=":remote" android:name="NapAlarm"></receiver>

请参阅此处了解更多信息

报警管理器教程

Your NapTime activity should be extending a BroadCast Receiver like this..

public class NapAlarm extends BroadcastReceiver {

@Override
 public void onReceive(Context context, Intent intent) {
//Here do your naptime code.

//YOu could actually have your NapTime class launch from this reciever if you want.
}

}

Make sure you register your reciever like this..

<receiver  android:process=":remote" android:name="NapAlarm"></receiver>

Refer here for more info

Alarm Manager tutorial

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