android之App Widget開發(fā)實(shí)例代碼解析
Android Widget開發(fā)案例實(shí)現(xiàn)是本文要介紹的內(nèi)容,主要是來了解并學(xué)習(xí)Android Widget開發(fā)應(yīng)用,今天我們要寫一下Android Widget的開發(fā),由于快點(diǎn)凌晨,我就不說的太具體了,同志們就模仿吧!
首先繼續(xù)了解下App Widget框架的主要的類:
AppWidgetProvider:繼承自BroadcastReceiver,在App Widget應(yīng)用update,enable,disable和deleted時(shí)接受通知。其中onUpdate,onReceive是最常用到的方法。
AppWidgetProviderInfo:描述AppWidget的大小,更新頻率和初始界面等信息,以xml文件的形式存在于應(yīng)用中的res/xml目錄下。
AppWidgetManager:負(fù)責(zé)管理AppWidget,向AppWidgetProvider發(fā)送通知。
RemoteViews:一個(gè)可以在其他應(yīng)用進(jìn)程中運(yùn)行的類,是構(gòu)造AppWidget的核心。
下面開始代碼的編寫,首先在res/xml下建立myappwidetprovider.xml
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="100dp" android:minHeight="72dp" android:updatePeriodMillis="86400000" android:initialLayout="@layout/myappwidget" > </appwidget-provider>
上面分別是 定義widget的寬度,高度,更新周期,以及l(fā)ayout的widget布局。
下面是我們的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/widget_bg1"
android:gravity="center"
android:id="@+id/layout"
android:orientation="vertical" >
<TextView
android:id="@+id/txtMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_margin="2dp"
android:text="" />
<TextView
android:id="@+id/txtDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#990033"
android:textSize="25dp"
android:text="" />
<TextView
android:id="@+id/txtWeekDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:textColor="#000000"
android:text="" />
</LinearLayout>
對(duì)應(yīng)布局widget要求比較高,大家自行設(shè)計(jì),更加美觀的界面。
接下來是我們的核心代碼ExampleAppWidgetProvider類了:
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.text.format.Time;
import android.widget.RemoteViews;
import android.widget.Toast;
public class ExampleAppWidgetProvider extends AppWidgetProvider{
private String[] months={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
private String[] days={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
RemoteViews remoteViews=new RemoteViews(context.getPackageName(), R.layout.myappwidget);
Time time=new Time();
time.setToNow();
String month=time.year+" "+months[time.month];
remoteViews.setTextViewText(R.id.txtDay, new Integer(time.monthDay).toString());
remoteViews.setTextViewText(R.id.txtMonth, month);
remoteViews.setTextViewText(R.id.txtWeekDay, days[time.weekDay]);
Intent intent=new Intent("cn.com.karl.widget.click");
PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
if(intent.getAction().equals("cn.com.karl.widget.click")){
Toast.makeText(context, "點(diǎn)擊了widget日歷", 1).show();
}
}
}
上面代碼忘記做注釋了,在這類分別解釋下,使用remoteViews類分別加載上來布局文件的相應(yīng)ID設(shè)置好值,然PendingIntent 這就沒什么好解釋的了。
最后在manifest中加入:
<receiver android:name="ExampleAppWidgetProvider" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="cn.com.karl.widget.click" >
</action>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/myappwidetprovider" />
</receiver>
這樣就完成了,運(yùn)行項(xiàng)目看一下載手機(jī)上運(yùn)行的效果吧:

上面就是我們自己定義的AppWidget顯示效果,點(diǎn)擊它:
這里為了表示點(diǎn)擊了它,使用了Toast打印信息,當(dāng)然我們也可以點(diǎn)擊它之后啟動(dòng)相應(yīng)的Activity。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android的Glide庫加載圖片的用法及其與Picasso的對(duì)比
這篇文章主要介紹了Android的Glide庫加載圖片的用法及其與Picasso的對(duì)比,Glide的加載gif圖片的功能和性能受到了很多開發(fā)者的青睞,需要的朋友可以參考下2016-04-04
android開發(fā)教程之使用listview顯示qq聯(lián)系人列表
這篇文章主要介紹了android使用listview顯示qq聯(lián)系人列表的示例,需要的朋友可以參考下2014-02-02
Android動(dòng)態(tài)模糊效果的快速實(shí)現(xiàn)方法
這篇文章主要介紹了Android動(dòng)態(tài)模糊效果的快速實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android實(shí)現(xiàn)點(diǎn)擊縮略圖放大效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊縮略圖放大效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android7.0開發(fā)實(shí)現(xiàn)Launcher3去掉應(yīng)用抽屜的方法詳解
這篇文章主要介紹了Android7.0開發(fā)實(shí)現(xiàn)Launcher3去掉應(yīng)用抽屜的方法,結(jié)合實(shí)例形式分析了Android7.0 Launcher3調(diào)整界面布局的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-11-11
Android 通過SQLite數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)管理
SQLiteOpenHelper 是Android 提供的一個(gè)抽象工具類,負(fù)責(zé)管理數(shù)據(jù)庫的創(chuàng)建、升級(jí)工作。本文主要介紹了如何使用SQLite數(shù)據(jù)庫實(shí)現(xiàn)對(duì)數(shù)據(jù)進(jìn)行存儲(chǔ)管理,感興趣的可以了解一下2021-11-11
解決WebView通過URL加載H5界面出現(xiàn)空白的問題
這篇文章主要介紹了解決WebView通過URL加載H5界面出現(xiàn)空白的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
android實(shí)現(xiàn)圓環(huán)倒計(jì)時(shí)控件
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)圓環(huán)倒計(jì)時(shí)控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Android XRecyclerView實(shí)現(xiàn)多條目加載
這篇文章主要為大家詳細(xì)介紹了Android XRecyclerView實(shí)現(xiàn)多條目加載效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

