为什么需要将webView放在独立进程
- webView 加载网页的时候可能占用大量内存,导致应用程序OOM。
- webView 在访问结束的时候可以直接杀死该进程,防止内存泄漏。
- webView 在崩溃的时候不影响主进程。
webView独立进程需要注意什么
- 由于进程之间内存是独立的,所以导致了Appcation, 静态类需要在新的进程重新创建。
- 内存中的数据不共享,需要跨进程通讯。
如何声明一个独立进程
在默认情况下,同一应用的所有组件都在相同的进程中运行。
在Manifest中可以设置各组件 (<activity>、<service>、<receiver>、<provider>)的 android:process 属性来指定相应的进程。
跨进程的方式
在android当中提供了2种方式实现。
一种是Messenger, 另一种是Aidl.
- Messenger:实现相对简单,将所有请求放到消息队列中,不适合做并发处理,在大多数的场景用Messenger就可以实现了。
- AIDL: 适合并发操作。直接方法调用,结构更清晰。
Messenger
由于Messenger是采用消息队列的方式实现,所有接受和发送的时候都需要Handler协助。
服务端
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: //客服端向服务端请求数据 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: //客服端向服务端请求更新数据 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"); //屏幕自适应 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://ponentName name) { mBound = false; mAidlService = null; } };在获取了绑定接口后就可以直接和服务端通讯了。
2种通讯方式都简单的介绍了下,后面的实际应用还需要根据不同的业务进行调整。
由于aidl是方法直接调用的,从代码扩展和阅读来说比messenger要强很多。
如果有写的不好和不对的地方,希望大家可以及时指出来。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。