
接着AppWidget基础学习,今天是一个“进阶版”的小例子,用来检验一下自己的学习效果。于是就做了一个掷骰子的Widget。
方便大家观看,先截图如下:
需要注意的是在drawable文件夹下有几张图片,我是在网上下载的别人的素材。
下面就开始我们的学习之旅吧。
第一步:
是在res/目录下创建一个名为xml的文件夹(其实名字是随意的,不必拘泥与这一个名字),然后在里面创建一个appWidget_info.xml文件,其作用就是向系统进行声明。
<appWidget-provIDer xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:minHeight="72dp" androID:minWIDth="294dp" androID:updatePeriodMillis="86400000" androID:initialLayout="@layout/app_Widget_layout" ></appWidget-provIDer>
第二步:
对布局界面进行设置,我的设置如下app_Widget_layout.xml文件:
<?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:orIEntation="vertical" > <ImageVIEw androID:ID="@+ID/imagevIEw_Widget" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:src="@drawable/ic_launcher" /> <button androID:ID="@+ID/button_Widget" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:text="摇一摇" androID:textcolor="#6663c6" /></linearLayout>
第三步:
创建一个支撑Widget的类,用来完成接下来的逻辑的 *** 作,如下面的WidgetProvIDer.java.
package com.summer.myWidget;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.util.Log;import androID.Widget.RemoteVIEws;public class WidgetProvIDer extends appwidgetprovider{ private static final String MY_UPDATE_ACTION="com.summer.APP_Widget_ACTION"; /* *随机的获得一张图片的int值,为接下来的变换图片打基础 */ private static int getRandomPicture(){ int[] pictureArray=new int[]{R.drawable.dice_1,R.drawable.dice_2,R.drawable.dice_3,R.drawable.dice_4,R.drawable.dice_5,R.drawable.dice_6}; int RandomNumber=(int) ((Math.random()*100)%6); return pictureArray[RandomNumber]; } /* *用于接收action,分支是区别于自定义ACTION和系统action的有效方式 */ @OverrIDe public voID onReceive(Context context,Intent intent) { // Todo auto-generated method stub String RESPONSEACTION=intent.getAction(); Log.i("Summer","------------->>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+RESPONSEACTION); if(MY_UPDATE_ACTION.equals(RESPONSEACTION)){ RemoteVIEws remoteVIEws=new RemoteVIEws(context.getPackagename(),R.layout.app_Widget_layout); remoteVIEws.setimageVIEwResource(R.ID.imagevIEw_Widget,getRandomPicture()); AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context); Componentname componentname=new Componentname(context,WidgetProvIDer.class); appWidgetManager.updateAppWidget(componentname,remoteVIEws); }else{ super.onReceive(context,intent); } } /* *用一个循环的方式是为了应对桌面上添加了好几个这样的Widget的情形 */ @OverrIDe public voID onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIDs) { // Todo auto-generated method stub for(int i=0;i<appWidgetIDs.length;i++){ Intent intent=new Intent(); intent.setAction(MY_UPDATE_ACTION); PendingIntent pendingIntent=PendingIntent.getbroadcast(context,-1,intent,0); RemoteVIEws remoteVIEws=new RemoteVIEws(context.getPackagename(),R.layout.app_Widget_layout); remoteVIEws.setonClickPendingIntent(R.ID.button_Widget,pendingIntent); appWidgetManager.updateAppWidget(appWidgetIDs[i],remoteVIEws); } super.onUpdate(context,appWidgetManager,appWidgetIDs); } @OverrIDe public voID onDeleted(Context context,int[] appWidgetIDs) { // Todo auto-generated method stub super.onDeleted(context,appWidgetIDs); System.out.println("my app Widget ----------------------------->>>>>>>onDeleted"); } @OverrIDe public voID onEnabled(Context context) { // Todo auto-generated method stub System.out.println("my app Widget ----------------------------->>>>>>>onEnabled"); super.onEnabled(context); } @OverrIDe public voID onDisabled(Context context) { // Todo auto-generated method stub System.out.println("my app Widget ----------------------------->>>>>>>onDisabled"); super.onDisabled(context); }} 第四步:
在清单文件Manifest.xml文件中进行相关项的声明。详如下:
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.summer.myWidget" androID:versionCode="1" androID:versionname="1.0" > <uses-sdk androID:minSdkVersion="8" androID:targetSdkVersion="18" /> <application androID:allowBackup="true" androID:icon="@drawable/ic_launcher" androID:label="@string/app_name" androID:theme="@style/Apptheme" > <activity androID:name="com.summer.myWidget.MainActivity" androID:label="@string/app_name" > <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver androID:name="com.summer.myWidget.WidgetProvIDer"> <intent-filter > <action androID:name="androID.appWidget.action.APPWidget_UPDATE"/> </intent-filter> <intent-filter > <action androID:name="com.summer.APP_Widget_ACTION"></action> </intent-filter> <Meta-data androID:name="androID.appWidget.provIDer" androID:resource="@xml/appWidget_info"> </Meta-data> </receiver> </application></manifest>
第五步:
大功告成,运行一下代码,然后手工的进行app_Widget 的添加,然后你就可以拥有一款”骰子“咯。
总结:
这个小程序虽说是实现了,但是仍然不是比较复杂。需要对广播消息等知识有一定的了解。
改进方向:给每次的点击事件完成时添加动画效果,以获得更好地用户体验。(需要借助于RemoteVIEws内的相关的方法)。
代码中不可避免的会出现一些错误和不足之处,希望广大博友看到后予以指出,希望能和你们一起进步!
以上是内存溢出为你收集整理的Android学习之AppWidget高级效果全部内容,希望文章能够帮你解决Android学习之AppWidget高级效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)