RemoteViews的用法

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>



作者:名字_都被占了
链接:https://www.jianshu.com/p/6b31812ed5fa
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值