接收连续SEND意图使应用程序返回到前面,但不更新意向但不、意图、应用程序、意向

2023-09-12 04:49:22 作者:不能释怀的过去

在一个项目,我的工作我已经遇到一个问题,接受来自外部的意图,即非发射意图。我有一个活动,可以使用正常的 LAUNCHER 意图启动,但它也回应发送从其他应用程序,如意图分享的YouTube应用程序链接到一个视频。第一次我分享一个链接从其他应用程序,我活动被创建,我可以使用 getIntent()得到的意图的细节,即 EXTRA_TEXT 。如果我闭上活动,然后再试一次与另一个链接,这也能正常工作。但是,如果我不收我的活动,回到通过家或最近推出的应用程序其他的应用,并分享另一个链接,我的活动又回到了前台,但 getIntent()结果在旧的意图,而不是新一个,我还以为触发了我的活动的启动

我已经创建了一个MCVE说明此问题:

活动

 公共类MainActivity扩展ActionBarActivity {
    私人TextView的电视;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        Log.d(MainActivity,的onCreate);
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        电视=(TextView中)findViewById(R.id.intent_text);
    }

    @覆盖
    保护无效onResume(){
        Log.d(MainActivity,onResume);
        super.onResume();
        意向意图= getIntent();
        文本字符串;
        如果(intent.hasExtra(Intent.EXTRA_TEXT)){
            文= intent.getExtras()的getString(Intent.EXTRA_TEXT)。
        } 其他 {
            文=无;
        }
        tv.setText(文本);
    }
}
 

该清单:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.intenttest
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =21/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=IntentTest
        机器人:主题=@风格/ Theme.AppCompat>
        <活动
            机器人:名称=。MainActivity
            机器人:标签=IntentTest>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.SEND/>
                <类机器人:名称=android.intent.category.DEFAULT/>
                <数据机器人:MIMETYPE =文/ */>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>

< /舱单>
 
一个C程序是如何运行在硬件上的

有关完整的缘故,琐碎的布局:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=com.example.intenttest.MainActivity>

    <的TextView
        机器人:ID =@ + ID / intent_text
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>

< / RelativeLayout的>
 

在启动应用程序,文本视图打印一无所有 启动YouTube应用程序,共享链接,选择这个应用程序,应用程序被重新启动,文本视图显示任何文本YouTube已经共享 preSS家 返回到YouTube应用程序,共享不同链接,选择这个应用程序,应用程序被重新启动,文本视图显示了同样的老文从之前 preSS背部或刷卡应用程序从最近的应用程序 返回到YouTube应用程序,共享另一个链接,选择这个应用程序,应用程序被重新启动(重新创建),文本视图显示新文本的YouTube刚刚共享

我得到我失去了一些东西根本感觉这里。如何让我的应用程序,以应对新的意图

解决方案

添加安卓launchMode =singleTop<活性GT; 的说明。

在活动启动(如果尚未运行),Android将调用的onCreate()键,即可获得意图致电 getIntent()(就像你已经这样做)。

如果该活动启动,而您的应用程序正在运行,Android将其带到前台,并调用 onNewIntent()传递新的意图。你应该重写此方法,做一些与传递意图(将其保存在一个成员变量或提取你想从它或其他什么)。之后,Android将调用 onResume()

调用 getIntent()将总是返回意图被用于最初启动活动。

On a project I'm working on I've come across an issue with receiving external intents, i.e. non-launcher intents. I have an Activity that can be started using the normal LAUNCHER intent, but it also responds to SEND intents from other apps, e.g. sharing a link to a video in the YouTube app. The first time I'm sharing a link from the other app, my Activity is created and I can use getIntent() to get at the details of the intent, i.e. the EXTRA_TEXT. If I close my Activity and try again with another link, that works fine too. However, if I don't close my Activity, go back to the other app via "home" or "recently launched apps", and share another link, my Activity comes back to the foreground, but getIntent() results in the old intent, not the new one that I would have thought triggered the restart of my Activity.

I have created an MCVE illustrating this issue:

The Activity:

public class MainActivity extends ActionBarActivity {
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("MainActivity", "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.intent_text);
    }

    @Override
    protected void onResume() {
        Log.d("MainActivity", "onResume");
        super.onResume();
        Intent intent = getIntent();
        String text;
        if (intent.hasExtra(Intent.EXTRA_TEXT)) {
            text = intent.getExtras().getString(Intent.EXTRA_TEXT);
        } else {
            text = "nothing";
        }
        tv.setText(text);
    }
}

The manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.intenttest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="IntentTest"
        android:theme="@style/Theme.AppCompat" >
        <activity
            android:name=".MainActivity"
            android:label="IntentTest" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/*" />
            </intent-filter>
        </activity>
    </application>

</manifest>

For completeness sake, the trivial layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.intenttest.MainActivity" >

    <TextView
        android:id="@+id/intent_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Start app, text view prints "nothing" Start YouTube app, share link, select this app, app gets restarted, text view shows whatever text YouTube has shared Press home Back to YouTube app, share different link, select this app, app gets restarted, text view shows same old text from before Press back or swipe app from "recent apps" Back to YouTube app, share yet another link, select this app, app gets restarted (re-created), text view shows new text YouTube has just shared

I get the feeling I'm missing something fundamental here. How do I get my app to respond to the new Intent?

解决方案

Add android:launchMode="singleTop" to the <activity> description.

When the Activity is launched (if it isn't already running), Android will call onCreate() and you can get the Intent by calling getIntent() (like you are already doing).

If the Activity is launched while your app is already running, Android will bring it to the foreground and will call onNewIntent() passing in the new Intent. You should override this method and do something with the passed Intent (save it in a member variable or extract what you want from it or whatever). After that Android will call onResume().

Calling getIntent() will ALWAYS return the Intent that was used to originally start the Activity.