美文网首页
Android 跳转至微博用户个人信息页面

Android 跳转至微博用户个人信息页面

作者: tianma | 来源:发表于2017-06-10 01:35 被阅读485次

版权声明:本文源自简书tianma,转载请务必注明出处: http://www.jianshu.com/p/994228144838

最近需要去实现应用内跳转至微博的个人信息页面(用户个人主页),其本质就是打开并跳转至第三方App指定的页面。显然,我们可以通过调用 startActivity(intent) 来实现该功能,其关键就是 intent 中需要携带哪些信息。

反编译 AndroidManifest.xml

将新浪微博的apk文件中的 AndroidManifest.xml 文件解压提取出来,然后使用 AXMLPrinter2.jar 对清单文件进行反编译:

java -jar AXMLPrinter2.jar AndroidManifest.xml > weibo.xml

其中 weibo.xml 即反编译成功的清单文件。

获取微博个人页 Activity

因为之前 weibo.xml 中的 Activity 过多,我们需要知道微博的用户个人信息页面对应的 Activity 全称。这里有两种方法:

  1. 使用辅助App来获取当前的 Activity ,例如 当前界面(当前Activity) ,具体用法这里就不介绍了。
  2. 使用 adb 命令获取当前 Activity:
    adb shell dumpsys activity activities | grep mFocusedActivity
    

当我们把新浪微博的个人信息页面切至前台时, adb 命令的结果为:

mFocusedActivity: ActivityRecord{9063f6f u0 com.sina.weibo/.page.ProfileInfoActivity t1578}

也就是说,新浪微博的个人信息页对应的 Activity 为 com.sina.weibo.page.ProfileInfoActivity, 那我们看 weibo.xml 中与该 Activity 对应的 xml 节点:

<activity 
    android:theme="@7F0B0011" 
    android:name="com.sina.weibo.page.ProfileInfoActivity" 
    android:exported="true" 
    android:configChanges="0x000004A0" 
    android:windowSoftInputMode="0x00000020"> 
    <intent-filter> 
        <action android:name="android.intent.action.VIEW"></action>  
        <category android:name="android.intent.category.DEFAULT"></category>  
        <category android:name="android.intent.category.BROWSABLE"></category>  
        <data android:scheme="sinaweibo" android:host="userinfo"></data> 
    </intent-filter>  
    <intent-filter> 
        <action android:name="android.intent.action.VIEW"></action>  
        <category android:name="android.intent.category.DEFAULT"></category>  
        <category android:name="android.intent.category.BROWSABLE"></category>  
        <data android:scheme="http" android:host="weibo.cn" android:path="/qr/userinfo"></data> 
    </intent-filter>  
    <intent-filter> 
        <action android:name="android.intent.action.VIEW"></action>  
        <category android:name="android.intent.category.DEFAULT"></category>  
        <data android:mimeType="vnd.android.cursor.item/vnd.com.sina.weibo.profile"></data> 
    </intent-filter> 
</activity>  

分析

上面的 xml 节点中,可以看到 android:exported="true",也就是说 ProfileInfoActivity 可以被外部程序调用打开。有三个 intent-filter 节点,其中第三个 intent-filterContentProvider 有关,可以忽略第三个只考虑前两个。
第一个 intent-filter 接受的 data 格式为 sinaweibo:userinfo, 第二个 intent-filter 接受的 data 格式为 http://weibo.cn/qr/userinfo。 我们通过 data 节点的数据可以判断除,第二个除了可以被新浪微博响应,也可以被浏览器之类的应用响应,但第一个只能被新浪微博响应,所以在使用第一个之前,需要判断新浪微博是否已经安装,否则会崩溃

实现

通过查阅相关文档,ProfileInfoActivity 可以接受的参数为 uid=xxx, 也就是 sinaweibo://userinfo?uid=xxxhttp://weibo.cn/qr/userinfo?uid=xxx,所以最终代码实现:

// 跳转至微博个人页
public void jumpToWeiboProfileInfo(Context context, String uid) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    boolean weiboInstalled = PackageUtils.isSinaWeiboInstalled(context);
    if (weiboInstalled) {
        intent.setData(Uri.parse("sinaweibo://userinfo?uid=" + uid));
    } else {
        intent.setData(Uri.parse("http://weibo.cn/qr/userinfo?uid=" + uid));
    }
    context.startActivity(intent);
}

// PackageUtils.java
public class PackageUtils {
    private PackageUtils(){}

    // 新浪微博是否安装
    public static boolean isSinaWeiboInstalled(Context context) {
        return isPackageInstalled(context, "com.sina.weibo");
    }

    // 包名对应的App是否安装
    public static boolean isPackageInstalled(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        if (packageManager == null)
            return false;
        List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
        for(PackageInfo info : packageInfoList) {
            if (info.packageName.equals(packageName))
                return true;
        }
        return false;
    }
}

参考

调用新浪微博显示用户信息

相关文章

网友评论

      本文标题:Android 跳转至微博用户个人信息页面

      本文链接:https://www.haomeiwen.com/subject/amlqqxtx.html