一、概述 本篇文章將和大家一起來學習AIDL實現跨進程更新UI。 需求是:在同一個應用中有兩個Activity,MainActivity和TempActivity,這兩個Activity不在同一個進程中。 現在需要通過TempActivity來改變MainActivity中的視圖,即修改MainAc ...
一、概述
本篇文章將和大家一起來學習AIDL實現跨進程更新UI。
需求是:在同一個應用中有兩個Activity,MainActivity和TempActivity,這兩個Activity不在同一個進程中。
現在需要通過TempActivity來改變MainActivity中的視圖,即修改MainActivity中TextView的顯示內容並且添加兩個Button,也就是實現跨進程更新UI這麼一個功能。
對於這種跨進程更新UI的需求我們可以通過AIDL或者BroadcastReceiver的方式實現,而本篇文章主要通過AIDL的方式實現,如果你對AIDL
還不熟悉的話可以先看看我的這篇文章:
二、實現效果圖
在MainActivity里點擊“跳轉到新進程ACTIVITY”按鈕,會啟動一個新進程的TempActivity,我們先點擊“綁定服務”,這樣我們就啟動了服務,再點擊“AIDL更新”按鈕,通過調用handler來實現跨進程更新UI,點擊返回,我們發現MainActivity頁面中的TextView內容已經更新了,並且新添加了兩個按鈕。
三、核心代碼
IViewManager.aidl
裡面提供了兩個方法,一個是根據id更新TextView裡面的內容,一個是根據id添加view視圖
// IViewManager.aidl package com.czhappy.remoteviewdemo; // Declare any non-default types here with import statements interface IViewManager { void setTextViewText(in int id,in String text);//設置TextView的內容 void addView(in int layoutId); //添加View視圖 }
rebuild project讓IDE工具自己生成AIDL介面對應的java文件。
ViewAIDLService.java
package com.czhappy.remoteviewdemo.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.os.Message; import android.os.RemoteException; import android.util.Log; import com.czhappy.remoteviewdemo.IViewManager; import com.czhappy.remoteviewdemo.activity.MainActivity; public class ViewAIDLService extends Service { private static final String TAG = "ViewAIDLService"; private Binder viewManager = new IViewManager.Stub(){ @Override public void setTextViewText(int id, String text) throws RemoteException { Message message = new Message(); message.what = 2; Bundle bundle = new Bundle(); bundle.putInt("id",id); bundle.putString("text",text); message.setData(bundle); new MainActivity.MyHandler(ViewAIDLService.this,getMainLooper()).sendMessage(message); } @Override public void addView(int layoutId) throws RemoteException { Message message = new Message(); message.what = 3; Bundle bundle = new Bundle(); bundle.putInt("layoutId",layoutId); message.setData(bundle); Log.i(TAG,"thread name = "+Thread.currentThread().getName()); new MainActivity.MyHandler(ViewAIDLService.this,getMainLooper()).sendMessage(message); } }; public ViewAIDLService() { } /** * 當客戶端綁定到該服務時調用 * @param intent * @return */ @Override public IBinder onBind(Intent intent) { return viewManager; } }
MainActivity.java
package com.czhappy.remoteviewdemo.activity; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.RemoteViews; import android.widget.TextView; import com.czhappy.remoteviewdemo.R; import java.lang.ref.WeakReference; public class MainActivity extends AppCompatActivity { private static String TAG = "MainActivity"; private static LinearLayout mLinearLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLinearLayout = (LinearLayout) this.findViewById(R.id.mylayout); } public static class MyHandler extends Handler { WeakReference<Context> weakReference; public MyHandler(Context context, Looper looper) { super(looper); weakReference = new WeakReference<>(context); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); Log.i(TAG, "handleMessage"); switch (msg.what) { case 2: //修改MainActivity中TextView的內容 Bundle bundle = msg.getData(); TextView textView = (TextView) mLinearLayout.findViewById(bundle.getInt("id")); textView.setText(bundle.getString("text")); break; case 3: //在MainActivity中添加View視圖 LayoutInflater inflater = LayoutInflater.from(weakReference.get()); View view = inflater.inflate(msg.getData().getInt("layoutId"),null); mLinearLayout.addView(view); default: break; } } }; public void readyGo(View view){ Intent intent = new Intent(MainActivity.this, TempActivity.class); startActivity(intent); } }
TempActivity.java
package com.czhappy.remoteviewdemo.activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import com.czhappy.remoteviewdemo.IViewManager; import com.czhappy.remoteviewdemo.R; import com.czhappy.remoteviewdemo.service.ViewAIDLService; public class TempActivity extends AppCompatActivity { private String TAG = "TempActivity"; private IViewManager viewsManager; private boolean isBind = false; private ServiceConnection viewServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e(TAG,"onServiceConnected"); viewsManager = IViewManager.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { //回收 viewsManager = null; } }; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_temp); } /** * 綁定服務 */ public void bindService(View view) { Intent viewServiceIntent = new Intent(this,ViewAIDLService.class); isBind = bindService(viewServiceIntent,viewServiceConnection, Context.BIND_AUTO_CREATE); } /** * 更新UI */ public void UpdateUI(View view){ try { viewsManager.setTextViewText(R.id.mytext,"通過AIDL跨進程修改TextView內容"); viewsManager.addView(R.layout.button_layout); } catch (RemoteException e) { e.printStackTrace(); } } @Override protected void onDestroy() { super.onDestroy(); if(isBind){ unbindService(viewServiceConnection); isBind = false; } } }
配置文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.czhappy.remoteviewdemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".activity.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".activity.TempActivity" android:process=":remote"> </activity> <service android:name="com.czhappy.remoteviewdemo.service.ViewAIDLService"/> </application> </manifest>