繁体   English   中英

如何使用 android 中的小部件 id 获取小部件状态/视图?

[英]How to get the Widget state/view using the widget id in android?

我创建了一个简单的小部件,其中有一个文本视图,它在 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 获得该文本视图的 state。

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
            }
        }
//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 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM