在上一篇博客中,主要从文档的角度介绍如何实现一个Android桌面小部件,本篇博客将用实例向你介绍如何实现一个Android小部件!!
先看一下效果图:

从效果图上可以看出,桌面小部件已经被实现了,只不过里面没有什么内容,现在我们看一下具体的实现代码:

小部件的布局文件:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/text_clock_container"><TextClock    android:textAlignment="center"    android:textSize="48sp"    android:id="@+id/text_clock"    android:layout_width="match_parent"    android:layout_height="wrap_content" />FrameLayout>

小部件的AppWidgetProviderInfo文件:

<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:initialLayout="@layout/app_widget_layout"    android:minHeight="80dp"    android:minWidth="80dp"    android:minResizeHeight="80dp"    android:minResizeWidth="80dp"    android:resizeMode="vertical"    android:widgetCategory="home_screen"    android:previewImage="@drawable/shape_xxx"    android:updatePeriodMillis="10000"    android:configure="com.example.appwidgetdemo.ConfigureActivity">appwidget-provider>

配置Activity文件:

package com.example.appwidgetdemo;import android.app.Activity;import android.os.Bundle;import android.support.annotation.Nullable;import android.util.Log;/** * Created by Administrator on 2017/4/21 0021. */public class ConfigureActivity extends Activity {    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Log.i("zyq","ConfigureActivity:onCreate");        setResult(RESULT_OK);        finish();    }}

AppWidgetProvider的实现文件:

package com.example.appwidgetdemo;import android.app.PendingIntent;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.widget.RemoteViews;/** * Created by Administrator on 2017/4/21 0021. */public class AppWidgetReceiver extends AppWidgetProvider {    @Override    public void onReceive(Context context, Intent intent) {        super.onReceive(context, intent);        Log.i("zyq","AppWidgetReceiver:onReceive");    }    @Override    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {        super.onUpdate(context, appWidgetManager, appWidgetIds);        Log.i("zyq","AppWidgetReceiver:onUpdate");        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.app_widget_layout);        PendingIntent i = PendingIntent.getActivity(context,0,new Intent(context,MainActivity.class),0);        remoteViews.setOnClickPendingIntent(R.id.text_clock_container,i);        for (int i1 = 0;i1@Override    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);        Log.i("zyq","AppWidgetReceiver:onAppWidgetOptionsChanged");    }    @Override    public void onDeleted(Context context, int[] appWidgetIds) {        super.onDeleted(context, appWidgetIds);        Log.i("zyq","AppWidgetReceiver:onDeleted");    }    @Override    public void onEnabled(Context context) {        super.onEnabled(context);        Log.i("zyq","AppWidgetReceiver:onEnabled");    }    @Override    public void onDisabled(Context context) {        super.onDisabled(context);        Log.i("zyq","AppWidgetReceiver:onDisabled");    }    @Override    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {        super.onRestored(context, oldWidgetIds, newWidgetIds);        Log.i("zyq","AppWidgetReceiver:onRestored");    }}

以及将所有文件注册到系统内部的清单文件:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.appwidgetdemo">    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true" android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            intent-filter>        activity>        <activity android:name=".ConfigureActivity">            <intent-filter>                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>            intent-filter>        activity>        <receiver android:name=".AppWidgetReceiver">            <intent-filter>                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />                <action android:name="android.intent.action.TIME_SET"/>            intent-filter>            <meta-data android:name="android.appwidget.provider"                android:resource="@xml/app_widget_provider_info"/>        receiver>    application>manifest>

关于小部件的实现代码大概就这么多,这里有几点需要说明一下,第一个是xml中的一个属性previewImage,这个属性必须是一个drawable,是一个在xml中定义的对象,不能是一个color值,虽然AS自动提示包含了Color值,但是引用过后,在实际创建小部件时会报空指针异常,因为previewImage指向的不是一个drawable。
这个就是perviewImage:

如何实现Android桌面小部件(二)_第1张图片

在AppWidgetProvider实现中的onUpdate方法中传入了一个appWidgetIds数组,之所以会是数组,因同一个小部件用户可能会在桌面创建多个,就像效果图里展示的那样,那么在更新小部件时需要根据小部件的ID来跟新,所以需要对同一种小部件进行更新,也就是需要遍历数组!!

点击事件就是通过使用pendingIntent来实现的,这个没有什么好说了!!!

这是我的微信公众号,如果可以的话,希望您可以帮忙关注一下,这将是对我最大的鼓励了,谢谢!!

如何实现Android桌面小部件(二)_第2张图片

代码地址:(里面的代码比较杂乱,需要自己根据需要获取)
https://github.com/zhuyuqiang2017/Other

更多相关文章

  1. android 签名文件
  2. Android Eclipse JNI 调用 .so文件加载问题
  3. Android之Layout资源文件
  4. Android实战技巧:使用原始资源文件

随机推荐

  1. Android NDK下载:Download Android 1.5 ND
  2. Android(安卓)悬浮窗 (附圆形菜单悬浮窗)
  3. Android 进度条
  4. How Android Draws Views
  5. android图片放大 缩小 旋转
  6. Layout1.9
  7. Android usb 驱动
  8. Android 查看本机外网IP
  9. AndroidMenifest.xml(Android清单文件)内
  10. Android调用摄像头闪退