【问题标题】:How to get the Widget state/view using the widget id in android?如何使用 android 中的小部件 id 获取小部件状态/视图?
【发布时间】:2021-12-28 15:27:05
【问题描述】:

我创建了一个简单的小部件,其中有一个文本视图,它会在 n 次后用一些新字符串更新。 现在,当用户单击该小部件时,我希望将当前显示在该文本视图中的文本复制到用户的剪贴板。

到目前为止,我知道如何获取小部件 ID。

   private fun getPendingSelfIntent(context: Context, id: Int): PendingIntent {
        val intent = Intent(context, this::class.java)
        intent.action = "ROOT_CLICK"
        intent.putExtra("widget id", id)
        return PendingIntent.getBroadcast(context, 0, intent, 0)
    }

是否有任何视图可以让我使用该小部件的小部件 ID 获取该文本视图的状态。

val appWidgetManager = AppWidgetManager.getInstance(context)
        val thisAppWidgetComponentName = ComponentName(context!!.packageName, this::class.java.name)
        val appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidgetComponentName)
        for(widgetId in appWidgetIds) {
            if(widgetId == id) {
                val temp = appWidgetManager.getAppWidgetInfo(id)
                // what should i do here, so that i can get the view of this particular widget
            }
        }

【问题讨论】:

    标签: android widget android-widget


    【解决方案1】:
    //1 Store a string value by call 
    
        Bundle bundle = appWidgetManager.getAppWidgetOptions( appWidgetId);
        bundle.putString( name, value);
        appWidgetManager.updateAppWidgetOptions(appWidgetId, bundle );
    
       ...
    //2. Get a string value:
    
        Bundle bundle = appWidgetManager.getAppWidgetOptions( appWidgetId);
        String value = bundle.getString( name );
    

    【讨论】:

      猜你喜欢
      • 2021-08-27
      • 2014-04-30
      • 2021-09-21
      • 2021-12-24
      • 2013-10-20
      • 1970-01-01
      • 2019-12-16
      • 2021-02-16
      • 2019-11-29
      相关资源
      最近更新 更多