国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android桌面組件App Widget用法入門教程

2020-04-11 11:24:52
字體:
來源:轉載
供稿:網友

本文實例講述了Android桌面組件App Widget用法。分享給大家供大家參考。具體如下:

Android開發應用除了程序應用,還有App Widget應用。好多人會開發程序應用而不會開發App Widget應用。本帖子就是幫助大家學習如何開發App Widget應用的。

先簡單說說App Widget的原理。App Widget是在桌面上的一塊顯示信息的東西,通過單擊App Widget跳轉到程序入口類。而系統自帶的程序,典型的App Widget是music,這個Android內置的音樂播放小程序。這個是典型的App Widget+app應用。就是一個程序既可以通過App Widget啟動,也可以通過App啟動。App Widget就是一個AppWidgetProvider+一個UI界面顯示(預先綁定了好多Intent),界面上的信息可以通過程序控制而改變,單擊Widget上的控件只能激發發送一個Intent,或發出一個Service的啟動通知。而AppWidgetProvider可以攔截這個Intent,而進行相應的處理(比如顯示新的信息)。

以下模擬一下App Widget的應用

通過兩種方式啟動應用程序

1、App Widget啟動

長按空白的桌面主屏幕會彈出“添加到主屏幕”,然后選擇“窗口小部件”選項進入“選擇窗口小部件”,最后選擇想要的小部件就會添加到桌面主屏幕,當點擊剛才添加的桌面控件就會進入到程序主入口。

2、App啟動:跟普通的Activity一樣

以下為實現代碼

main.xml布局文件,程序入口類的界面

my_layout.xml布局文件:帶一個圖片的按鈕

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:layout_width="fill_parent"  android:layout_height="fill_parent">  <TextView android:layout_width="fill_parent"    android:layout_height="wrap_content"     android:text="程序入口" /></LinearLayout>

類MainActivity程序入口類:

package com.ljq.activity;import android.app.Activity;import android.os.Bundle;/** * 主程序入口類 *  * @author jiqinlin * */public class MainActivity extends Activity{  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);  }}

下面的代碼才是開發AppWidget用到的代碼:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"   android:layout_width="fill_parent"  android:layout_height="fill_parent">  <!-- <ImageView    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/imageView"     android:gravity="center"    android:layout_width="fill_parent"    android:layout_height="wrap_content" /> -->  <Button android:id="@+id/btn"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:gravity="center"    android:background="@drawable/png1"/></LinearLayout>

my_appwidget.xml布局文件:

<?xml version="1.0" encoding="utf-8"?><!-- AppWidgetProvderInfo: 描述AppWidget的大小、更新頻率和初始界面等信息,以XML文件形式存在于應用的res/xml/目錄下。注意:SDK1.5之后此android:updatePeriodMillis就失效了,要自己創建service更新 --><appwidget-provider  xmlns:android="http://schemas.android.com/apk/res/android"  android:minWidth="75dip"  android:minHeight="45dip"  android:updatePeriodMillis="1000"  android:initialLayout="@layout/my_layout"/>

TestActivity類:

package com.ljq.activity;import android.app.PendingIntent;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.widget.RemoteViews;/** * 為手機添加桌面控件,當點擊桌面控件時則進入主程序 *  * AppWidgetProvider:繼承自BroadcastRecevier,在AppWidget應用update、enable、disable和delete時接收通知。 * 其中,onUpdate、onReceive是最常用到的方法,它們接收更新通知 *  * @author jiqinlin * */public class TestActivity extends AppWidgetProvider {  /**   * 用來間隔的更新App Widget,間隔時間用AppWidgetProviderInfo里的updatePeriodMillis屬性定義(單位為毫秒)。   * 注意:SDK1.5之后此android:updatePeriodMillis就失效了,要自己創建service更新。   * 這個方法也會在用戶添加App Widget時被調用,因此它應該執行基礎的設置,比如為視圖定義事件處理器并啟動一個臨時的服務Service,如果需要的話。   * 但是,如果你已經聲明了一個配置活動,這個方法在用戶添加App Widget時將不會被調用,   * 而只在后續更新時被調用。配置活動應該在配置完成時負責執行第一次更新。   */  @Override  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {    System.out.println("onUpdate");    //點擊桌面組件時進入主程序入口    Intent intent=new Intent(context, MainActivity.class);    PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0);    //RemoteViews類描述了一個View對象能夠顯示在其他進程中,可以融合layout資源文件實現布局。    //雖然該類在android.widget.RemoteViews而不是appWidget下面,但在Android Widgets開發中會經常用到它,    //主要是可以跨進程調用(appWidget由一個服務宿主來統一運行的)。    RemoteViews myRemoteViews = new RemoteViews(context.getPackageName(), R.layout.my_layout);    //myRemoteViews.setImageViewResource(R.id.imageView, R.drawable.png1);//設置布局控件的屬性(要特別注意)    myRemoteViews.setOnClickPendingIntent(R.id.btn, pendingIntent);    ComponentName myComponentName = new ComponentName(context, TestActivity.class);    //負責管理AppWidget,向AppwidgetProvider發送通知。提供了更新AppWidget狀態,獲取已經安裝的Appwidget提供信息和其他的相關狀態    AppWidgetManager myAppWidgetManager = AppWidgetManager.getInstance(context);    myAppWidgetManager.updateAppWidget(myComponentName, myRemoteViews);  }  /**   * 當App Widget從宿主中刪除時被調用。   */  @Override  public void onDeleted(Context context, int[] appWidgetIds) {    System.out.println("onDeleted");    super.onDeleted(context, appWidgetIds);  }  /**   * 當一個App Widget實例第一次創建時被調用。   * 比如,如果用戶添加兩個App Widget實例,只在第一次被調用。   * 如果你需要打開一個新的數據庫或者執行其他對于所有的App Widget實例只需要發生一次的設置,   * 那么這里是完成這個工作的好地方。   */  @Override  public void onEnabled(Context context) {    System.out.println("onEnabled");    super.onEnabled(context);  }  /**   * 當你的App Widget的最后一個實例被從宿主中刪除時被調用。你應該在onEnabled(Context)中做一些清理工作,比如刪除一個臨時的數據庫   */  @Override  public void onDisabled(Context context) {    System.out.println("onDisabled");    super.onDisabled(context);  }  /**   * 接收到每個廣播時都會被調用,而且在上面的回調函數之前。   * 你通常不需要實現這個方法,因為缺省的AppWidgetProvider實現過濾所有App Widget廣播并恰當的調用上述方法。   * 注意: 在Android 1.5中,有一個已知問題,onDeleted()方法在調用時不被調用。   * 為了規避這個問題,你可以像Group post中描述的那樣實現onReceive()來接收這個onDeleted()回調。   */  @Override  public void onReceive(Context context, Intent intent) {    System.out.println("onReceive");    super.onReceive(context, intent);  }}

清單文件:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="com.ljq.activity" android:versionCode="1"  android:versionName="1.0">  <application android:icon="@drawable/icon" android:label="@string/app_name">    <activity android:name=".MainActivity" android:label="主程序">      <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />      </intent-filter>    </activity>    <!-- TestActivity類為一個廣播接收器,因為TestActivity繼承自AppWidgetProvider -->    <receiver android:name=".TestActivity"  android:label="添加桌面控件">      <intent-filter>        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />      </intent-filter>      <meta-data android:name="android.appwidget.provider"        android:resource="@xml/my_appwidget"/>    </receiver>  </application>  <uses-sdk android:minSdkVersion="7" /></manifest>

希望本文所述對大家的Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遵化市| 宁乡县| 临澧县| 峨眉山市| 沙河市| 漠河县| 扬中市| 和平区| 汪清县| 确山县| 成安县| 嘉义市| 宁安市| 吴堡县| 甘孜县| 新邵县| 武川县| 红原县| 普安县| 晋江市| 台山市| 宜城市| 化德县| 嘉祥县| 景宁| 理塘县| 凉城县| 卫辉市| 普安县| 桂平市| 比如县| 凤翔县| 三都| 文水县| 蓝田县| 沙雅县| 伊金霍洛旗| 会东县| 临城县| 栾城县| 武夷山市|