android使用AppWidget

AppWidget简单理解就是应用程序的手机桌面快捷方式,也就是说点快捷方式和点菜单里的图标效果是一样的,通过这种机制可以打开应用程序。

AppWidget是BroadCast的一个子类,因此这个类和广播的操作是类似的,下面就一步一步来弄下这个吧

1.编写该组件对于桌面图标的快捷方式,一般来说就是一个图标

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/mldn_3g"
     android:id="@+id/img"
 />

</LinearLayout>

下面还要配置一个xml用来控制该组件的属性如高度、宽度、更新时间等

<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
	android:minHeight="80px"
	android:minWidth="300px"
	android:updatePeriodMillis="6000"
	android:initialLayout="@layout/appwidget">
</appwidget-provider>

还有最后一个配置文件,因为该组件也运用到了广播机制所以要在AndroidManifest.xml里增加如下配置

<receiver android:name=".MyAppwidget">
			<intent-filter>
				<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
			</intent-filter>
			<!-- <intent-filter>
				<action android:name="org.lxh.action.MYAPPWIDGET_UPDATE" />
			</intent-filter>  -->
			<!--appwidget是控制组件属性的那个xml  -->
			<meta-data android:name="android.appwidget.provider"
				android:resource="@xml/appwidget" />
		</receiver>

2.编写AppWidgetProvider,只需要写一个类继承AppWidgetProvider就行,我们只需要覆写onUpdate方法即可

package org.lxh.activity;

import android.app.Activity;
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.widget.RemoteViews;

public class MyAppwidget extends AppWidgetProvider {

	
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		for(int i=0;i<appWidgetIds.length;i++){
			//设置activity
			Intent intent=new Intent(context,AppwidgetActivity.class);
			PendingIntent pIntent=PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		    //定义要操作的RemoteViews
			RemoteViews remote=new RemoteViews(context.getPackageName(),R.layout.appwidget);
			remote.setOnClickPendingIntent(R.id.img, pIntent);
			appWidgetManager.updateAppWidget(appWidgetIds[i], remote);
		}
		
	}
    
}

到这里程序就写完了,除了这种方法外还有一种方法可以实现相同的操作代码如下

package org.lxh.demo;

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.widget.RemoteViews;

public class MyAppWidget extends AppWidgetProvider {
	
	@Override
	public void onReceive(Context context, Intent intent) {
		if ("org.lxh.action.MYAPPWIDGET_UPDATE".equals(intent.getAction())) {
			RemoteViews remote = new RemoteViews(context.getPackageName(),
					R.layout.mldn_appwidget);
			remote.setImageViewResource(R.id.img, R.drawable.mldn_man);
			remote.setTextViewText(R.id.but, "www.MLDNJAVA.cn") ;
			AppWidgetManager appWidgetManager = AppWidgetManager
					.getInstance(context);
			ComponentName componentName = new ComponentName(context,
					MyAppWidget.class);
			appWidgetManager.updateAppWidget(componentName, remote) ;
		} else {
			super.onReceive(context, intent) ;	// 如果不写此代码,表示无法调用onUpdate()
		}
	}
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Intent intent = new Intent();
		intent.setAction("org.lxh.action.MYAPPWIDGET_UPDATE") ;
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		RemoteViews remote = new RemoteViews(context.getPackageName(),
				R.layout.mldn_appwidget);
		remote.setOnClickPendingIntent(R.id.but, pendingIntent);
		appWidgetManager.updateAppWidget(appWidgetIds, remote);
	}

}

这种实现对于的配置文件如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.lxh.demo" android:versionCode="1" android:versionName="1.0">
	<uses-sdk android:minSdkVersion="10" />

	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".MyAppWidgetDemo" 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=".MyAppwidget">
		<intent-filter>
			<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
		</intent-filter>
		<intent-filter>
			<action android:name="org.lxh.action.MYAPPWIDGET_UPDATE" />
		</intent-filter>
		<meta-data android:name="android.appwidget.provider"
			android:resource="@xml/appwidget" />
	</receiver>
	
	</application>
</manifest>




你可能感兴趣的:(appwidget)