RemoteViews在Android中,主要用在通知栏(可参考《Android中通知栏的使用》)和桌面小部件(可参考《Android中小部件的使用》。然而,其实除了提到这两方面使用外,我们在实际开发中也会使用到RemoteViews。例如,应用程序中存在两个进程,A进程需要更新B进程的某个界面,这时可以选择AIDL去实现,但如果对界面的更新比较频繁,这时就会有效率问题,同时AIDL接口就可能会变得很复杂。此时如果界面中的View是一些简单的且被RemoteViews支持的View,那可考虑采用RemoteViews去实现。
实例
首先有两个Activity,它们分别运行在两个不同的进程中,发送的Activity我们叫它Activity2,而接收被更新的Activity,我们叫它Activity1。那么我们先来看看发送方Activity2的代码:
public class Main2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button btnSend = (Button)findViewById(R.id.btn_send);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_test_view);
remoteViews.setTextViewText(R.id.txt_msg, "我的进程是:" + android.os.Process.myPid());
remoteViews.setImageViewResource(R.id.icon, R.mipmap.ic_launcher);
PendingIntent pendingIntent = PendingIntent.getActivity(Main2Activity.this, 0, new Intent(Main2Activity.this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.item_holder, pendingIntent);
Intent intent = new Intent(MainActivity.UPDATE_UI_ACTION);
intent.putExtra(MainActivity.EXTRA_REMOTE_VIEWS, remoteViews);
sendBroadcast(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="10dp"
android:orientation="vertical" >
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="给Activity1更新界面" />
</LinearLayout>
在Activity2中存在一个按钮,点击按钮后,构造一个RemoveViews对象,然后以广播的形式传递给接收UPDATE_UI_ACTION的Activity1。
接着我们看看Activity1的代码:
public class MainActivity extends Activity {
public static final String UPDATE_UI_ACTION = "project.test.com.remoteviewsdemo.MAIN_ACTIVITY_UPDATE_UI";
public static final String EXTRA_REMOTE_VIEWS = "extra_remoteViews";
private LinearLayout mRemoteViewsContent;
private BroadcastReceiver mRemoteViewsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!UPDATE_UI_ACTION.equalsIgnoreCase(intent.getAction())) {
return;
}
RemoteViews remoteViews = intent.getParcelableExtra(EXTRA_REMOTE_VIEWS);
if (remoteViews != null) {
View view = remoteViews.apply(MainActivity.this, mRemoteViewsContent);
mRemoteViewsContent.addView(view);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRemoteViewsContent = (LinearLayout) findViewById(R.id.remote_views_content);
Button btnGotoActivity2 = (Button)findViewById(R.id.btn_goto_activity);
btnGotoActivity2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
IntentFilter filter = new IntentFilter(UPDATE_UI_ACTION);
registerReceiver(mRemoteViewsReceiver, filter);
}
@Override
protected void onDestroy() {
unregisterReceiver(mRemoteViewsReceiver);
super.onDestroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="10dp"
android:orientation="vertical" >
<Button
android:id="@+id/btn_goto_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开Activity2" />
<LinearLayout
android:id="@+id/remote_views_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Activity1在onCreate中动态注册了广播mRemoteViewsReceiver,当接收到广播后获得RemoteViews对象,然后执行RemoteViews的apply方法将在Activity2中的RemoteViews加载起来并更新之前在Activity2中的一些set方法,最终将得到的View对象add到界面中的LinearLayout显示出来。