实现一个桌面小部件,里面一个按钮一个textview,点击按钮开启服务,5秒内每秒更新data,并发送广播更新textview的text
1.先在AndroidManifest.xml中定义桌面小部件(类似广播的静态注册)
<receiver
android:name=".AppWidget"
android:exported="false">
<intent-filter>
<!--点击按钮-->
<action android:name="action_button" />
</intent-filter>
<intent-filter>
<!--更新数据到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"是设置桌面小部件在小部件列表中的图标
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生成的小工具图标,小工具就会出现在桌面上了。
|