Android通過RemoteViews實(shí)現(xiàn)跨進(jìn)程更新UI示例
一、概述
前面一篇文章Android通過AIDL實(shí)現(xiàn)跨進(jìn)程更新UI我們學(xué)習(xí)了aidl跨進(jìn)程更新ui,這種傳統(tǒng)方式實(shí)現(xiàn)跨進(jìn)程更新UI是可行的,但有以下弊端:
- View中的方法數(shù)比較多,在IPC中需要增加對(duì)應(yīng)的方法比較繁瑣。
- View的每一個(gè)方法都會(huì)涉及到IPC操作,多次IPC帶來的開銷問題不容小覷。
- View中方法的某些參數(shù)可能不支持IPC傳輸。例如:OnClickListener,它僅僅是個(gè)接口沒有序列化。
接下來我們通過RemoteViews實(shí)現(xiàn)跨進(jìn)程更新UI
二、實(shí)現(xiàn)效果圖
在同一個(gè)應(yīng)用中有兩個(gè)Activity,MainActivity和Temp2Activity,這兩個(gè)Activity不在同一個(gè)進(jìn)程中。

現(xiàn)在需要通過Temp2Activity來改變MainActivity中的視圖,即在MainActivity中添加兩個(gè)Button,也就是實(shí)現(xiàn)跨進(jìn)程更新UI這么一個(gè)功能。
在MainActivity里點(diǎn)擊“跳轉(zhuǎn)到新進(jìn)程ACTIVITY”按鈕,會(huì)啟動(dòng)一個(gè)新進(jìn)程的Temp2Activity,我們先點(diǎn)擊“綁定服務(wù)”,這樣我們就啟動(dòng)了服務(wù),再點(diǎn)擊“AIDL更新”按鈕,通過調(diào)用handler來實(shí)現(xiàn)跨進(jìn)程更新UI,點(diǎn)擊返回,我們發(fā)現(xiàn)MainActivity頁面中新添加了兩個(gè)按鈕,并且按鈕還具有點(diǎn)擊事件。

三、核心代碼
IremoteViewsManager.aidl
里面提供了兩個(gè)方法,一個(gè)是根據(jù)id更新TextView里面的內(nèi)容,一個(gè)是根據(jù)id添加view視圖
// IremoteViewsManager.aidl.aidl
package com.czhappy.remoteviewdemo;
interface IremoteViewsManager {
void addRemoteView(in RemoteViews remoteViews);
}
RemoteViewsAIDLService.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.widget.RemoteViews;
import com.czhappy.remoteviewdemo.IremoteViewsManager;
import com.czhappy.remoteviewdemo.activity.MainActivity;
/**
* Description:
* User: chenzheng
* Date: 2017/2/10 0010
* Time: 10:56
*/
public class RemoteViewsAIDLService extends Service {
private static final String TAG = "RemoteViewsAIDLService";
private Binder remoteViewsBinder = new IremoteViewsManager.Stub(){
@Override
public void addRemoteView(RemoteViews remoteViews) throws RemoteException {
Message message = new Message();
message.what = 1;
Bundle bundle = new Bundle();
bundle.putParcelable("remoteViews",remoteViews);
message.setData(bundle);
new MainActivity.MyHandler(RemoteViewsAIDLService.this,getMainLooper()).sendMessage(message);
}
};
public RemoteViewsAIDLService() {
}
@Override
public IBinder onBind(Intent intent) {
return remoteViewsBinder;
}
}
MainActivity.java
package com.czhappy.remoteviewdemo.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.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 1: //RemoteViews的AIDL實(shí)現(xiàn)
RemoteViews remoteViews = msg.getData().getParcelable("remoteViews");
if (remoteViews != null) {
Log.i(TAG, "updateUI");
View view = remoteViews.apply(weakReference.get(), mLinearLayout);
mLinearLayout.addView(view);
}
break;
default:
break;
}
}
};
public void readyGo(View view){
Intent intent = new Intent(MainActivity.this, Temp2Activity.class);
startActivity(intent);
}
}
Temp2Activity.java
package com.czhappy.remoteviewdemo.activity;
import android.app.PendingIntent;
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 android.widget.RemoteViews;
import com.czhappy.remoteviewdemo.IremoteViewsManager;
import com.czhappy.remoteviewdemo.R;
import com.czhappy.remoteviewdemo.service.RemoteViewsAIDLService;
/**
* Description:
* User: chenzheng
* Date: 2017/2/9 0009
* Time: 16:05
*/
public class Temp2Activity extends AppCompatActivity {
private String TAG = "Temp2Activity";
private IremoteViewsManager remoteViewsManager;
private boolean isBind = false;
private ServiceConnection remoteViewServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG,"onServiceConnected");
remoteViewsManager = IremoteViewsManager.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
//回收
remoteViewsManager = null;
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_temp);
}
/**
* 綁定服務(wù)
*/
public void bindService(View view) {
Intent viewServiceIntent = new Intent(this,RemoteViewsAIDLService.class);
isBind = bindService(viewServiceIntent,remoteViewServiceConnection, Context.BIND_AUTO_CREATE);
}
/**
* 更新UI
*/
public void UpdateUI(View view){
RemoteViews remoteViews = new RemoteViews(Temp2Activity.this.getPackageName(),R.layout.button_layout);
Intent intentClick = new Intent(Temp2Activity.this,FirstActivity.class);
PendingIntent openFirstActivity = PendingIntent.getActivity(Temp2Activity.this,0,intentClick,0);
remoteViews.setOnClickPendingIntent(R.id.firstButton,openFirstActivity);
Intent secondClick = new Intent(Temp2Activity.this,SecondActivity.class);
PendingIntent openSecondActivity = PendingIntent.getActivity(Temp2Activity.this,0,secondClick,0);
remoteViews.setOnClickPendingIntent(R.id.secondButton,openSecondActivity);
remoteViews.setCharSequence(R.id.secondButton,"setText","想改就改");
try {
remoteViewsManager.addRemoteView(remoteViews);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(isBind){
unbindService(remoteViewServiceConnection);
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.FirstActivity" />
<activity android:name=".activity.SecondActivity" />
<activity
android:name=".activity.Temp2Activity"
android:process=":remote2" />
<service android:name="com.czhappy.remoteviewdemo.service.RemoteViewsAIDLService" />
</application>
</manifest>
四、總結(jié)
RemoteViews就是為跨進(jìn)程更新UI而生的,內(nèi)部封裝了很多方法用來實(shí)現(xiàn)跨進(jìn)程更新UI。但這并不代表RemoteViews是就是萬能的了,它也有不足之處,目前支持的布局和View有限
layout:
FrameLayout LinearLayout RelativeLayout GridLayout
View:
AnalogClock button Chronometer ImageButton ImageView ProgressBar TextView ViewFlipper ListView GridView StackView AdapterViewFlipper ViewStub
不支持自定義View 所以具體使用RemoteViews還是aidl或者BroadCastReceiver還得看實(shí)際的需求。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
- Android 跨進(jìn)程模擬按鍵(KeyEvent )實(shí)例詳解
- Android AIDL實(shí)現(xiàn)兩個(gè)APP間的跨進(jìn)程通信實(shí)例
- Android編程實(shí)現(xiàn)AIDL(跨進(jìn)程通信)的方法詳解
- Android IPC機(jī)制利用Messenger實(shí)現(xiàn)跨進(jìn)程通信
- 詳解Android跨進(jìn)程IPC通信AIDL機(jī)制原理
- Android 跨進(jìn)程SharedPreferences異常詳解
- Android跨進(jìn)程拋異常的原理的實(shí)現(xiàn)
- Android 跨進(jìn)程通Messenger(簡(jiǎn)單易懂)
- Android實(shí)現(xiàn)跨進(jìn)程接口回掉的方法
相關(guān)文章
Framework源碼面試之a(chǎn)ctivity啟動(dòng)流程
這篇文章主要為大家介紹了Framework源碼面試之a(chǎn)ctivity啟動(dòng)流程實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android Studio用genymotion運(yùn)行后小圖標(biāo)無法顯示問題
這篇文章主要介紹了Android Studio用genymotion運(yùn)行后小圖標(biāo)無法顯示的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android實(shí)現(xiàn)繪制折線圖APP代碼
大家好,本篇文章主要講的是Android實(shí)現(xiàn)繪制折線圖APP代碼,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
android屏蔽按鈕連續(xù)點(diǎn)擊的示例代碼
這篇文章主要介紹了android屏蔽按鈕連續(xù)點(diǎn)擊的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
開源自研內(nèi)存分析利器Android?Bitmap?Monitor圖片定位詳解
這篇文章主要為大家介紹了Android?Bitmap?Monitor開源自研內(nèi)存分析利器,助你定位不合理的圖片使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
使用 Swift 語言編寫 Android 應(yīng)用入門
為了能順利使用這份向?qū)В阈枰? 1. 可以編譯Swift源碼的Linux環(huán)境。stdlib目前只能在Linux環(huán)境下編譯成安卓可用版本。在嘗試為安卓構(gòu)建之前,確保你能夠參考Swift項(xiàng)目的README為L(zhǎng)inux做編譯。2016-04-04
Android 解決使用SearchView時(shí)軟鍵盤不支持actionSearch的問題
本文主要介紹使用SearchView時(shí)軟鍵盤不支持actionSearch,這里提供了解決方案,希望能幫助開發(fā)Android應(yīng)用的同學(xué)2016-07-07
Android中ListView設(shè)置靜態(tài)數(shù)據(jù)的方法
這篇文章主要介紹了Android中ListView設(shè)置靜態(tài)數(shù)據(jù)的方法,如何為L(zhǎng)istView設(shè)置靜態(tài)數(shù)據(jù),感興趣的小伙伴們可以參考一下2015-12-12

