微信支付結(jié)果返回的一種獲取方式-廣播

get微信返回碼的方式之注冊廣播

Paste_Image.png

說明以下都是數(shù)據(jù)的模擬并未牽扯到微信的實(shí)際操作

第一步


配置清單文件

<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>

第二步模擬調(diào)用支付


調(diào)用支付開啟支付頁面

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();
}

//在銷毀時(shí)要與廣播解綁
@Override
protected void onDestroy() {
    unregisterReceiver(mMessageReceiver);
    super.onDestroy();
}


/**
 * 動態(tài)注冊廣播
 */
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>

第三步模擬結(jié)果返回


在這里發(fā)送一個(gè)廣播

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>

第四步接收支付返回碼,并對外暴露數(shù)據(jù)源


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);

    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容