如何从警报管理器调用活动的功能

发布于 2025-01-06 15:23:39 字数 2087 浏览 3 评论 0原文

我在活动中有一个 startApplicationService 方法。因此我有一个警报管理器。 startApplicationService 方法正在调用后台服务。我想在警报管理器的 onReceive 方法中使用/调用该方法。怎么做呢?请帮我。 :)

已编辑:添加了警报管理器类:

public class WatcherAlarm  extends BroadcastReceiver
{

    private final static String LOG_TAG = WatcherAlarm.class.getSimpleName();
    private AccessActivity activity = null;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d(LOG_TAG, "-------------------------- WATCHER ALARM ------ ON RECEIVE");
        if(activity != null) {
            activity.startApplicationService(intent.getExtras());
        }
    }

    public void startAlarm(AccessActivity activity, Bundle bundle)
    {
        this.activity = activity;
        AlarmManager alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(activity, WatcherAlarm.class); // explicit
        i.putExtras(bundle);
        PendingIntent pi = PendingIntent.getBroadcast(activity, 0, i, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 20, pi); // Millisec * Second * Minute
    }

    public void stopAlarm(Context context)
    {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, WatcherAlarm.class); // explicit
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        alarmManager.cancel(pi);
    }
}

这是我的 startApplicationService 方法:它正在启动 AsyncTask。

public void startApplicationService(Bundle bundle)
{
    try {
        task = new ApplicationServiceTask(this);
        requestId = task.execute(bundle).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

我尝试从上下文中获取活动。但这是不可能的。 :(。有没有办法从警报管理器中调用 Activity 的 startApplicationService 方法

谢谢您的每一个建议

。Zeck

I have a startApplicationService method in an activity. Therefore I have an alarm manager. startApplicationService method is calling background service. I want to use/call that method in onReceive method from my alarm manager. How to do it? Please help me. :)

EDITED: Added alarm manager class:

public class WatcherAlarm  extends BroadcastReceiver
{

    private final static String LOG_TAG = WatcherAlarm.class.getSimpleName();
    private AccessActivity activity = null;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d(LOG_TAG, "-------------------------- WATCHER ALARM ------ ON RECEIVE");
        if(activity != null) {
            activity.startApplicationService(intent.getExtras());
        }
    }

    public void startAlarm(AccessActivity activity, Bundle bundle)
    {
        this.activity = activity;
        AlarmManager alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(activity, WatcherAlarm.class); // explicit
        i.putExtras(bundle);
        PendingIntent pi = PendingIntent.getBroadcast(activity, 0, i, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 20, pi); // Millisec * Second * Minute
    }

    public void stopAlarm(Context context)
    {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, WatcherAlarm.class); // explicit
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        alarmManager.cancel(pi);
    }
}

And here is the my startApplicationService method: It's starting AsyncTask.

public void startApplicationService(Bundle bundle)
{
    try {
        task = new ApplicationServiceTask(this);
        requestId = task.execute(bundle).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

I'm tried get activiy from context. But it's not possible. :(. Is there any way to say call startApplicationService method to activity from alarm manager?

Thank you for every advice.

Zeck

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

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

发布评论

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

评论(1

紫轩蝶泪 2025-01-13 15:23:39

不...因为您无法保证您尝试调用的 Activity 在调用 onReceive() 时会存在。

您可以做的是使用 Intent 启动该 Activity,并使用 Extra 指示调用者是您 BroadcastReceiver。然后 Activity 可以检查 Extra 并调用该方法。

但是,考虑一下您似乎想要做什么。我建议使用服务。由于您无论如何都在后台进行工作,因此我看不出想要在活动中执行此操作的原因(当然,除非您有一个此处未显示的正当理由)。

查看我的答案这里有一个类似的例子。

No... because you have no guarantee that the Activity you are trying to call will even exist when your onReceive() is called.

What you can do is start that Activity using an Intent with an Extra indicating that the the caller is you BroadcastReceiver. The Activity can then check that Extra and call the method.

However, considering what you appear to want to do. I would recommend going with a Service. Since you are doing the work in the background anyways, I don't see a reason for wanting to do it in an Activity (unless, of course, you have a valid reason that does not show here).

See my answer here for an example of something similar.

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