3

在我活动的 onNewIntent() 方法中, getIntent().getData();始终为空。onCreate()在去或任何其他生命周期函数之前肯定会去这个方法。它从浏览器返回这里,但我不知道为什么getIntent().getData()为空。

这个活动像这样启动浏览器context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL())));

并返回这里

@Override
public void onNewIntent(Intent intent){
    super.onNewIntent(intent);

    Uri uri = getIntent().getData();
    if (uri != null && uri.toString().startsWith(TwitterConstants.CALLBACK_URL)) {...}
}

但 uri 始终为空。

清单的东西:

  <activity
        android:name="myapp.mypackage.TweetFormActivity"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar">
         <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="oauth" android:host="myapp"/>
        </intent-filter>
        </activity>

static final String CALLBACK_URL = "oauth://myapp";

我在这里想念什么?谢谢

4

1 回答 1

12

您应该在获取 URI 之前调用getData()参数或intent执行。不会自动设置新意图。setIntent(intent)onNewIntent()

更新:所以,这里有两种方法可以实现onNewIntent()。第一个用新的意图替换旧的意图,因此当您getIntent()稍后调用时,您将收到新的意图。

@Override
protected void onNewIntent(final Intent intent) {
    super.onNewIntent(intent);
    // Here we're replacing the old intent with the new one.
    setIntent(intent);
    // Now we can call getIntent() and receive the new intent.
    final Uri uri = getIntent().getData();
    // Do something with the URI...
}

第二种方法是使用来自新意图的数据,让旧意图保持原样。

@Override
protected void onNewIntent(final Intent intent) {
    super.onNewIntent(intent);
    // We do not call setIntent() with the new intent,
    // so we have to retrieve URI from the intent argument.
    final Uri uri = intent.getData();
    // Do something with the URI...
}

当然,您可以使用两种变体的组合,但不要期望收到新的意图,getIntent()直到您明确设置它setIntent()

于 2012-06-12T22:20:02.773 回答