<legend id='ygnXx'><style id='ygnXx'><dir id='ygnXx'><q id='ygnXx'></q></dir></style></legend>

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

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

        • <bdo id='ygnXx'></bdo><ul id='ygnXx'></ul>

        如何构建一个简单的 Android 小部件

        时间:2023-09-08

        <tfoot id='1ltZo'></tfoot>

        1. <small id='1ltZo'></small><noframes id='1ltZo'>

          <legend id='1ltZo'><style id='1ltZo'><dir id='1ltZo'><q id='1ltZo'></q></dir></style></legend>
            <tbody id='1ltZo'></tbody>

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

                  本文介绍了如何构建一个简单的 Android 小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我有一些构建 android 应用程序的经验.但现在我想为 android 构建一个小部件,它将位于主屏幕上并显示一个按钮,当按下按钮时它会播放声音.我一直在网上查看有关如何设置 android 小部件的教程,但我似乎无法弄清楚.是否有任何关于如何使独立小部件如此简单或我可以开始的地方的好的教程?提前致谢,彼得

                  So I have a bit of experience building android applications. But now i would like to build a widget for android that would sit on the home screen and display a button, and when the button is pressed it plays a sound. I've been looking at tutorials online on how to set up an android widget but i cant seem to figure it out. Are there any good tutorials out there on how to make a standalone widget this simple or somewhere i can start? Thanks in advance, Peter

                  推荐答案

                  首先在res/layout里面新建一个layout文件,在project结构下,按照如下结构定义widgetlayout.xml.

                  first Create a new layout file inside res/layout, under the project structure, that will define the widget layout (widgetlayout.xml) according to the following structure.

                  <TextView android:text="@string/widgettext" 
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="0.8"
                                android:layout_gravity="center_vertical"
                                android:textColor="#000000"></TextView>
                  <TextView android:text="@string/widgetmoodtext"
                                android:id="@+id/widgetMood" android:layout_width="0dp" 
                                android:layout_height="wrap_content" 
                                android:layout_weight="0.3" 
                                android:layout_gravity="center_vertical" 
                                android:textColor="#000000"></TextView>
                  <ImageButton android:id="@+id/widgetBtn" 
                               android:layout_width="0dp" 
                               android:layout_height="wrap_content" 
                               android:layout_weight="0.5"  
                               android:src="@drawable/smile_icon" 
                               android:layout_gravity="center_vertical">
                   </ImageButton>
                  

                  在项目结构下创建res/xml文件夹使用以下参数创建一个 xml 文件 (widgetproviderinfo.xml):

                  Create the res/xml folder under the project structure Create a xml file (widgetproviderinfo.xml) with the following parameters:

                   <appwidget-provider
                             xmlns:android="http://schemas.android.com/apk/res/android" 
                             android:minWidth="220dp" 
                             android:minHeight="72dp"
                             android:updatePeriodMillis="86400000" 
                             android:initialLayout="@layout/widgetlayout">
                         </appwidget-provider>
                  

                  现在您应该创建对用户与笑脸图像按钮 (CurrentMoodService.java) 的交互作出反应的服务.

                  Now you should create the service that reacts to the user interaction with the smiley image button (CurrentMoodService.java).

                  @Override
                  public int onStartCommand(Intent intent, int flags, int startId) {
                      super.onStart(intent, startId);
                              updateMood(intent);
                      stopSelf(startId);
                      return START_STICKY;
                  }
                  
                  private void updateMood(Intent intent) {
                         if (intent != null){
                          String requestedAction = intent.getAction();
                          if (requestedAction != null &&  requestedAction.equals(UPDATEMOOD)){
                              this.currentMood = getRandomMood();
                              int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
                              AppWidgetManager appWidgetMan = AppWidgetManager.getInstance(this);
                              RemoteViews views = new RemoteViews(this.getPackageName(),R.layout.widgetlayout);
                              views.setTextViewText(R.id.widgetMood, currentMood);
                              appWidgetMan.updateAppWidget(widgetId, views);  
                          }
                         }
                     }
                  

                  定义服务后,是时候实现小部件提供者类(CurrentMoodWidgetProvider.java)了.

                  After defining the service, it is time to implement the widget provider class (CurrentMoodWidgetProvider.java).

                      @Override
                  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                          int[] appWidgetIds) {
                      super.onUpdate(context, appWidgetManager, appWidgetIds);
                  
                      for (int i=0; i<appWidgetIds.length; i++) {
                          int appWidgetId = appWidgetIds[i];
                          RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
                          Intent intent = new Intent(context, CurrentMoodService.class);
                          intent.setAction(CurrentMoodService.UPDATEMOOD);
                          intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                          PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
                          views.setOnClickPendingIntent(R.id.widgetBtn, pendingIntent);
                          appWidgetManager.updateAppWidget(appWidgetId, views);
                      }
                  }   
                  

                  最后需要在Manifest(AndroidManifest.xml)中声明Service和AppWidgetProvider.

                  Finally it is necessary to declare the Service and the AppWidgetProvider in the Manifest (AndroidManifest.xml).

                      <service android:name=".CurrentMoodService">
                      </service>
                  <receiver android:name=".CurrentMoodWidgetProvider">
                      <intent-filter>
                          <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                      </intent-filter>
                      <meta-data android:name="android.appwidget.provider"
                                 android:resource="@xml/widgetproviderinfo" />
                  </receiver>
                  

                  如果您想下载整个源代码,请查看下面的网址...

                  and if you want to download the whole source code then have a look at the url below...

                  http://sites.google.com/site/androidsourcecode/src/CurrentMoodWidgetProject.rar?attredirects=0

                  这篇关于如何构建一个简单的 Android 小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:可滚动的应用小部件 下一篇:通过单击小部件开始活动

                  相关文章

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

                    • <bdo id='Zswyr'></bdo><ul id='Zswyr'></ul>

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

                      <tfoot id='Zswyr'></tfoot>