Open page in Twitter app from other app - Android

前端 未结 6 1963
南旧
南旧 2020-12-22 20:54

I was looking for some way to launch Twitter app and open a specified page from my application, without webview. I found the solution for Facebook here: Opening facebook app

6条回答
  •  不知归路
    2020-12-22 21:32

    Based on fg.radigales answer, this is what I used to launch the app if possible, but fall back to the browser otherwise:

    Intent intent = null;
    try {
        // get the Twitter app if possible
        this.getPackageManager().getPackageInfo("com.twitter.android", 0);
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USERID"));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    } catch (Exception e) {
        // no Twitter app, revert to browser
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/PROFILENAME"));
    }
    this.startActivity(intent);
    

    UPDATE

    Added intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); to fix an issue where twitter was opening inside my app instead of as a new activity.

提交回复
热议问题