本文介绍: 实现一个桌面部件,里面一个按钮一个textview点击按钮开启服务,5秒内每秒更新data,并发送广播更新textviewtext1.先在AndroidManifest.xml定义桌面部件(类似广播的静态注册)<receiver android:name=”.AppWidgetandroid:exported=”false“&gt; <intent-filter&gt;

实现一个桌面部件,里面一个按钮一个textview,点击按钮开启服务,5秒内每秒更新data,并发送广播更新textviewtext

1.先在AndroidManifest.xml中定义桌面部件(类似广播的静态注册

<receiver
            android:name=".AppWidget"
            android:exported="false"&gt;
            <intent-filter&gt;
                <!--点击按钮--&gt;
                <action android:name="action_button" /&gt;
            </intent-filter&gt;
            <intent-filter&gt;
                <!--更新数据textview-->
                <action android:name="action_update" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/appwidget" />
        </receiver>

2.定义xml文件

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/appwidget_layout"
    android:minHeight="40dp"
    android:minWidth="200dp"
    android:previewImage="@mipmap/ic_launcher"
    android:resizeMode="vertical|horizontal"
    android:updatePeriodMillis="0"
    android:widgetCategory="home_screen"/>

    android:previewImage=”@mipmap/ic_launcher”是设置桌面部件在小部件列表中的图标

  widgetCategory 属性声明应用微件是否可以显示在主屏幕 (home_screen) 和/或锁定屏幕 (keyguard) 上。只有低于 5.0 的 Android 版本支持锁定屏幕微件。对于 Android 5.0 及更高版本,只有 home_screen 有效

3.定义小部件布局文件

<?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:orientation="horizontal">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="80dp"
        android:text="text" />
</LinearLayout>

4.实现AppWidget

package com.example.appwidgetdemo

import android.annotation.SuppressLint
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.widget.RemoteViews

class AppWidget:AppWidgetProvider() {
    companion object{
        private const val ACTION_BUTTON = "action_button"
         const val ACTION_UPDATE="action_update"

    }
    @SuppressLint("RemoteViewLayout")
    override fun onReceive(context: Context?, intent: Intent?) {
        super.onReceive(context, intent)
        if(intent==null||context==null){
            return
        }
        val action =intent.action
        //更新textview的text
        if (TextUtils.equals(action, ACTION_UPDATE)){
            val data=intent.getStringExtra("data")
            val remoteViews=RemoteViews(context.packageName,R.layout.appwidget_layout)
            remoteViews.setTextViewText(R.id.tv,data)
            val appWidgetManager=AppWidgetManager.getInstance(context)
            val componentName=ComponentName(context,AppWidget::class.java)
            appWidgetManager.updateAppWidget(componentName,remoteViews)
        }else if (action== ACTION_BUTTON){//启动服务,5秒内每秒更新一次数据
            val serviceIntent=Intent(context,MyService::class.java)
            context.startService(serviceIntent)
        }

    }
     /**
	 * 到达指定的更新时间或者当用户向桌面添加AppWidget时被调用
	 * appWidgetIds:桌面上所有的widget都会被分配一个唯一的ID标识,这个数组就是他们的列表
	 */
    override fun onUpdate(
        context: Context?,
        appWidgetManager: AppWidgetManager?,
        appWidgetIds: IntArray?
    ) {
        super.onUpdate(context, appWidgetManager, appWidgetIds)
        if (context!=null) {
            此处的myBroadCast应该与AndroidManifest.xml文件中的保持一致
            val intent = Intent(ACTION_BUTTON)
            //在androi9.0以上版本需要设置包名
            intent.`package`=context.packageName
            val pendingIntent = PendingIntent.getBroadcast(context,0,intent,0)
            val remoteViews=RemoteViews(context.packageName,R.layout.appwidget_layout)
            remoteViews.setOnClickPendingIntent(R.id.btn,pendingIntent)
            appWidgetManager?.updateAppWidget(appWidgetIds,remoteViews)
        }
    }
}

注意:发送广播时定义的intent,在Android 9.0以上需要设置包名 ,广播才能发送出去

5.定义service

package com.example.appwidgetdemo

import android.app.IntentService
import android.content.Intent

class MyService :IntentService("MyService") {
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
    }
    override fun onHandleIntent(p0: Intent?) {
        for (i in 0 until 5){
            Thread.sleep(1000)
            val intent=Intent(AppWidget.ACTION_UPDATE)
            intent.`package`=this.packageName
            intent.putExtra("data", "data$i")
            sendBroadcast(intent)
        }
    }
}

6.将桌面小部件显示在桌面上:

长按手机桌面空白区域进入编辑状态,点击桌面小工具,会出现桌面小工具列表,点击demo生成的小工具图标,小工具就会出现在桌面上了。

原文地址:https://blog.csdn.net/qq_34332698/article/details/124339636

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_8493.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注