如何从 BroadcastReceiver 启动 Activity

发布于 2024-11-06 04:26:19 字数 2889 浏览 2 评论 0原文

我目前正在开发一款 Android 闹钟应用程序(是的,我知道我不是唯一的一个)。我对 Android 开发相当陌生,但我已经成功完成了应用程序的核心,我需要的只是一件事。能够设置闹钟(在 xyz 时间),一旦闹钟响起,即使手机处于睡眠状态(显然没有关闭)也可以启动活动,剩下的就完成了。

现在我阅读了开发人员指南,我相信要走的路是通过 BroadcastReceiver,然后应该使用 IntentSender 来启动活动,但我似乎无法在哪里找到任何示例或类似的帖子来至少得到一个想法。

现在,我已经让 AlarmManager 真正工作,唤醒活动,但只有在手机完全唤醒时才会工作,如果睡着,或者至少在手机解锁之前什么也不会工作。

有什么建议吗?如果需要可以发布代码示例。提前感谢

更新

@Joel 感谢您的回复。我实际上读过有关 PowerManager.WakeLock 的内容,但很高兴您以这种方式重定向了我。现在,这就是我针对接收活动提出的方案(可能是错误的,但请耐心等待)

public class OnAlarmActivity extends Activity {

    MediaPlayer mpAlarm;
    MediaPlayer mpButton;
    PowerManager.WakeLock wl;

    private BroadcastReceiver theReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
            wl.acquire();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.on_alarm);

        mpAlarm = MediaPlayer.create(this, R.raw.filename);
        mpAlarm.start();
        mpButton = MediaPlayer.create(this, R.raw.buttonfilename);


        ImageView imgForAlarmScreen= (ImageView)findViewById(R.id.oftheimage);
        Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein);
        imgForAlarmScreen.startAnimation(myFadeInAnimation); //animation for ImageView

        Button bNextActivity = (Button)findViewById(R.id.ofthebutton);
        bNextActivity.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mpButton.start();
                startActivity(new Intent("com.myapps.otheractivity"));
            }
        });
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mpAlarm.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mpAlarm.pause();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        mpAlarm.start();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        wl.release();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        mpAlarm.release();
    }
}

现在,除非手机处于活动状态(唤醒),否则它不会唤醒,甚至不会发出声音。关于如何有效使用唤醒锁有什么想法吗?

I am currently working on a alarm application for android (yea i know im not the only one). I'm fairly new to Android Developing but I've managed to complete the core of the App and all i need is 1 thing. To be able to set an alarm (at x-y-z time) and once the alarm goes off, to start an activity EVEN IF PHONE IS ASLEEP(not off obviously), and the rest is done.

Now ive read the developers guide and i believe that the way to go is through a BroadcastReceiver and then should intentSender to the launch the activity but i cant seem where to find any examples or similar posts to atleast get an idea.

Right now Ive got the AlarmManager working actually waking to the activity but will only work if phone is completely awake and nothing at all if asleep, or at least til phone is unlocked.

Any suggestions? If required can post code example. Thanks in advance

Update

@Joel Thanks for the reply man. I had actually read about PowerManager.WakeLock, but glad u redirected me its way. Now this is what ive come up with for the receiving activity(it might be wrong but bear with me)

public class OnAlarmActivity extends Activity {

    MediaPlayer mpAlarm;
    MediaPlayer mpButton;
    PowerManager.WakeLock wl;

    private BroadcastReceiver theReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
            wl.acquire();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.on_alarm);

        mpAlarm = MediaPlayer.create(this, R.raw.filename);
        mpAlarm.start();
        mpButton = MediaPlayer.create(this, R.raw.buttonfilename);


        ImageView imgForAlarmScreen= (ImageView)findViewById(R.id.oftheimage);
        Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein);
        imgForAlarmScreen.startAnimation(myFadeInAnimation); //animation for ImageView

        Button bNextActivity = (Button)findViewById(R.id.ofthebutton);
        bNextActivity.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mpButton.start();
                startActivity(new Intent("com.myapps.otheractivity"));
            }
        });
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mpAlarm.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mpAlarm.pause();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        mpAlarm.start();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        wl.release();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        mpAlarm.release();
    }
}

Now all this and yet it will not awake nor even sound unless phone is active(awake). Any ideas on how i can effectively use the wake lock?

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

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

发布评论

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

评论(1

半枫 2024-11-13 04:26:19

我相信您需要的系统服务是PowerManager.WakeLock。在谷歌或堆栈溢出上搜索这个,你可能会得到你需要的东西。

I believe the system service you need is PowerManager.WakeLock. Search this on google or stack overflow and you will probably get what you need.

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