查看: 940|回复: 0

[手机开发] Android通过RemoteViews实现跨进程更新UI示例

发表于 2017-8-1 08:00:04
尚学堂AD

一、概述

前面一篇文章Android通过AIDL实现跨进程更新UI我们学习了aidl跨进程更新ui,这种传统方式实现跨进程更新UI是可行的,但有以下弊端:

  • View中的方法数比较多,在IPC中需要增加对应的方法比较繁琐。
  • View的每一个方法都会涉及到IPC操作,多次IPC带来的开销问题不容小觑。
  • View中方法的某些参数可能不支持IPC传输。例如:OnClickListener,它仅仅是个接口没有序列化。

接下来我们通过RemoteViews实现跨进程更新UI

二、实现效果图

在同一个应用中有两个Activity,MainActivity和Temp2Activity,这两个Activity不在同一个进程中。

这里写图片描述

现在需要通过Temp2Activity来改变MainActivity中的视图,即在MainActivity中添加两个Button,也就是实现跨进程更新UI这么一个功能。

在MainActivity里点击“跳转到新进程ACTIVITY”按钮,会启动一个新进程的Temp2Activity,我们先点击“绑定服务”,这样我们就启动了服务,再点击“AIDL更新”按钮,通过调用handler来实现跨进程更新UI,点击返回,我们发现MainActivity页面中新添加了两个按钮,并且按钮还具有点击事件。

这里写图片描述

三、核心代码

IremoteViewsManager.aidl

里面提供了两个方法,一个是根据id更新TextView里面的内容,一个是根据id添加view视图

  1. // IremoteViewsManager.aidl.aidl
  2. package com.czhappy.remoteviewdemo;
  3. interface IremoteViewsManager {
  4. void addRemoteView(in RemoteViews remoteViews);
  5. }
复制代码

RemoteViewsAIDLService.Java

  1. package com.czhappy.remoteviewdemo.service;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.Binder;
  5. import android.os.Bundle;
  6. import android.os.IBinder;
  7. import android.os.Message;
  8. import android.os.RemoteException;
  9. import android.widget.RemoteViews;
  10. import com.czhappy.remoteviewdemo.IremoteViewsManager;
  11. import com.czhappy.remoteviewdemo.activity.MainActivity;
  12. /**
  13. * Description:
  14. * User: chenzheng
  15. * Date: 2017/2/10 0010
  16. * Time: 10:56
  17. */
  18. public class RemoteViewsAIDLService extends Service {
  19. private static final String TAG = "RemoteViewsAIDLService";
  20. private Binder remoteViewsBinder = new IremoteViewsManager.Stub(){
  21. @Override
  22. public void addRemoteView(RemoteViews remoteViews) throws RemoteException {
  23. Message message = new Message();
  24. message.what = 1;
  25. Bundle bundle = new Bundle();
  26. bundle.putParcelable("remoteViews",remoteViews);
  27. message.setData(bundle);
  28. new MainActivity.MyHandler(RemoteViewsAIDLService.this,getMainLooper()).sendMessage(message);
  29. }
  30. };
  31. public RemoteViewsAIDLService() {
  32. }
  33. @Override
  34. public IBinder onBind(Intent intent) {
  35. return remoteViewsBinder;
  36. }
  37. }
复制代码

MainActivity.java

  1. package com.czhappy.remoteviewdemo.activity;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Looper;
  7. import android.os.Message;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.util.Log;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.widget.LinearLayout;
  13. import android.widget.RemoteViews;
  14. import android.widget.TextView;
  15. import com.czhappy.remoteviewdemo.R;
  16. import java.lang.ref.WeakReference;
  17. public class MainActivity extends AppCompatActivity {
  18. private static String TAG = "MainActivity";
  19. private static LinearLayout mLinearLayout;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. mLinearLayout = (LinearLayout) this.findViewById(R.id.mylayout);
  25. }
  26. public static class MyHandler extends Handler {
  27. WeakReference<Context> weakReference;
  28. public MyHandler(Context context, Looper looper) {
  29. super(looper);
  30. weakReference = new WeakReference<>(context);
  31. }
  32. @Override
  33. public void handleMessage(Message msg) {
  34. super.handleMessage(msg);
  35. Log.i(TAG, "handleMessage");
  36. switch (msg.what) {
  37. case 1: //RemoteViews的AIDL实现
  38. RemoteViews remoteViews = msg.getData().getParcelable("remoteViews");
  39. if (remoteViews != null) {
  40. Log.i(TAG, "updateUI");
  41. View view = remoteViews.apply(weakReference.get(), mLinearLayout);
  42. mLinearLayout.addView(view);
  43. }
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. };
  50. public void readyGo(View view){
  51. Intent intent = new Intent(MainActivity.this, Temp2Activity.class);
  52. startActivity(intent);
  53. }
  54. }
复制代码

Temp2Activity.java

  1. package com.czhappy.remoteviewdemo.activity;
  2. import android.app.PendingIntent;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Bundle;
  8. import android.os.IBinder;
  9. import android.os.RemoteException;
  10. import android.support.annotation.Nullable;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.RemoteViews;
  15. import com.czhappy.remoteviewdemo.IremoteViewsManager;
  16. import com.czhappy.remoteviewdemo.R;
  17. import com.czhappy.remoteviewdemo.service.RemoteViewsAIDLService;
  18. /**
  19. * Description:
  20. * User: chenzheng
  21. * Date: 2017/2/9 0009
  22. * Time: 16:05
  23. */
  24. public class Temp2Activity extends AppCompatActivity {
  25. private String TAG = "Temp2Activity";
  26. private IremoteViewsManager remoteViewsManager;
  27. private boolean isBind = false;
  28. private ServiceConnection remoteViewServiceConnection = new ServiceConnection() {
  29. @Override
  30. public void onServiceConnected(ComponentName name, IBinder service) {
  31. Log.i(TAG,"onServiceConnected");
  32. remoteViewsManager = IremoteViewsManager.Stub.asInterface(service);
  33. }
  34. @Override
  35. public void onServiceDisconnected(ComponentName name) {
  36. //回收
  37. remoteViewsManager = null;
  38. }
  39. };
  40. @Override
  41. protected void onCreate(@Nullable Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. this.setContentView(R.layout.activity_temp);
  44. }
  45. /**
  46. * 绑定服务
  47. */
  48. public void bindService(View view) {
  49. Intent viewServiceIntent = new Intent(this,RemoteViewsAIDLService.class);
  50. isBind = bindService(viewServiceIntent,remoteViewServiceConnection, Context.BIND_AUTO_CREATE);
  51. }
  52. /**
  53. * 更新UI
  54. */
  55. public void UpdateUI(View view){
  56. RemoteViews remoteViews = new RemoteViews(Temp2Activity.this.getPackageName(),R.layout.button_layout);
  57. Intent intentClick = new Intent(Temp2Activity.this,FirstActivity.class);
  58. PendingIntent openFirstActivity = PendingIntent.getActivity(Temp2Activity.this,0,intentClick,0);
  59. remoteViews.setOnClickPendingIntent(R.id.firstButton,openFirstActivity);
  60. Intent secondClick = new Intent(Temp2Activity.this,SecondActivity.class);
  61. PendingIntent openSecondActivity = PendingIntent.getActivity(Temp2Activity.this,0,secondClick,0);
  62. remoteViews.setOnClickPendingIntent(R.id.secondButton,openSecondActivity);
  63. remoteViews.setCharSequence(R.id.secondButton,"setText","想改就改");
  64. try {
  65. remoteViewsManager.addRemoteView(remoteViews);
  66. } catch (RemoteException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. @Override
  71. protected void onDestroy() {
  72. super.onDestroy();
  73. if(isBind){
  74. unbindService(remoteViewServiceConnection);
  75. isBind = false;
  76. }
  77. }
  78. }
复制代码

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.czhappy.remoteviewdemo">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme">
  10. <activity android:name=".activity.MainActivity">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. <activity android:name=".activity.FirstActivity" />
  17. <activity android:name=".activity.SecondActivity" />
  18. <activity
  19. android:name=".activity.Temp2Activity"
  20. android:process=":remote2" />
  21. <service android:name="com.czhappy.remoteviewdemo.service.RemoteViewsAIDLService" />
  22. </application>
  23. </manifest>
复制代码

四、总结

RemoteViews就是为跨进程更新UI而生的,内部封装了很多方法用来实现跨进程更新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还得看实际的需求。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报