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

首頁 > 系統(tǒng) > Android > 正文

Android學習之AppWidget高級效果

2019-12-12 05:37:55
字體:
來源:轉載
供稿:網友

接著AppWidget基礎學習,今天是一個“進階版”的小例子,用來檢驗一下自己的學習效果。于是就做了一個擲骰子的Widget。

方便大家觀看,先截圖如下:

目錄結構 

這里寫圖片描述 

這里寫圖片描述

需要注意的是在drawable文件夾下有幾張圖片,我是在網上下載的別人的素材。

下面就開始我們的學習之旅吧。

第一步:
是在res/目錄下創(chuàng)建一個名為xml的文件夾(其實名字是隨意的,不必拘泥與這一個名字),然后在里面創(chuàng)建一個appwidget_info.xml文件,其作用就是向系統(tǒng)進行聲明。

<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>
 

第三步:
創(chuàng)建一個支撐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,分支是區(qū)別于自定義ACTION和系統(tǒng)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);    }  }  /*  *用一個循環(huán)的方式是為了應對桌面上添加了好幾個這樣的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 的添加,然后你就可以擁有一款”骰子“咯。

總結:

這個小程序雖說是實現(xiàn)了,但是仍然不是比較復雜。需要對廣播消息等知識有一定的了解。
改進方向:給每次的點擊事件完成時添加動畫效果,以獲得更好地用戶體驗。(需要借助于RemoteViews內的相關的方法)。
代碼中不可避免的會出現(xiàn)一些錯誤和不足之處,希望廣大博友看到后予以指出,希望能和你們一起進步! 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 筠连县| 汕头市| 根河市| 新宁县| 永登县| 武义县| 会理县| 登封市| 南京市| 泸州市| 台江县| 淳安县| 德钦县| 城市| 太谷县| 桐城市| 安丘市| 喜德县| 鹤山市| 沅陵县| 衡阳市| 南澳县| 民县| 乌苏市| 敦煌市| 神农架林区| 贵州省| 饶阳县| 英德市| 全州县| 镇原县| 台湾省| 绥棱县| 枞阳县| 苏尼特左旗| 沂源县| 高阳县| 嘉鱼县| 澜沧| 北流市| 吴忠市|