android widget 案頭外掛程式的簡單案例講解

來源:互聯網
上載者:User

  想要做個可以在案頭上顯示的外掛程式需要3個步驟:  1. 繼承AppWidgetProvider  2. 編寫widget的介面xml, 編寫mywidget屬性xml  3. AndroidManifest.xml  註冊外掛程式 先這個案例是一個會自增的數字, 非常的簡單。 1. 繼承AppWidgetProvider[java]  public class MyWidgetProvider extends AppWidgetProvider  {      private static Timer myTimer;      private static int index = 0;            private final String broadCastString = "knowheart.wd.appWidgetUpdate";             @Override      public void onDeleted(Context context, int[] appWidgetIds)      {          // TODO Auto-generated method stub          super.onDeleted(context, appWidgetIds);                }                    @Override      public void onEnabled(Context context)      {          // TODO Auto-generated method stub          super.onEnabled(context);                }            @Override      public void onUpdate(Context context, AppWidgetManager appWidgetManager,              int[] appWidgetIds)      {          // TODO Auto-generated method stub          super.onUpdate(context, appWidgetManager, appWidgetIds);      }                        @Override      public void onReceive(Context context, Intent intent)      {          super.onReceive(context, intent);      }    }  首先我們添加一個類, 用於繼承AppWidgetProvider, 它就是用來控制我們的外掛程式更新刪除的一些操作這裡會用到onEnabled、onUpdate、onReceive三個方法   2. 編寫widget的介面xml, 編寫mywidget屬性xmllayout/widget_layout.xml[html] <?xml version="1.0" encoding="utf-8"?>    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"      android:background="#000000"      android:layout_gravity="center_horizontal"      android:layout_width="300dp"      android:layout_height="wrap_content">        <TextView          android:id="@+id/update"          android:text="0"           android:layout_width="fill_parent"          android:layout_height="wrap_content"           android:gravity="center"       />        </LinearLayout>   建立一個檔案夾命名xml, 再添加一個xml檔案xml/mywidget.xml[html]  <?xml version="1.0" encoding="UTF-8"?>  <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"        android:minWidth="300dip" android:minHeight="72dp"        android:updatePeriodMillis="86400000"       android:initialLayout="@layout/widget_layout"        >    </appwidget-provider>     這裡設定外掛程式的長寬、重新整理時間, 0為手動重新整理、以及外掛程式的介面xml   3. AndroidManifest.xml  註冊外掛程式[html] <!-- 註冊該外掛程式 -->         <receiver android:name="MyWidgetProvider" >             <intent-filter >                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />                 <action android:name="com.wd.appWidgetUpdate" >                 </action>             </intent-filter>               <meta-data                 android:name="android.appwidget.provider"                 android:resource="@xml/mywidget" />         </receiver>    <action android:name="com.wd.appWidgetUpdate" >  這一段是向系統註冊一個廣播, 我們會在邏輯處理時將該廣播發送給AppWidgetProvider的更新方法, 以實現手動重新整理。 那麼完成上面三部基本上就可以顯示一個外掛程式在案頭上了。 如果我們想讓外掛程式顯示的數字每秒加一的話, 我們需要做幕後處理[java] public class MyWidgetProvider extends AppWidgetProvider  {      private static Timer myTimer;      private static int index = 0;            //定義我們要發送的事件      private final String broadCastString = "com.wd.appWidgetUpdate";             @Override      public void onDeleted(Context context, int[] appWidgetIds)      {          // TODO Auto-generated method stub          super.onDeleted(context, appWidgetIds);                    System.out.println("onDeleted");      }                    @Override      public void onEnabled(Context context)      {          System.out.println("onEnabled");          // TODO Auto-generated method stub          super.onEnabled(context);                    //在外掛程式被建立的時候這裡會被調用, 所以我們在這裡開啟一個timer 每秒執行一次          MyTask mMyTask = new MyTask(context);          myTimer = new Timer();          myTimer.schedule(mMyTask, 1000, 1000);          System.out.println("onEnabled2");      }            @Override      public void onUpdate(Context context, AppWidgetManager appWidgetManager,              int[] appWidgetIds)      {          System.out.println("onUpdate");          // TODO Auto-generated method stub          super.onUpdate(context, appWidgetManager, appWidgetIds);      }                        @Override      public void onReceive(Context context, Intent intent)      {          //當判斷到是該事件發過來時, 我們就擷取外掛程式的介面, 然後將index自加後傳入到textview中          System.out.println("onReceive");          if(intent.getAction().equals(broadCastString))          {              index++;                            RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);              rv.setTextViewText(R.id.update, Integer.toString(index));                            //將該介面顯示到外掛程式中              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);              ComponentName componentName = new ComponentName(context,MyWidgetProvider.class);              appWidgetManager.updateAppWidget(componentName, rv);          }             // TODO Auto-generated method stub          super.onReceive(context, intent);      }                class MyTask extends TimerTask      {            private Context mcontext = null;          private Intent intent = null;                    public MyTask(Context context) {                            //建立一個要發送的Intent              mcontext = context;              intent = new Intent();              intent.setAction(broadCastString);          }          @Override          public void run()          {              System.out.println("2");              //發送廣播(由onReceive來接收)              mcontext.sendBroadcast(intent);          }                }  }   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.