<legend id='8GRUg'><style id='8GRUg'><dir id='8GRUg'><q id='8GRUg'></q></dir></style></legend>

  1. <tfoot id='8GRUg'></tfoot>

  2. <i id='8GRUg'><tr id='8GRUg'><dt id='8GRUg'><q id='8GRUg'><span id='8GRUg'><b id='8GRUg'><form id='8GRUg'><ins id='8GRUg'></ins><ul id='8GRUg'></ul><sub id='8GRUg'></sub></form><legend id='8GRUg'></legend><bdo id='8GRUg'><pre id='8GRUg'><center id='8GRUg'></center></pre></bdo></b><th id='8GRUg'></th></span></q></dt></tr></i><div id='8GRUg'><tfoot id='8GRUg'></tfoot><dl id='8GRUg'><fieldset id='8GRUg'></fieldset></dl></div>
      <bdo id='8GRUg'></bdo><ul id='8GRUg'></ul>

    <small id='8GRUg'></small><noframes id='8GRUg'>

      如何在活动和小部件之间共享数据?

      How to share data between activity and widget?(如何在活动和小部件之间共享数据?)

      <i id='nzIu5'><tr id='nzIu5'><dt id='nzIu5'><q id='nzIu5'><span id='nzIu5'><b id='nzIu5'><form id='nzIu5'><ins id='nzIu5'></ins><ul id='nzIu5'></ul><sub id='nzIu5'></sub></form><legend id='nzIu5'></legend><bdo id='nzIu5'><pre id='nzIu5'><center id='nzIu5'></center></pre></bdo></b><th id='nzIu5'></th></span></q></dt></tr></i><div id='nzIu5'><tfoot id='nzIu5'></tfoot><dl id='nzIu5'><fieldset id='nzIu5'></fieldset></dl></div>

      <small id='nzIu5'></small><noframes id='nzIu5'>

    1. <legend id='nzIu5'><style id='nzIu5'><dir id='nzIu5'><q id='nzIu5'></q></dir></style></legend>
    2. <tfoot id='nzIu5'></tfoot>

            • <bdo id='nzIu5'></bdo><ul id='nzIu5'></ul>
                  <tbody id='nzIu5'></tbody>
                本文介绍了如何在活动和小部件之间共享数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我阅读了 hellowidget 教程和开发指南的 App Widgets.然后我知道如何创建一个包含按钮或文本或其他内容的小部件.

                I read the hellowidget tutorial and Dev Guide' App Widgets. Then I know how to create a widget which contains button or text or something.

                但我真正想做的是让它与我的应用交互.例如,我想创建一个具有文本视图的小部件,当我单击它时,它会向我的活动发送一个 PendingIntent,我可以在其中编辑文本.

                But what I really want to do is making it interact with my app. For example, I want to create a widget that has a text view, and when I click it, it sends a PendingIntent to my activity in which I can edit the text.

                我可以执行发送 PendingIntent"步骤.但是我在活动中编辑文本后,小部件如何读取它?

                I can do the step "sends a PendingIntent". But after I edit text in acitivy, how does the widget read it?

                推荐答案

                你需要做的是注册一个自定义的intent,例如在你的AppWidgetProvider中注册一个ACTION_TEXT_CHANGED,例如:

                What you need to do is register a custom intent, for example ACTION_TEXT_CHANGED in your AppWidgetProvider like this for example:

                public static final String ACTION_TEXT_CHANGED = "yourpackage.TEXT_CHANGED";
                

                在此之后,您需要在您的 AndroidManifest.xml 中注册您希望在接收者标签的意图过滤器部分接收此意图,如下所示:

                After this, you need to register in your AndroidManifest.xml that you want to receive this intents in the intent-filter section of your receiver tag like this:

                <receiver android:name=".DrinkWaterAppWidgetProvider">
                    <intent-filter>
                        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                        <action android:name="yourpackage.TEXT_CHANGED" />                
                    </intent-filter>
                    <meta-data android:name="android.appwidget.provider"
                        android:resource="@xml/appwidget_info" />
                </receiver>
                

                然后你必须在你的 AppWidgetProvider 中扩展 onReceive 方法,并确保你处理你的意图是这样的:

                Then you have to extend the onReceive method in your AppWidgetProvider and make sure that you're handling your intent like this:

                @Override
                public void onReceive(Context context, Intent intent) {
                    super.onReceive(context, intent);
                    if (intent.getAction().equals(ACTION_TEXT_CHANGED)) {
                        // handle intent here
                        String s = intent.getStringExtra("NewString");
                    }
                }
                

                以上所有设置完成后,您只需要在文本更改后在活动中广播意图,如下所示:

                After all the above is set up, you just need to broadcast the intent in your activity after the text has changed like this:

                Intent intent = new Intent(YourAppWidgetProvider.ACTION_TEXT_CHANGED);
                intent.putExtra("NewString", textView.getText().toString());
                getApplicationContext().sendBroadcast(intent);
                

                其中NewString"应更改为您为字符串提供的名称.

                Where "NewString" should be changed to the name you to give the the string.

                希望对你有帮助.

                这篇关于如何在活动和小部件之间共享数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
                cocos2d-android: how to display score(cocos2d-android:如何显示分数)
                Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
                SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
                Android file copy(安卓文件拷贝)
                Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)
                • <bdo id='7Np9M'></bdo><ul id='7Np9M'></ul>
                    <tfoot id='7Np9M'></tfoot>
                    <legend id='7Np9M'><style id='7Np9M'><dir id='7Np9M'><q id='7Np9M'></q></dir></style></legend>

                    <small id='7Np9M'></small><noframes id='7Np9M'>

                            <tbody id='7Np9M'></tbody>
                          <i id='7Np9M'><tr id='7Np9M'><dt id='7Np9M'><q id='7Np9M'><span id='7Np9M'><b id='7Np9M'><form id='7Np9M'><ins id='7Np9M'></ins><ul id='7Np9M'></ul><sub id='7Np9M'></sub></form><legend id='7Np9M'></legend><bdo id='7Np9M'><pre id='7Np9M'><center id='7Np9M'></center></pre></bdo></b><th id='7Np9M'></th></span></q></dt></tr></i><div id='7Np9M'><tfoot id='7Np9M'></tfoot><dl id='7Np9M'><fieldset id='7Np9M'></fieldset></dl></div>