很多情况,我们都需要用户关注我们的官方微博,现在提供两种办法展现自己的新浪微博:
[color=red]1.如果用户有安装新浪微博客户端,直接打开个人首页
2.如果用户没有安装,则浏览器打开[/color]
上代码:(uid为微博uid,查看方法:进入个人微博首页查看源代码,找出uid对应值)
[color=red]1.如果用户有安装新浪微博客户端,直接打开个人首页
2.如果用户没有安装,则浏览器打开[/color]
上代码:(uid为微博uid,查看方法:进入个人微博首页查看源代码,找出uid对应值)
//打开微博
private void openWeiBo(Context context, String uid)
{
if(isHasWeiBo(context))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("sinaweibo://userinfo?uid=" + uid);
intent.setData(uri);
Intent chooseIntent = Intent.createChooser(intent, "Weibo");
startActivity(chooseIntent);
}
else
{
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http://weibo.cn/qr/userinfo?uid=" + uid);
intent.setData(uri);
Intent chooseIntent = Intent.createChooser(intent, "Weibo");
startActivity(chooseIntent);
}
}
//检查是否有安装微博客户端
private boolean isHasWeiBo(Context context)
{
final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
List<String> pName = new ArrayList<String>();// 用于存储所有已安装程序的包名
// 从pinfo中将包名字逐一取出,压入pName list中
if (pinfo != null)
{
for (int i = 0; i < pinfo.size(); i++)
{
String pn = pinfo.get(i).packageName;
pName.add(pn);
}
}
return pName.contains("com.sina.weibo");// 判断pName中是否有目标程序的包名,有TRUE,没有FALSE
}