如何在 Android 10 中从后台启动 Activity?

2024-01-30

我正在构建一个 Android 应用程序,我需要从后台启动一个活动。我正在使用 ForegroundStarter 来扩展服务来完成此任务。我有一个活动 Adscreen.class,我需要从前台服务运行它。活动 Adscreen.class 在除 Android 10 之外的所有 Android 版本上都能正常工作(从后台启动)。

ForeGroundStarter.class

public class ForeGroundStarter extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }



    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("sK", "Inside Foreground");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("sK", "Inside Foreground onStartCommand");
        Intent notificationIntent = new Intent(this, Adscreen.class);
        PendingIntent pendingIntent =
                PendingIntent.getActivity(this, 0, notificationIntent, 0);


        Notification notification =
                null;

        //Launching Foreground Services From API 26+

        notificationIntent = new Intent(this, Adscreen.class);
        pendingIntent =
                PendingIntent.getActivity(this, 0, notificationIntent, 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String NOTIFICATION_CHANNEL_ID = "com.currency.usdtoinr";
            String channelName = "My Background Service";
            NotificationChannel chan = null;
            chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
            chan.setLightColor(Color.BLUE);
            chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            assert manager != null;
            manager.createNotificationChannel(chan);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
            notification = notificationBuilder.setOngoing(true)
                    .setSmallIcon(R.drawable.nicon)
                    .setContentTitle("")
                    .setPriority(NotificationManager.IMPORTANCE_MIN)
                    .setCategory(Notification.CATEGORY_SERVICE)
                    .build();
            startForeground(2, notification);


            Intent dialogIntent = new Intent(this, Adscreen.class);
            dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(dialogIntent);
            Log.d("sk", "After startforeground executed");

        }



        else //API 26 and lower
            {
                notificationIntent = new Intent(this, Adscreen.class);
                pendingIntent =
                        PendingIntent.getActivity(this, 0, notificationIntent, 0);

                notification =
                        new Notification.Builder(this)
                                .setContentTitle("")
                                .setContentText("")
                                .setSmallIcon(R.drawable.nicon)
                                .setContentIntent(pendingIntent)
                                .setTicker("")
                                .build();

                startForeground(2, notification);
                Intent dialogIntent = new Intent(this, Adscreen.class);
                dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(dialogIntent);
            }




        return super.onStartCommand(intent, flags, startId);

    }
}

我读到 Android 10 上从后台启动活动有一些限制。此代码似乎不再起作用。https://developer.android.com/guide/components/activities/background-starts https://developer.android.com/guide/components/activities/background-starts

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

在 Android 10 上有从后台启动 Activity 的解决方法吗?


不确定这样做是否正确,但我没有足够的时间(应用程序仅供公司内部使用)。

我使用的权限:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

然后每个用户都必须转到应用程序的设置 -> 权限,然后在高级设置中选中“在其他应用程序上显示应用程序”功能

抱歉我的英语或不完全正确的解决方案,但它有效,对我来说是很好的修补程序。

On Pixel 4 it will be looking like this: enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Android 10 中从后台启动 Activity? 的相关文章

  • 找不到参数的方法 dependencyResolutionManagement()

    我正在尝试使用老师给我的一个项目 但它显示了一个错误 Settings file Users admin AndroidStudioProjects HTTPNetworking settings gradle line 1 A probl
  • 使用workmanager时Firestore脱机持久性错误

    我正在使用一个WorkManger定期从我的中检索信息Firestore当应用程序处于后台和前台时的数据库 此信息用于根据状态更新 UI 因此不同的状态会添加或删除 UI 的不同部分 第一次运行时效果很好 但是 一旦应用程序处于后台并且Wo
  • 在包“android”中找不到属性“backgroundTint”的资源标识符

    我发现了一些视图 xml 属性 例如backgroundTint backgroundTintMode 但是当我使用它作为视图属性定义时 Eclipse 显示错误 No resource identifier found for attri
  • React Native 从 JavaScript 代码内部访问 strings.xml

    有没有办法访问当前值android app src main res values strings xml从 JavaScript 代码内部 我想为每个构建放置不同的端点 URL 但我什至无法检测到反应本机代码内的构建类型 而不必求助于 D
  • Sqlite数据库生命周期?关闭应用程序后它会被删除吗?

    我正在遵循一个简单的教程 该教程创建一个从 SQLiteOpenHelper 扩展的类 并创建一个包含一个表和 5 行的数据库 好的 但我需要更多地了解 android Sqlite 数据库 例如 如果应用程序关闭或手机关机会发生什么 数据
  • SearchView过滤ListView

    我已经实现了搜索视图来过滤我的列表视图项目 当我输入任何文本时 它会过滤列表 但当我退出搜索视图时 它不会返回原始列表项 public class PlacesListAdapter extends ArrayAdapter
  • java.lang.NoClassDefFoundError:org.apache.batik.dom.svg.SVGDOMImplementation

    我在链接到我的 Android LibGDX 项目的 Apache Batik 库时遇到了奇怪的问题 但让我们从头开始 在 IntelliJ Idea 中我有一个项目 其中包含三个模块 Main Android 和 Desktop 我强调的
  • Android Activity 生命周期函数基础知识

    我正在测试这段代码 它显示活动所处的状态 public class Activity101Activity extends Activity String tag Lifecycle Called when the activity is
  • Android:捕获的图像未显示在图库中(媒体扫描仪意图不起作用)

    我遇到以下问题 我正在开发一个应用程序 用户可以在其中拍照 附加到帖子中 并将图片保存到外部存储中 我希望这张照片也显示在图片库中 并且我正在使用媒体扫描仪意图 但它似乎不起作用 我在编写代码时遵循官方的Android开发人员指南 所以我不
  • 是否有 ADB 命令来检查媒体是否正在播放

    我想使用 ADB 命令检查根植于终端的外部设备中是否正在播放音频 视频 我无法找到任何 ADB 命令 如果有 我尝试过 adb shell dumpsys media player 我想要一个命令来指定视频是否正在运行 您可以使用以下命令查
  • 无法展开 RemoteViews - 错误通知

    最近 我收到越来越多的用户收到 RemoteServiceException 错误的报告 我每次给出的堆栈跟踪如下 android app RemoteServiceException Bad notification posted fro
  • 原色(有时)变得透明

    我正在使用最新的 SDK 版本 API 21 和支持库 21 0 2 进行开发 并且在尝试实施新的材料设计指南时遇到了麻烦 材料设计说我需要有我的primary color and my accent color并将它们应用到我的应用程序上
  • Ubuntu 16.04 - Genymotion:找不到 /dev/hw_random

    I install Genymotion on the Ubuntu 16 04 64Bit I created a virtual emulator for Android 6 0 then I run this emulator but
  • 如何发布Android .aar源以使Android Studio自动找到它们?

    我正在将库发布到内部 Sonatype Nexus 存储库 Android Studio 有一个功能 可以自动查找通过 gradle 引用的库的正确源 我将 aar 的源代码作为单独的 jar 发布到 Nexus 但 Android Stu
  • 错误:在根项目“projectName”中找不到项目“app”

    我有一个在 Eclipse 中开发的旧应用程序 现在尝试将其迁移到 Android Studio 我更新了库并遵循了基本步骤 现在 我收到此错误 Error Project app not found in root project pro
  • Android访问远程SQL数据库

    我可以直接从 Android 程序访问远程 SQL 数据库 在网络服务器上 吗 即简单地打开包含所有必需参数的连接 然后执行 SQL 查询 这是一个私人程序 不对公众开放 仅在指定的手机上可用 因此我不担心第三方获得数据库访问权限 如果是这
  • Android 套接字和 asynctask

    我即将开始制作一个应该充当 tcp 聊天客户端的应用程序 我一直在阅读和阅读 我得出的结论是最好 如果不需要 将我的套接字和异步任务中的阅读器 问题是我不确定从哪里开始 因为我是 Android 新手 这至少对我来说是一项艰巨的任务 但据我
  • 在activity_main.xml中注释

    我是安卓新手 据我所知 XML 中的注释与 HTML 中的注释相同 使用 形式 我想在 Android 项目的 Activity main xml 配置文件中写一些注释 但它给了我错误 值得注意的是 我使用的是 Eclipse 但目前 我直
  • 一次显示两条Toast消息?

    我希望在一个位置显示一条 Toast 消息 并在另一位置同时显示另一条 Toast 消息 多个 Toast 消息似乎总是按顺序排队和显示 是否可以同时显示两条消息 是否有一种解决方法至少可以提供这种外观并且不涉及扰乱活动布局 Edit 看来
  • 强制 Listview 不重复使用视图(复选框)

    我做了一个定制Listview 没有覆盖getView 方法 Listview 中的每个项目都具有以下布局 联系布局 xml

随机推荐

  • Android 在 Google 地图中进行捏合缩放

    谷歌地图中的捏缩放功能是否可行 我进行了多次搜索但没有得到任何解决方案 请给出您的看法 或者需要编写api来实现这些功能 您是否使用 ScaleGestureDetector 来实现捏合和缩放 http developer android
  • 我的 Android 设备上的多个编码器支持 Mime 类型视频/avc

    当我枚举设备上的所有媒体编解码器时 我注意到以下编码器支持 mime 类型 video avc OMX qcom video encoder avc OMX google h264 encoder 你打电话时MediaCodec Creat
  • 如何高效找到重叠区间?

    我有以下玩具示例数据框 df f low f high 0 476201 0 481915 0 479161 0 484977 0 485997 0 491911 0 503259 0 508679 0 504687 0 510075 0
  • 尝试在 C# 中的 UInt16 上执行按位非 (~) 时出错

    出于某种原因 我根本不理解 或不明白 为什么这是有效的 UInt32 a 0x000000FF a UInt32 0x00000001 但这并没有 UInt16 a 0x00FF a UInt16 0x0001 它给出错误 常量值 某些数字
  • 如何处理 上的点击而不是子元素上的点击?

    我用以下代码处理点击 带输入的表 table tr td td tr table
  • sass 会损害性能吗?

    我一直在教育自己 阅读this http code google com speed page speed docs rendering html 引擎从右到左评估每个规则 从最右边的选择器 称为 键 开始并遍历每个选择器 直到找到匹配或丢
  • 如何为 Windows Phone 8 应用程序创建图表/图形? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我想在Windows Phone 8 应用程序 图表 图表 http img837 imageshack
  • 使您的 .NET 语言在调试器中正确运行

    首先 我对这个问题的长度表示歉意 我是作者钢铁计划 http ironscheme codeplex com 最近 我一直在努力发出合适的调试信息 以便我可以使用 本机 NET 调试器 虽然这取得了部分成功 但我遇到了一些初期问题 第一个问
  • AJAX HTML PHP 问题

    这是我的scripts js function showHint str if str length 0 document getElementById txtHint innerHTML return if window XMLHttpR
  • Haskell 中的链接/组合类型类

    假设我有两个定义如下的类型类 它们的功能相同但名称不同 class Monad m where gt gt m a gt a gt m b gt m b return a gt m a class PhantomMonad p where
  • 如何从 Excel 连接 Mongodb

    我想使用 excel 宏连接到 mongodb 数据库 有人知道如何完成这个任务吗 外壳方法 几乎所有与命令行交互的内容都可以使用 Shell 进行访问 下面是一个简单的示例 它连接到正在运行的 MongoDB 实例并将查询打印到即时窗口
  • 如何告诉函子模板类用户他必须编写什么函子operator()签名?

    考虑这个使用函子作为模板参数的模板化类的毫无意义的示例 godbolthere https godbolt org z TdeGh7 volatile int a 0 volatile int b 0 template
  • 为什么 Python 中“and”和“or”返回操作数?

    我正在经历 LPTHW 遇到了一些我无法理解的事情 你什么时候会想要你的布尔值and or or返回布尔值以外的东西 LPTHW 文本指出所有语言 如 python 都有这种行为 他的意思是解释语言与编译语言还是鸭子类型与静态类型语言 我运
  • 两个线程之间通信

    我有一个线程 A其中有一个列表 列表保存一些对象 现在我想设计一种机制 通过它我可以向线程发送一些消息A Thread A循环运行 它不等待或睡眠 其他一些线程 B 向线程发送一些消息A和线程A清空其所有队列 如何在线程之间发送消息 cla
  • 为什么 conda 尝试使用 --no-update-dependencies 更新包?

    通常 当我尝试安装新软件包时 conda 也想更新其他软件包 即使我添加了 no update dependency 开关 这些更新似乎是 不必要的 就像大多数时候只有版本号的最后一部分发生了变化 今天我想安装 mpld3 软件包 并且 c
  • 在 /etc/ansible/hosts 文件中指定 docker 容器

    我有一堆或 docker 容器在我的本地机器上运行 并且想在这些容器上测试 ansible 作为我的远程主机 但我还没有找到任何这方面的指南 现在我正在尝试使用以下命令配置单个容器以启用 SSH https docs docker com
  • 删除包含该单词的每一行

    如果该行包含特定单词 例如 我想删除整行 cat If cat 就在这一行 删除整行 Example Before 0123456789 CAT 0912WORDS 53DAD DSFC09243 DOG KLJ0963 CAT 8953
  • ValueError: 无法将字符串转换为浮点数: '' 在 Python 3 中

    我正在用 Python 编写一个小机器人 但遇到了问题 这似乎是一个常见问题 但我从未见过它在我所处的情况下被问到 好的 这是提出问题的代码 old values float removeprc browser find element b
  • proto 文件中定义的消息在最终的 dotnet 程序集中不可用

    我正在使用 dotnet standard 2 0 Visual Studio 2017 进行 gRPC 这就是我的整个项目的样子 消息 proto syntax proto3 package Messages message IdRequ
  • 如何在 Android 10 中从后台启动 Activity?

    我正在构建一个 Android 应用程序 我需要从后台启动一个活动 我正在使用 ForegroundStarter 来扩展服务来完成此任务 我有一个活动 Adscreen class 我需要从前台服务运行它 活动 Adscreen clas