微信支付结果返回的一种获取方式-广播
2016-11-18 本文已影响0人
宁静世界
get微信返回码的方式之注册广播

说明以下都是数据的模拟并未牵扯到微信的实际操作
第一步
配置清单文件
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2"></activity>
<receiver android:name=".MyBroadCast">
<intent-filter >
<action android:name="wx.msg.send"/>
</intent-filter>
</receiver>
第二步模拟调用支付
调用支付开启支付页面
public class MainActivity extends AppCompatActivity {
private Button mWXPay;
private TextView mResult;
public MessageReceiver mMessageReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mResult = (TextView) findViewById(R.id.text);
mWXPay = (Button) findViewById(R.id.btn_pay);
mWXPay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,Activity2.class));
}
});
registerMessageReceiver();
}
//在销毁时要与广播解绑
@Override
protected void onDestroy() {
unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
/**
* 动态注册广播
*/
public void registerMessageReceiver() {
mMessageReceiver = new MessageReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("wx.msg.receiver");
registerReceiver(mMessageReceiver, filter);
}
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("wx.msg.receiver")) {
mResult.setText(intent.getStringExtra("message"));
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:text="测试按钮"
android:id="@+id/btn_pay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="我是文本啊"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
第三步模拟结果返回
在这里发送一个广播
public class Activity2 extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
button = (Button) findViewById(R.id.action0);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent("wx.msg.send");
mIntent.putExtra("messageSend", "模拟支付返回码为1");
sendBroadcast(mIntent);
finish();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/action0"
android:text="按钮2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
第四步接收支付返回码,并对外暴露数据源
public class MyBroadCast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("wx.msg.send"))
{
processCustomMessage(context, intent);
}
}
//send msg to MainActivity
private void processCustomMessage(Context context, Intent intent) {
String messageSend = intent.getStringExtra("messageSend");
Intent mIntent=new Intent("wx.msg.receiver");
mIntent.putExtra("message", messageSend);
context.sendBroadcast(mIntent);
}
}