WebView內(nèi)存泄露與跨進(jìn)程通信

需求

  1. 用WebView加載網(wǎng)頁商城,內(nèi)存泄露問題
  2. 商城調(diào)用APP支付(微信支付和支付寶支付)

解決方案

針對需求1

在新的進(jìn)程中開啟加載WebView的界面,在頁面退出之后,關(guān)閉該進(jìn)程

  1. 開啟新進(jìn)程的方法
    通常我們會使用修改清單文件的android:process來達(dá)到目的。
    不了解多進(jìn)程的可以參考:傳送
 <activity
            android:name=".web.StoreWebActivity"
            android:process="com.test.web"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan">
</activity>
  1. 當(dāng)WebView退出時,在WebView所在的Activity中,殺死WebView所在的進(jìn)程
@Override
public void onDestroy() {
        android.os.Process.killProcess(android.os.Process.myPid());
        super.onDestroy();
 }

解決問題2

因為WebView在新的進(jìn)程中,導(dǎo)致WXPayEntryActivityStoreWebActivity不在一個進(jìn)程中,導(dǎo)致在onResq回調(diào)處理支付結(jié)果時,callbacknull
當(dāng)然有人會說把WxPayEntryActivity也放在com.test.web這個進(jìn)程中不就好了,我沒有嘗試是否可以,因為微信支付也會主進(jìn)程中調(diào)用
這個時候就需要處理WxPayEntryActivityStoreWebActivity之間的跨進(jìn)程通信問題

  1. 跨進(jìn)程通信的方案有很多,可以參考這篇文章傳送,我也是參考這篇文章中的方案解決--使用Messenger解決跨進(jìn)程通信
  2. 創(chuàng)建一個和StoreWebActivity的相同進(jìn)程的Service
<service android:name=".MessengerService"
         android:process="com.test.web"/>

MessagerService的代碼如下:

public class MessengerService extends Service {

    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            WechatPay.getInstance().onResp(msg.what);
        }
    }

    Messenger mMessenger = new Messenger(new IncomingHandler());

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mMessenger.getBinder();
    }
}
  1. 然后修改WxPayEntryActivity

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;
    String processName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        processName = BMApplication.getProcessName();
        if (!TextUtils.isEmpty(processName) && processName.equals(this.getPackageName())) {
        }else{
                Intent intent = new Intent(WXPayEntryActivity.this, MessengerService.class);
                bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
        }
       
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);

    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }


    @Override
    public void onReq(BaseReq req) {
    }

    private int code = -1;

    @Override
    public void onResp(BaseResp resp) {
        
        if (!TextUtils.isEmpty(processName) && processName.equals(this.getPackageName())) {//判斷進(jìn)程名,保證只有主進(jìn)程運(yùn)行
            if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
                WechatPay.getInstance().onResp(msg.what);
                WXPayEntryActivity.this.finish();   
            }
        } else {
            if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
                code = resp.errCode;
            }
        }
    }

    private boolean mBound;
    private Messenger mMessenger;
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mMessenger = new Messenger(service);
            mBound = true;
            deal(code);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            deal(code);
            mMessenger = null;
            mBound = false;
        }
    };

    @Override
    protected void onStop() {
        super.onStop();
        if (mBound) {
            unbindService(mServiceConnection);
            mBound = false;
        }
    }

    private void deal(int code) {
        if (!mBound) {
            return;
        }
        Message msg = Message.obtain(null, code);
        try {
            mMessenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        WXPayEntryActivity.this.finish();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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