1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
public  class  ShareUtil {
     /**
      * 判断是否安装腾讯、新浪等指定的分享应用
      *
      * @param packageName 应用的包名
      */
     public  static  boolean  checkInstallation(Activity activity, String packageName) {
         try  {
             activity.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
             return  true ;
         catch  (PackageManager.NameNotFoundException e) {
             return  false ;
         }
     }
     /**
      * 分享到QQ好友
      */
     private  void  shareToQQFriend(Activity activity) {
         Intent intent =  new  Intent();
         ComponentName componentName =  new  ComponentName( "com.tencent.mobileqq" "com.tencent.mobileqq.activity.JumpActivity" );
         intent.setComponent(componentName);
         intent.setAction(Intent.ACTION_SEND);
         intent.setType( "text/*" );
         intent.putExtra(Intent.EXTRA_TEXT,  "这是分享内容" );
         activity.startActivity(intent);
     }
     /**
      * 分享信息到朋友
      *
      */
     private  void  shareToWxFriend(Activity activity) {
         Intent intent =  new  Intent();
         ComponentName componentName =  new  ComponentName( "com.tencent.mm" "com.tencent.mm.ui.tools.ShareImgUI" );
         intent.setComponent(componentName);
         intent.setAction(Intent.ACTION_SEND);
         intent.setType( "text/*" );
         intent.putExtra(Intent.EXTRA_TEXT,  "这是分享内容" );
         intent.putExtra(Intent.EXTRA_STREAM,  "http://www.weixin.com" );
         activity.startActivity(intent);
     }
 
     /**
      * 分享信息到朋友圈
      *
      */
     private  void  shareToTimeLine(Activity activity) {
         Intent intent =  new  Intent();
         ComponentName componentName =  new  ComponentName( "com.tencent.mm" "com.tencent.mm.ui.tools.ShareToTimeLineUI" );
         intent.setComponent(componentName);
 
         intent.setAction(Intent.ACTION_SEND);
         intent.putExtra(Intent.EXTRA_STREAM, "http://www.weixin.com" );
         intent.setType( "image/*" );
         activity.startActivity(intent);
     }
     public   void  qqshare(Activity activity) {
         //Tencent类是SDK的主要实现类,开发者可通过Tencent类访问腾讯开放的OpenAPI。
         // 其中APP_ID是分配给第三方应用的appid,类型为String。
         Tencent mTencent = Tencent.createInstance( "1111112" , x.app().getApplicationContext());
         ShareListener myListener =  new  ShareListener();
 
         final  Bundle params =  new  Bundle();
         params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
         params.putString(QQShare.SHARE_TO_QQ_TITLE,  "要分享的标题" );
         params.putString(QQShare.SHARE_TO_QQ_SUMMARY,  "要分享的摘要" );
         params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,  "http://www.qq.com/news/1.html" );
         params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,  "https://www.baidu.com/img/bd_logo1.png" );
         mTencent.shareToQQ(activity, params, myListener);
     }
     private  class  ShareListener  implements  IUiListener {
 
         @Override
         public  void  onCancel() {
             // TODO Auto-generated method stub
             Toast.makeText(x.app(),  "分享取消" , Toast.LENGTH_SHORT).show();
//            MainActivity.this.toast("分享取消");
         }
 
         @Override
         public  void  onComplete(Object arg0) {
             // TODO Auto-generated method stub
             Toast.makeText(x.app(),  "分享成功" , Toast.LENGTH_SHORT).show();
         }
 
         @Override
         public  void  onError(UiError arg0) {
             // TODO Auto-generated method stub
             Toast.makeText(x.app(),  "分享出错" , Toast.LENGTH_SHORT).show();
         }
 
     }
}

















本文转自wauoen51CTO博客,原文链接: http://blog.51cto.com/7183397/1915715,如需转载请自行联系原作者