Android学习之AppWidget高级效果

所属分类: 软件编程 / Android 阅读数: 535
收藏 0 赞 0 分享

接着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中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多