zoukankan      html  css  js  c++  java
  • Android应用中对于微信分享的实例及问题



    源码地址

    如何分享

    分享无相应

    分享结果如何接收响应

    微信 分享回调

    (提示几点关键问题:   debug_key 一定要获得对应的签名码 然后和weixin官网的appid对应     )

    几点注意事项 
    1)必须新建一个包位置是
        <you package>.wxapi
    2)并在此包下简历class
        WXEntryActivity.class
    3)AndroidManifest.xml  需要添加对应Activity的声明
         <activity
                       android:name="com.yinhang.pos.wxapi.WXEntryActivity"
                       android:exported="true"
                       android:theme="@android:style/Theme.Translucent" >
    4) WXEntryActivity.class 的基本要求
        public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler{


    RelativeLayout

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".StartActivity" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
        <Button 
            android:id="@+id/btn_share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/MMLineActionButton"
            android:layout_centerHorizontal="true"
            android:text="share"
            />
    
    </RelativeLayout>



    manifest

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.weishare"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
        
        
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.weishare.activity.StartActivity"
                android:exported="true"
                android:launchMode="singleTop"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity  
               android:name="com.example.weishare.wxapi.WXEntryActivity"  
               android:exported="true"  
               android:theme="@android:style/Theme.Translucent" >  
           </activity>  
        </application>
    
    </manifest>



    WXEntryActivity   响应 发送返回的关键

    public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    	 private IWXAPI api;  
    	@Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            api = WXAPIFactory.createWXAPI(this,  
                    "your app id", false);  
            api.handleIntent(getIntent(), this);  
      
        }  
    	@Override  
        public void onReq(BaseReq arg0) {  
      
        }  
      
        @Override  
        public void onResp(BaseResp resp) {  
            int result = 0;  
      
            switch (resp.errCode) {  
            case BaseResp.ErrCode.ERR_OK:  
                result = R.string.errcode_success;  
                break;  
            case BaseResp.ErrCode.ERR_USER_CANCEL:  
                result = R.string.errcode_cancel;  
                break;  
            case BaseResp.ErrCode.ERR_AUTH_DENIED:  
                result = R.string.errcode_deny;  
                break;  
            default:  
                result = R.string.errcode_unknown;  
                break;  
            }  
      
            Toast.makeText(this, result, Toast.LENGTH_LONG).show();  
      
            // TODO 微信分享 成功之后调用接口  
            this.finish();  
        }  
    
    }




    StartActivity

    public class StartActivity extends Activity {
    	
    	
    	private static final String APP_ID = "your app id";
    	private IWXAPI api;
    	private static final String text = "sdafljsalfdkj";
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_start);
    		
    		findViewById(R.id.btn_share).setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				final EditText editor = new EditText(StartActivity.this);
    				editor.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    				editor.setText("默认发送信息");
    //				MMAlert.showAlert(StartActivity.class, "title", editor, "ok", "cancel", new DialogInterface.OnClickListener())
    				
    				regToWx();
    				// 初始化一个WXTextObject对象
    				WXTextObject textObj = new WXTextObject();
    				textObj.text = text;
    
    				// 用WXTextObject对象初始化一个WXMediaMessage对象
    				WXMediaMessage msg = new WXMediaMessage();
    				msg.mediaObject = textObj;
    				// 发送文本类型的消息时,title字段不起作用
    				// msg.title = "Will be ignored";
    				msg.description = text;
    
    				// 构造一个Req
    				SendMessageToWX.Req req = new SendMessageToWX.Req();
    				req.transaction = String.valueOf(System.currentTimeMillis());//buildTransaction("text"); // transaction字段用于唯一标识一个请求
    				req.message = msg;
    //				req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
    				Log.e("",api.sendReq(req) + "----------------");
    				// 调用api接口发送数据到微信
    				api.sendReq(req);
    				
    			}
    		});
    	}
    	
    	private void regToWx(){
    		api = WXAPIFactory.createWXAPI(this, APP_ID, true);
    		api.registerApp(APP_ID);
    	}
    	
    	@Override
    	protected void onRestart() {
    		// TODO Auto-generated method stub
    		super.onRestart();
    //		Toast.makeText(this, "onRestart   sdkjflsaj", Toast.LENGTH_LONG).show();
    		Log.e(this.getApplication().toString(), "---------onRestart-----------------------");
    	}
    	@Override
    	protected void onResume() {
    		// TODO Auto-generated method stub
    		super.onResume();
    //		Toast.makeText(this, "onResume  lasdkjflsaj", Toast.LENGTH_LONG).show();
    		Log.e(this.getApplication().toString(), "---------onResume-----------------------");
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.start, menu);
    		return true;
    	}
    
    //	@Override
    //	public void onReq(BaseReq arg0) {
    //		// TODO Auto-generated method stub
    ////		Toast.makeText(this, "onReqonReqonReqlasdkjflsaj", Toast.LENGTH_LONG).show();
    //		Log.e(this.getApplication().toString(), "---------onReq-----------------------");
    //	}
    //
    //	@Override
    //	public void onResp(BaseResp arg0) {
    //		// TODO Auto-generated method stub
    ////		Toast.makeText(this, "onResponResponRespasdkjflsaj", Toast.LENGTH_LONG).show();;
    //		Log.e(this.getApplication().toString(), "---------onResp-----------------------");
    //		
    //		
    //	}
    
    }














  • 相关阅读:
    centos文件与权限
    python执行centos命令
    Centos下新建用户及修改用户目录
    微信现金红包 python
    图像直方图
    十三种基于直方图的图像全局二值化算法原理、实现、代码及效果。
    图像处理之常见二值化方法汇总
    OpenCV中对图像进行二值化的关键函数——cvThreshold()。
    cvLoadImage
    cvGet2D的用法
  • 原文地址:https://www.cnblogs.com/flyingsir/p/3983711.html
Copyright © 2011-2022 走看看