本文参考了Android_Tutor的【Android高手进阶教程(八)之----Android Widget开发案例(世界杯倒计时!)】,十分感谢Android_Tutor。本文在Android_Tutor文章的基础上,作了一些简化和改进,主要功能是实现把小插件拖到桌面后,按一下小插件,出现一个Toast。其实按一下小插件,可以做很多事,这里为了简化,就简单地出现一个Toast。效果如下:

下面开始一步一步做:

一、新建一个Android工程命名为:WidgetDemo.

二、建立Widget内容提供者文件,我们在res下建立xml文件夹,并且新建一个widget_provider.xml代码入下:

<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"android:minWidth="50dip"android:minHeight="50dip"android:updatePeriodMillis="10000"android:initialLayout="@layout/main"/> 
三、 修改main.xml布局,代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="0.75"        android:text="点击我!" /></LinearLayout>
四、 修改WidgetDemo.java代码如下:
package com.android.WidgetDemo;import android.app.PendingIntent;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.Context;import android.content.Intent;import android.util.Log;import android.widget.RemoteViews;import android.widget.Toast;public class WidgetDemo extends AppWidgetProvider {    /** Called when the activity is first created. */public static String REFRESH_ACTION = "com.example.android.weatherlistwidget.REFRESH";public WidgetDemo() {Log.e("debug", "WidgetDemo");}@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {final int N = appWidgetIds.length;        // Perform this loop procedure for each App Widget that belongs to this provider        for (int i=0; i<N; i++) {            int appWidgetId = appWidgetIds[i];            Intent intent = new Intent(context, WidgetDemo.class);            intent.setAction(WidgetDemo.REFRESH_ACTION);            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,            intent, PendingIntent.FLAG_UPDATE_CURRENT);            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);            views.setOnClickPendingIntent(R.id.button1, pendingIntent);            views.setTextViewText(R.id.button1, "plessMe!");            // Tell the AppWidgetManager to perform an update on the current App Widget            appWidgetManager.updateAppWidget(appWidgetId, views);        }        super.onUpdate(context, appWidgetManager, appWidgetIds);}@Overridepublic void onDeleted(Context context, int[] appWidgetIds){Toast.makeText(context, "onDeleted", 1).show();super.onDeleted(context, appWidgetIds);}@Overridepublic void onEnabled(Context context){Toast.makeText(context, "onEnabled", 1).show();super.onEnabled(context);}@Overridepublic void  onDisabled(Context context){Toast.makeText(context, "onDisabled", 1).show();super.onDisabled(context);} @Override    public void onReceive(Context context, Intent intent) {        final String action = intent.getAction();        if (action.equals(REFRESH_ACTION)) {        Toast.makeText(context, "點擊我了!!!!", 1).show();        }        super.onReceive(context, intent);    }  }
代码中各个重写的函数都加入了Toast,方便了解程序运行的整个过程。

五、修改配置文件AndroidManifest.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.android.WidgetDemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <receiver            android:name="WidgetDemo"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />            </intent-filter>            <meta-data                android:name="android.appwidget.provider"                android:resource="@xml/widget_provider" />        </receiver>    </application></manifest>

到此结束!

PS:其实把Button换成ImageView效果更佳。main文件修改如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/ImageViewPress"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:adjustViewBounds="true"        android:cropToPadding="false"        android:paddingLeft="10.0dip"        android:paddingRight="10.0dip"        android:paddingTop="3.0dip"        android:scaleType="fitCenter"        android:src="@drawable/button" /></LinearLayout>
WidgetDemo里对应的ID自行修改。

此外,下面这句得去了。

views.setTextViewText(R.id.buttonPress, "PressMe!");

效果如下:【图片自行准备,放在相应drawable目录下。】

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 玩一下android的反编译和代码混淆
  3. Android(安卓)学习之《第一行代码》第二版 笔记(十八)调用摄像头拍
  4. Android06之RecyclerView详解
  5. 直播一对一源码在Android音频开发中如何实现对讲机实时语音对话
  6. Android(安卓)Studio或Idea中必备插件、自定义注释和Javadoc
  7. Android获取其他包的Context实例,然后调用它的方法,反射!!!
  8. Servlet中POST服务延时返回数据
  9. RN 与 Android(安卓)代码交互

随机推荐

  1. android studio不会导入及出现各种问题怎
  2. HwBinder驱动篇-Android10.0 HwBinder通
  3. PHP 即将来到 Google Android?
  4. android:layout_gravity 和 android:grav
  5. Android将允许纯C/C++开发应用
  6. Android的一些基本概念
  7. Android主流三方库源码分析(五、深入理解R
  8. Android常用类库包介绍
  9. Android 内核剖析
  10. Android之Shape和state_enabled使用总结