你如何开始与AlarmManager Android中的活动? [英] How do you start an Activity with AlarmManager in Android?

查看:120
本文介绍了你如何开始与AlarmManager Android中的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了十几个教程和论坛上回答这个问题倒,但还是没有能够得到一些工作code一起。我会尽量保持这个问题很简单:

如何使用AlarmManager(在Android API)在给定时间启动一个活动?任何解决这一问题的都行。

我最近实现这一尝试是如下。

(进口省略了。我希望MyActivity开始3秒钟程序打开后,它并没有,有说话的没有错误消息。)

 公共类AndroidTest2Activity延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        上下文的背景下=这一点; // getApplicationContext();

        AlarmManager经理=(AlarmManager)getSystemService(Context.ALARM_SERVICE); // 正确
        意向意图=新的意图(背景下,myReceiver.class); // 正确
        PendingIntent未决= PendingIntent.getBroadcast(上下文,0,意图,0); // 正确
        manager.set(AlarmManager.RTC,System.currentTimeMillis的()+ 3000,待定); // 正确

        的setContentView(R.layout.main);
    }
}

公共类myReceiver扩展的BroadcastReceiver {
    公共无效的onReceive(上下文的背景下,意图意图){
        意图I =新的意图(背景下,myActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(ⅰ);
    }
}

公共类myActivity延伸活动{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.d(,难以捉摸的成功);
        的setContentView(R.layout.main);
    }
}
 

任何意见将是AP preciated。

请注意:我有myReceiver在清单中已

解决方案
  

如何使用AlarmManager(在Android API)在给定时间启动一个活动?

提供一个 PendingIntent 设置()呼叫标识活动启动。或者,你在做什么,这应该只是罚款。

此示例项目是有点复杂的,因为它是19教程深入到我的书之一,但如果你看一下类,如编辑preferences OnBootReceiver OnAlarmReceiver ,你会发现你正在使用上述相同的基本配方。在这种情况下,我可以只使用一个 getActivity() PendingIntent ,但经过这一次的教程给用户开展活动或显示通知,所以的BroadcastReceiver 更有意义。一个选择

查找除了在LogCat中错误的警告。最有可能的,你的接收器或活动是不是在你的清单。

需要注意的是弹出一个活动了中间的地方一般是不是一个好主意。从书中引用自己的问题:

  

通过一个全屏幕的活动肯定的作品显示了午餐时间报警,   并且如果用户看着屏幕,它会引起他们的注意。然而,   它也是相当的破坏性,如果他们碰巧使用手机正确的,   瞬间。举例来说,如果他们输入文字信息,而驾驶,你   报警活动弹出无处可够分散​​他们   造成事故。所以,在公众安全的利益,我们应该给用户一个选项   有一个更微妙的方式来提醒他们吃午饭。

I've poured through a dozen tutorials and forum answers about this problem, but still haven't been able to get some working code together. I'll try to keep the question straightforward:

How do you use AlarmManager (in the Android API) to start an Activity at a given time? Any solution to this problem will do.

My latest attempt to achieve this is below.

(Imports omitted. I expect MyActivity to start 3 seconds after the program is opened, which it doesn't. There are no error messages to speak of.)

public class AndroidTest2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Context context = this;//.getApplicationContext();

        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); // CORRECT
        Intent intent = new Intent(context, myReceiver.class); // CORRECT
        PendingIntent pending = PendingIntent.getBroadcast( context, 0, intent, 0 ); // CORRECT
        manager.set( AlarmManager.RTC, System.currentTimeMillis() + 3000, pending ); // CORRECT

        setContentView(R.layout.main);
    }
}

public class myReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Intent i=new Intent(context, myActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

public class myActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("", "Elusive success");
        setContentView(R.layout.main);
    }
}

Any advice would be appreciated.

Please note: I've got myReceiver in the manifest already

解决方案

How do you use AlarmManager (in the Android API) to start an Activity at a given time?

Supply a PendingIntent to the set() call that identifies the activity to start up. Or, do what you're doing, which should work just fine.

This sample project is a bit elaborate, because it's 19 tutorials deep into one of my books, but if you look at classes like EditPreferences, OnBootReceiver, and OnAlarmReceiver, you will see the same basic recipe that you're using above. In this case, I could have just used a getActivity() PendingIntent, but the tutorial after this one gives the user a choice of launching an activity or displaying a Notification, so a BroadcastReceiver makes more sense.

Look for warnings in addition to errors in LogCat. Most likely, your receiver or activity is not in your manifest.

Note that popping up an activity out of the middle of nowhere is generally not a good idea. Quoting myself from the book in question:

Displaying the lunchtime alarm via a full-screen activity certainly works, and if the user is looking at the screen, it will get their attention. However, it is also rather disruptive if they happen to be using the phone right that instant. For example, if they are typing a text message while driving, your alarm activity popping up out of nowhere might distract them enough to cause an accident. So, in the interest of public safety, we should give the user an option to have a more subtle way to remind them to have lunch.

这篇关于你如何开始与AlarmManager Android中的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆