美文网首页
RemoteViews的用法

RemoteViews的用法

作者: 名字_都被占了 | 来源:发表于2018-05-21 16:40 被阅读0次
RemoteViews所支持的View类型如下图所示(注意不支持下图中View的子类):
RemoteViews所支持的View的类型
RemoteViews没有提供findViewById方法,因为RemoteViews在远程进程中显示,因此无法直接访问里面的View元素,而必须通过RemoteViews所提供的一系列set方法来完成,部分set方法如下所示:

1:在Notification中的应用

示例代码如下:

    private Button button;
    private Notification notification;
    private NotificationManager notificationManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ceshi);
        button = findViewById(R.id.button);
        Intent intent=new Intent(this, com.example.liang.arlvyou.lanjiazai.ar.MainActivity.class);
        Intent remote=new Intent(this, RemoteActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent remoteIntent=PendingIntent.getActivity(this,0,remote,PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.remoteview);
        remoteViews.setTextViewText(R.id.textView2,"我是remoteview");
        remoteViews.setImageViewResource(R.id.image,R.drawable.icon);
        remoteViews.setOnClickPendingIntent(R.id.button2,remoteIntent);
        notification=new Notification();//注意Notification创建得用new的方式了创建,因为
        //如果用Builder的方式创建的话,在设置remoteview的时候会用到setCustomContentView方法,
        //该方法需要api24才能使用,api版本太高了,但是使用new的方式就可以不用考虑低api的问题。
        notification.icon=R.mipmap.ic_launcher;
        notification.tickerText="hello,world";
        notification.when=System.currentTimeMillis();
        notification.contentView=remoteViews;
        notification.contentIntent=pendingIntent;
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notificationManager.notify(1,notification);
            }
        });
    }

remoteview文件中的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"/>
    <ImageView
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:id="@+id/image"/>

    <Button
        android:text="Button"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:id="@+id/button2"/>
</LinearLayout>

2:在桌面小组件中的应用,直接运行应用就行,都不需要打开应用,桌面小组件就已经有了。

示例代码如下:

public class MyAppWidgetProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d("MyAppWidgetProvider", "调用了onUpdate方法");
        Intent intent = new Intent(context, com.example.liang.arlvyou.lanjiazai.ar.MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remoteview);
        remoteViews.setImageViewResource(R.id.image, R.drawable.icon);
        remoteViews.setTextViewText(R.id.textView2, "我是桌面小组件");
        remoteViews.setOnClickPendingIntent(R.id.textView2, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);//用updateAppWidget来更新remoteViews的状态
    }
}

在androidmanifest.xml文件中

...
   <receiver android:name=".lanjiazai.MyAppWidgetProvider">
            <meta-data
                android:name="android.appwidget.provider" <!--name的值是固定的,只能是android.appwidget.provider-->
                android:resource="@xml/widget"/>
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/><!--必须得有这个action-->
            </intent-filter>
        </receiver>
...

在res的xml文件夹下创建桌面组件的信息文件,比如widget.xml,文件内容如下

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/remoteview"
    android:minWidth="100dp"
    android:minHeight="100dp"
    android:updatePeriodMillis="86400000">
</appwidget-provider>

相关文章

网友评论

      本文标题:RemoteViews的用法

      本文链接:https://www.haomeiwen.com/subject/omqqjftx.html