简体   繁体   English

当应用程序在后台运行时,使用挂起的意图启动活动

[英]Launch activity using pending intent when app running in background

I have some problem how to launch DetailActivity using pending intent on a notification received when the app is running in the background, it's always launching a MainActivity ( but when the app is running in the foreground its work fine ) 我有一些问题,如何在应用程序在后台运行时如何使用收到的通知的待定​​意图来启动DetailActivity ,它始终会启动MainActivity (但是当应用程序在前台运行时其工作正常)

Everybody can help me please and sorry for my bad English 每个人都可以帮助我,对我英语不好的人感到抱歉

notificationIntent notificationIntent

val intent = Intent(this, DetailActivity::class.java)
    intent.action = Intent.ACTION_MAIN
    intent.addCategory(Intent.CATEGORY_LAUNCHER)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

PendingIntent 的PendingIntent

val pendingIntent = PendingIntent.getActivity(this, 0, intent, 
    PendingIntent.FLAG_CANCEL_CURRENT)

manifest.xml 的manifest.xml

<application
            android:name=".helper.App"
            android:icon="@drawable/ic_main_app"
            android:label="@string/app_name"
            android:roundIcon="@drawable/ic_main_app"
            android:supportsRtl="true"
            android:theme="@style/AppTheme"
            android:usesCleartextTraffic="true"
            tools:ignore="GoogleAppIndexingWarning"
            tools:targetApi="m">

        <activity
           ndroid:screenOrientation="portrait"
           android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"
           android:name=".ui.activity.aduan.DetailActivity">
           <meta-data
                    android:name="android.support.PARENT_ACTIVITY"
             android:value="kominfo.com.umat.ui.activity.main.MainActivity"/>
        </activity>

        <activity
                android:screenOrientation="portrait"
                android:name=".ui.activity.main.MainActivity"
                android:launchMode="singleTop"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
</application>

you have set to your DetailActivity in Manifest 您已在Manifest设置为DetailActivity

<meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value="kominfo.com.umat.ui.activity.main.MainActivity"/>

This is the reason why your DetailActivity needs MainActivity 这就是您的DetailActivity需要MainActivity的原因

Also try to add following Flags to your Intent 还要尝试将以下标志添加到您的意图中

Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_SINGLE_TOP

and for Notification Notification

notification.flags |= Notification.FLAG_AUTO_CANCEL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果应用程序已打开或在后台运行,则“待定意图”不会启动活动 - Pending Intent does not launch Activity if app is already open or in background 启动待处理的意图并返回已启动它的活动 - Launch a pending intent and return to activity that has launch it 仅当应用程序不在后台运行时,才具有启动器活动的NFC意图启动应用程序 - NFC intent launch app with launcher activity only if the app isn't running in background 意图启动活动时应用崩溃 - App Crashing When Intent to Launch Activity Called 如果未通过某些未决的意图打开应用而不使用通知或按钮单击,是否可以调用活动? - Can an activity be invoked when the app is not opened from some pending intent without using notification or button clicks? 使用FusedLocationProviderClient将应用程序杀死并在后台使用Intent将其挂起但位置不规则时,将获取位置 - Location is fetching when the app is killed and in background using FusedLocationProviderClient with pending Intent but in Irregular times 使用隐式意图启动活动? - Using implicit intent to launch activity? 无法使用意图启动活动 - Not Able to Launch Activity using intent 当应用未在后台运行时,如何使用通知打开任何活动? - How to open any Activity using notification when app is not running in Background? 使用 createComposeRule 运行 Jetpack Compose UI 测试时出现“RuntimeException:无法启动活动...无法解析意图的活动” - "RuntimeException: Could not launch activity...Unable to resolve activity for Intent" when running Jetpack Compose UI tests with createComposeRule
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM