一、概述

前面一篇文章Android通过AIDL实现跨进程更新UI我们学习了aidl跨进程更新ui,这种传统方式实现跨进程更新UI是可行的,但有以下弊端:
接下来我们通过RemoteViews实现跨进程更新UI
二、实现效果图
在同一个应用中有两个Activity,MainActivity和Temp2Activity,这两个Activity不在同一个进程中。
现在需要通过Temp2Activity来改变MainActivity中的视图,即在MainActivity中添加两个Button,也就是实现跨进程更新UI这么一个功能。
在MainActivity里点击“跳转到新进程ACTIVITY”按钮,会启动一个新进程的Temp2Activity,我们先点击“绑定服务”,这样我们就启动了服务,再点击“AIDL更新”按钮,通过调用handler来实现跨进程更新UI,点击返回,我们发现MainActivity页面中新添加了两个按钮,并且按钮还具有点击事件。
三、核心代码
IremoteViewsManager.aidl
里面提供了两个方法,一个是根据id更新TextView里面的内容,一个是根据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实现
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);
}
/**
* 绑定服务
*/
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>
四、总结
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还得看实际的需求。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# android
# remoteviews
# 更新ui
# Android应用程序四大组件之使用AIDL如何实现跨进程调用Service
# Android 跨进程模拟按键(KeyEvent )实例详解
# Android AIDL实现两个APP间的跨进程通信实例
# Android编程实现AIDL(跨进程通信)的方法详解
# Android IPC机制利用Messenger实现跨进程通信
# 详解Android跨进程IPC通信AIDL机制原理
# Android 跨进程SharedPreferences异常详解
# Android跨进程抛异常的原理的实现
# Android 跨进程通Messenger(简单易懂)
# Android实现跨进程接口回掉的方法
# 不支持
# 绑定
# 是个
# 也有
# 中有
# 这两个
# 自定义
# 还得
# 但这
# 并不代表
# 跳转
# 涉及到
# 比较多
# 来实现
# 装了
# 而生
# 即在
# 一篇文章
# 到新
# 大家多多
相关文章:
北京制作网站的公司排名,北京三快科技有限公司是做什么?北京三快科技?
如何在阿里云ECS服务器部署织梦CMS网站?
如何在阿里云通过域名搭建网站?
浅析上传头像示例及其注意事项
公众号网站制作网页,微信公众号怎么制作?
网站制作公司排行榜,四大门户网站排名?
动图在线制作网站有哪些,滑动动图图集怎么做?
表情包在线制作网站免费,表情包怎么弄?
定制建站是什么?如何实现个性化需求?
广德云建站网站建设方案与建站流程优化指南
电影网站制作价格表,那些提供免费电影的网站,他们是怎么盈利的?
网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?
如何制作算命网站,怎么注册算命网站?
建站上市公司网站建设方案与SEO优化服务定制指南
宝盒自助建站智能生成技巧:SEO优化与关键词设置指南
电商网站制作价格怎么算,网上拍卖流程以及规则?
定制建站策划方案_专业建站与网站建设方案一站式指南
如何通过wdcp面板快速创建网站?
建站之星上传入口如何快速找到?
XML的“混合内容”是什么 怎么用DTD或XSD定义
如何用y主机助手快速搭建网站?
网站设计制作书签怎么做,怎样将网页添加到书签/主页书签/桌面?
寿县云建站:智能SEO优化与多行业模板快速上线指南
浅谈Javascript中的Label语句
在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?
成都网站制作报价公司,成都工业用气开户费用?
岳西云建站教程与模板下载_一站式快速建站系统操作指南
为什么Go需要go mod文件_Go go mod文件作用说明
网站制作软件免费下载安装,有哪些免费下载的软件网站?
如何用狗爹虚拟主机快速搭建网站?
SQL查询语句优化的实用方法总结
婚礼视频制作网站,学习*后期制作的网站有哪些?
如何快速搭建FTP站点实现文件共享?
相亲简历制作网站推荐大全,新相亲大会主持人小萍萍资料?
Android滚轮选择时间控件使用详解
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
北京建设网站制作公司,北京古代建筑博物馆预约官网?
实惠建站价格推荐:2025年高性价比自助建站套餐解析
教育培训网站制作流程,请问edu教育网站的域名怎么申请?
如何在建站之星绑定自定义域名?
高防服务器租用如何选择配置与防御等级?
家庭服务器如何搭建个人网站?
建站之星后台密码如何安全设置与找回?
如何配置FTP站点权限与安全设置?
广州建站公司哪家好?十大优质服务商推荐
如何在阿里云高效完成企业建站全流程?
网站制作的步骤包括,正确网址格式怎么写?
如何配置IIS站点权限与局域网访问?
西安制作网站公司有哪些,西安货运司机用的最多的app或者网站是什么?
Swift中循环语句中的转移语句 break 和 continue
*请认真填写需求信息,我们会在24小时内与您取得联系。