您的位置:首页 > 移动开发 > Android开发

超简单实用详细的android Widget实例教程

2012-08-10 21:31 507 查看
本文参考了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");
}
@Override
public 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);
}

@Override
public void onDeleted(Context context, int[] appWidgetIds){
Toast.makeText(context, "onDeleted", 1).show();
super.onDeleted(context, appWidgetIds);
}

@Override
public void onEnabled(Context context){
Toast.makeText(context, "onEnabled", 1).show();
super.onEnabled(context);
}

@Override
public 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目录下。】

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: