当前位置:   article > 正文

Android 小组件完整案例

android 小组件

介绍一下android桌面小组件完整的案例,包括:

  • 注册一个小组件(系统中能找到并添加小组件)
  • 更新小组件数据
  • 应用主动添加小组件
  • 小组件中的坑

AndroidManifest.xml:  (小组件注册, 其本质是一个BroadcastReceiver)

  1. ....
  2. <receiver android:name=".appwidget.HanlonglinWidgetProvider"
  3. android:exported="false">
  4. <intent-filter>
  5. <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  6. </intent-filter>
  7. <meta-data android:name="android.appwidget.provider"
  8. android:resource="@xml/app_widget" />
  9. </receiver>
  10. ....

xml/app_widget.xml: (小组件配置信息)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:minWidth="90dp"
  4. android:minHeight="90dp"
  5. android:updatePeriodMillis="1800000"
  6. android:initialLayout="@layout/moment_widget_moment"
  7. android:widgetCategory="home_screen">
  8. </appwidget-provider>
HanlonglinWidgetProvider.kt: (小组件绑定view的类)
  1. package com.dragon.test.hanlonglindemo.appwidget
  2. import android.app.PendingIntent
  3. import android.appwidget.AppWidgetManager
  4. import android.appwidget.AppWidgetProvider
  5. import android.content.Context
  6. import android.content.Intent
  7. import android.util.Log
  8. import android.widget.RemoteViews
  9. import com.dragon.test.hanlonglindemo.MainActivity
  10. import com.dragon.test.hanlonglindemo.R
  11. /***
  12. * create by DragonForest on 2022/4/15
  13. */
  14. class HanlonglinWidgetProvider: AppWidgetProvider() {
  15. private val TAG = this.javaClass.simpleName
  16. override fun onUpdate(
  17. context: Context?,
  18. appWidgetManager: AppWidgetManager?,
  19. appWidgetIds: IntArray?
  20. ) {
  21. Log.i(TAG, "HanlonglinWidgetProvider.onUpdate")
  22. appWidgetIds?.forEach { appWidgetId ->
  23. val views: RemoteViews = RemoteViews(
  24. context?.packageName,
  25. R.layout.widget_layout
  26. )
  27. var intent = Intent(context, MainActivity::class.java)
  28. var pendingIntent = PendingIntent.getActivity(
  29. context,
  30. 0,
  31. intent,
  32. PendingIntent.FLAG_UPDATE_CURRENT
  33. )
  34. views.setOnClickPendingIntent(R.id.iv_widget, pendingIntent)
  35. appWidgetManager?.updateAppWidget(appWidgetId, views)
  36. }
  37. }
  38. companion object{
  39. /**
  40. * 更新组件调用
  41. */
  42. fun updateAppWidget(
  43. context: Context?,
  44. appWidgetManager: AppWidgetManager?,
  45. appWidgetId: Int,
  46. imgRes: Int
  47. ){
  48. val views = RemoteViews(context!!.packageName, R.layout.widget_layout)
  49. views.setImageViewResource(R.id.iv_widget,imgRes)
  50. appWidgetManager?.updateAppWidget(appWidgetId,views)
  51. }
  52. }
  53. }
WidgetController.kt: (小组件操作类)
  1. package com.dragon.test.hanlonglindemo.appwidget
  2. import android.app.PendingIntent
  3. import android.appwidget.AppWidgetManager
  4. import android.content.ComponentName
  5. import android.content.Context
  6. import android.content.Intent
  7. import android.os.Build
  8. import android.os.Bundle
  9. import android.widget.Toast
  10. import androidx.annotation.RequiresApi
  11. /***
  12. * 小组件操作类
  13. * create by DragonForest on 2022/4/15
  14. */
  15. object WidgetController {
  16. private val TAG = this.javaClass.simpleName
  17. /**
  18. * 更新小组件,触发组件的onUpdate
  19. */
  20. fun update(context: Context){
  21. var intent = Intent(context,HanlonglinWidgetProvider::class.java)
  22. intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
  23. var bundle = Bundle()
  24. bundle.putIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS, getAppWidgetIds(context))
  25. intent.putExtras(bundle)
  26. context.sendBroadcast(intent)
  27. }
  28. /**
  29. * 添加到主屏幕
  30. */
  31. @RequiresApi(Build.VERSION_CODES.O)
  32. fun addToMainScreen(context: Context){
  33. val appWidgetManager = AppWidgetManager.getInstance(context)
  34. val myProvider = ComponentName(context, HanlonglinWidgetProvider::class.java)
  35. if (getAppWidgetIds(context).isNotEmpty()) {
  36. Toast.makeText(context,"组件已经存在",Toast.LENGTH_SHORT).show()
  37. return
  38. }
  39. if (appWidgetManager.isRequestPinAppWidgetSupported()) {
  40. // Create the PendingIntent object only if your app needs to be notified
  41. // that the user allowed the widget to be pinned. Note that, if the pinning
  42. // operation fails, your app isn't notified. This callback receives the ID
  43. // of the newly-pinned widget (EXTRA_APPWIDGET_ID).
  44. // val successCallback = PendingIntent.getBroadcast(
  45. // /* context = */ context,
  46. // /* requestCode = */ 0,
  47. // /* intent = */ null,
  48. // /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT)
  49. appWidgetManager.requestPinAppWidget(myProvider, null, null)
  50. }
  51. }
  52. private fun getAppWidgetIds(context: Context) :IntArray{
  53. var awm = AppWidgetManager.getInstance(context)
  54. val appWidgetIDs: IntArray = awm.getAppWidgetIds(ComponentName(context,HanlonglinWidgetProvider::class.java))
  55. return appWidgetIDs
  56. }
  57. }
WidgetTestActivity.kt:  (测试页面,用来展示操作)
  1. package com.dragon.test.hanlonglindemo.appwidget
  2. import android.app.Activity
  3. import android.os.Build
  4. import android.os.Bundle
  5. import androidx.annotation.RequiresApi
  6. import com.dragon.test.hanlonglindemo.R
  7. import kotlinx.android.synthetic.main.activity_widget_test.*
  8. /***
  9. * create by DragonForest on 2022/4/15
  10. */
  11. class WidgetTestActivity: Activity() {
  12. @RequiresApi(Build.VERSION_CODES.O)
  13. override fun onCreate(savedInstanceState: Bundle?) {
  14. super.onCreate(savedInstanceState)
  15. setContentView(R.layout.activity_widget_test)
  16. btn_add_to_screen.setOnClickListener {
  17. WidgetController.addToMainScreen(this)
  18. }
  19. }
  20. }

注意的地方,

更多信息: 

Enable users to configure app widgets  |  Android Developers

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/313035
推荐阅读
相关标签
  

闽ICP备14008679号