【问题标题】:Intent.getType() returning null in onNewIntent(Intent intent ) methodIntent.getType() 在 onNewIntent(Intent intent) 方法中返回 null
【发布时间】:2016-08-11 07:24:13
【问题描述】:

我正在尝试从其他应用程序获取 MIME 类型 text/plain 的 Intent,并将该文本存储在字符串类型的变量中。 onCreate 方法可以正常工作,但是当我使用 singleTask 作为启动模式并暂停应用程序(通过按主页按钮)并尝试将其他应用程序中的文本共享到我的应用程序时, onNewIntent(Intent intent) 方法被调用,intent.getType() 返回 null,但 onCreate 方法工作正常,我不知道为什么,请帮忙。谢谢

private String sharedData="";
@Override
protected void onNewIntent(Intent intent) {

    intent = getIntent();
    String action = intent.getAction();

    String type = intent.getType();  //this method is returning null

    if(intent.ACTION_SEND.equals(action) && type!=null)
    {
        sharedData = FileHandler.handleSendText(intent);//FileHandler is class i created with contains method handleSendText
    }

    super.onNewIntent(intent);
}

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    调用intent = getIntent(); 将返回给您提供给活动的原始intent。因此,它应该为空。尝试删除此行。

    【讨论】:

    • 是的,它正在工作,你的初衷是什么意思?请你能解释更多吗?谢谢
    • 每当启动一个活动时,都会向它提供一个新的意图,您可以通过getIntent() 获得它,这基本上是在应用程序首次启动时初始化的。在您的情况下,因为您在onNewIntent() 中收到调用,更新后的意图应该是仅在方法参数中提供的意图,而不是通过getIntent()
    • 有道理。谢谢。
    • 酷...如果您感到满意,您可能想“接受”这个答案
    猜你喜欢
    • 2015-07-14
    • 2014-11-16
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多