在 Android 中从服务启动 Activity [英] Start Activity from Service in Android

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

问题描述

安卓:

public class LocationService extends Service {

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        startActivity(new Intent(this, activity.class));
    }
}

我从Activity

Activity中如果条件满足则开始

In Activity if condition satisfies start

startService(new Intent(WozzonActivity.this, LocationService.class));

从我上面提到的 LocationService 无法启动 Activity,我怎样才能在服务类中获取当前运行的 Activity 的上下文?

from my LocationService mentioned above could not launch Activity, how can I get context of current running Activity in service class?

推荐答案

从 Service 类内部:

From inside the Service class:

Intent dialogIntent = new Intent(this, MyActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);

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

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