如何在 Android 中使用 AlarmManager 启动 Activity? [英] How do you start an Activity with AlarmManager in Android?

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

问题描述

关于这个问题,我已经翻阅了十几篇教程和论坛的答案,但仍然无法将一些可以工作的代码放在一起.我会尽量让问题简单明了:

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:

如何使用 AlarmManager(在 Android API 中)在给定时间启动 Activity?这个问题的任何解决方案都可以.

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.

(省略了导入.我希望 MyActivity 在程序打开后 3 秒启动,但事实并非如此.没有错误消息可言.)

(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.

请注意:我已经在清单中找到了 myReceiver

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

推荐答案

万一其他人偶然发现这个 - 这里有一些工作代码(在 2.3.3 模拟器上测试):

In case someone else stumbles upon this - here's some working code (Tested on 2.3.3 emulator):

public final void setAlarm(int seconds) {
    // create the pending intent
    Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
    // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,
            intent, 0);
    // get the alarm manager, and scedule an alarm that calls the receiver
    ((AlarmManager) getSystemService(ALARM_SERVICE)).set(
            AlarmManager.RTC, System.currentTimeMillis() + seconds
                    * 1000, pendingIntent);
    Toast.makeText(MainActivity.this, "Timer set to " + seconds + " seconds.",
            Toast.LENGTH_SHORT).show();
}

public static class AlarmReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Log.d("-", "Receiver3");
    }
}

AndroidManifest.xml:

AndroidManifest.xml:

    <receiver android:name="com.example.test.MainActivity$AlarmReceiver" >
    </receiver>

BenLambell 的代码问题:

Issues with BenLambell's code :

  • 要么:
    • 将接收器移动到它自己的 .java 文件或
    • 将内部类设为静态 - 以便可以从外部访问
    • 如果它是 MainActivity 中的内部类,请使用:<receiver android:name="package.name.MainActivity$AlarmReceiver" ></receiver>
    • 如果它在一个单独的文件中:<receiver android:name="package.name.AlarmReceiver" ></receiver>

    如果您打算在接收器的 onReceive 中显示一个对话框(像我一样):这是不允许的 - 只有活动才能启动对话框.这可以通过对话活动来实现.

    If your intention is to display a dialog in the receiver's onReceive (like me): that's not allowed - only activities can start dialogs. This can be achieved with a dialog activity.

    您可以使用 AlarmManager 直接调用活动:

    You can directly call an activity with the AlarmManager:

    Intent intent = new Intent(MainActivity.this, TriggeredActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    ((AlarmManager) getSystemService(ALARM_SERVICE)).set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + seconds * 1000, pendingIntent);
    

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

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