您好,登錄后才能下訂單哦!
為什么需要將webView放在獨(dú)立進(jìn)程
webView獨(dú)立進(jìn)程需要注意什么
如何聲明一個(gè)獨(dú)立進(jìn)程
在默認(rèn)情況下,同一應(yīng)用的所有組件都在相同的進(jìn)程中運(yùn)行。
在Manifest中可以設(shè)置各組件 (<activity>、<service>、<receiver>、<provider>)的 android:process 屬性來(lái)指定相應(yīng)的進(jìn)程。
跨進(jìn)程的方式
在android當(dāng)中提供了2種方式實(shí)現(xiàn)。
一種是Messenger, 另一種是Aidl.
Messenger
由于Messenger是采用消息隊(duì)列的方式實(shí)現(xiàn),所有接受和發(fā)送的時(shí)候都需要Handler協(xié)助。
服務(wù)端
public class MessengerService extends Service { public static final int GET_DATA = 1; public static final int SET_DATA = 2; Messenger messenger = new Messenger(new ServiceHandler()); Messenger replyMessenger; //向客服端返回信息 public MessengerService() { } @Override public IBinder onBind(Intent intent) { return messenger.getBinder(); } class ServiceHandler extends Handler { @Override public void handleMessage(Message msg) { replyMessenger = msg.replyTo; switch (msg.what) { case GET_DATA: //客服端向服務(wù)端請(qǐng)求數(shù)據(jù) if (replyMessenger != null) { Bundle bundle = new Bundle(); bundle.putString("str", CustomData.getInstance().getData()); Message message = Message.obtain(null, 1); message.setData(bundle); try { replyMessenger.send(message); } catch (RemoteException e) { e.printStackTrace(); } } break; case SET_DATA: //客服端向服務(wù)端請(qǐng)求更新數(shù)據(jù) CustomData.getInstance().setData(msg.getData().getString("str")); break; } } } }
客服端:
public class MessengerClientActivity extends AppCompatActivity { private WebView mWebView; private Button mGetDatBtn; private Button mSetDatBtn; public static void startThis(Context context, String url) { Intent intent = new Intent(context, MessengerClientActivity.class); intent.putExtra("url", url); context.startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_messenger_client); mWebView = (WebView) findViewById(R.id.webview); mGetDatBtn = (Button) findViewById(R.id.get_data_btn); mSetDatBtn = (Button) findViewById(R.id.set_data_btn); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setSupportZoom(false); webSettings.setBuiltInZoomControls(false); webSettings.setAllowFileAccess(true); webSettings.setDatabaseEnabled(true); webSettings.setDomStorageEnabled(true); webSettings.setGeolocationEnabled(true); webSettings.setAppCacheEnabled(true); webSettings.setAppCachePath(getApplicationContext().getCacheDir().getPath()); webSettings.setDefaultTextEncodingName("UTF-8"); //屏幕自適應(yīng) webSettings.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); } else { webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { webSettings.setDisplayZoomControls(false); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { webSettings.setLoadsImagesAutomatically(true); } else { webSettings.setLoadsImagesAutomatically(false); } mWebView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY); mWebView.setHorizontalScrollBarEnabled(false); mWebView.setHorizontalFadingEdgeEnabled(false); mWebView.setVerticalFadingEdgeEnabled(false); String url = "http://www.jianshu.com/"; mWebView.loadUrl(url); mGetDatBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getData(); } }); mSetDatBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setData(); } }); } Messenger messenger; Messenger messengerReply = new Messenger(new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MessengerService.GET_DATA: mGetDatBtn.setText("" + msg.getData().get("str")); break; } } }); boolean mBound; ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { messenger = new Messenger(service); mBound = true; } @Override public void onServiceDisconnected(ComponentName name) { messenger = null; mBound = false; } }; private void getData() { if (!mBound) return; Message message = Message.obtain(null, MessengerService.GET_DATA, 0,0); //用于服務(wù)端應(yīng)答 message.replyTo = messengerReply; sendMessage(message); } private void setData() { if (!mBound) return; Message message = Message.obtain(null, MessengerService.SET_DATA, 0,0); sendMessage(message); } private void sendMessage(Message message) { try { messenger.send(message); } catch (RemoteException e) { e.printStackTrace(); } } @Override protected void onStart() { super.onStart(); // Bind to the service bindService(new Intent(this, TestWebService.class), serviceConnection, Context.BIND_AUTO_CREATE); } @Override protected void onStop() { super.onStop(); // Unbind from the service if (mBound) { unbindService(serviceConnection); mBound = false; } } private void destroyWebView(WebView webView) { if (webView == null) return; webView.stopLoading(); ViewParent viewParent = webView.getParent(); if (viewParent != null && viewParent instanceof ViewGroup) ((ViewGroup) viewParent).removeView(webView); webView.removeAllViews(); webView.destroy(); webView = null; } @Override protected void onDestroy() { destroyWebView(mWebView); super.onDestroy(); } }
AIDL
第一步:創(chuàng)建.aidl文件
aidl文件:
interface IAidlProcess { //默認(rèn)支持原語(yǔ)類(lèi)型(int、long、char等等)、String、CharSequence、List、Map //自定義類(lèi)型需要導(dǎo)入 import eebochina.com.testtechniques.testwebview.XXXClass //自定義類(lèi)型傳輸一定需要是序列化對(duì)象 String getCustomData(); void setCustomData(String str); }
服務(wù)端
public class AidlService extends Service { public AidlService() { } ITestProcess.Stub mBinder = new ITestProcess.Stub() { @Override public String getCustomData() throws RemoteException { return CustomData.getInstance().getData(); } @Override public void setCustomData(String str) throws RemoteException { CustomData.getInstance().setData(str); } }; @Override public IBinder onBind(Intent intent) { return mBinder; } }
客服端獲取綁定接口
AidlService mAidlService; private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mAidlService = IAidlProcess.Stub.asInterface(service); mBound = true; } @Override public void onServiceDisconnected(ComponentName name) { mBound = false; mAidlService = null; } };
在獲取了綁定接口后就可以直接和服務(wù)端通訊了。
2種通訊方式都簡(jiǎn)單的介紹了下,后面的實(shí)際應(yīng)用還需要根據(jù)不同的業(yè)務(wù)進(jìn)行調(diào)整。
由于aidl是方法直接調(diào)用的,從代碼擴(kuò)展和閱讀來(lái)說(shuō)比messenger要強(qiáng)很多。
如果有寫(xiě)的不好和不對(duì)的地方,希望大家可以及時(shí)指出來(lái)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。