新浪微博 登陆  注册   设为首页 加入收藏

学PHP >> Android开发实例 >> android 窗口小部件 的实现

android 窗口小部件 的实现

查看次数16832 发表时间2012-11-24 00:31:59

“窗口小部件”可以是一个单独的apk,当然也可以内嵌在其他的apk当中。

此处主要是为了说明“窗口小部件”的实现方法,所有单独做成一个apk 。新建一个android项目(没有activity)。

下面是代码:


首先是Contants类:

package com.android.flashlight;

import android.util.Log;

public class Contants {
	
	private static final String TAG = "flash";
	public static final boolean DEBUG = true;
	
	public static final String ACTION_FLASHLIGHT_WIGET_CLICK = "com.lenovo.flashlight.action.WIGET_CLICK";
	
	public static void LogE(String msg){
		if(DEBUG){
			Log.e(TAG, msg);
		}
	}
	
	public static void LogD(String msg){
		if(DEBUG){
			Log.d(TAG, msg);
		}
	}
	
	public static void LogI(String msg){
		if(DEBUG){
			Log.i(TAG, msg);
		}
	}
}

这个主要是习惯和规范问题,没什么要说的。


在res/layout/下创建文件flashlight_widget_layout.xml布局文件,是“窗口小部件”在桌面上显示的布局文件:

<?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:id="@+id/imageview_widget"
  	    android:layout_width="fill_parent"    
    	android:layout_height="wrap_content" 
    	android:src="@drawable/flashlight_icon" />
</LinearLayout>

FlashSwitchWidgetProvider类是主要的类:

package com.android.flashlight;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class FlashSwitchWidgetProvider extends AppWidgetProvider {

	
	private static RemoteViews mRemoteViews;
	
	/**
	 * 每删除一次窗口小部件就调用一次
	 */
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
		Contants.LogI("onDeleted");
	}

	/**
	 * 当最后一个该窗口小部件删除时调用该方法,注意是最后一个
	 */
	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
		Contants.LogI("onDisabled");
	}

	/**
	 * 当该窗口小部件第一次添加到桌面时调用该方法,可添加多次但只第一次调用
	 */
	@Override 
	public void onEnabled(Context context) {
		super.onEnabled(context);
		Contants.LogI("onEnabled");
	}

	/**
	 * 接收窗口小部件点击时发送的广播
	 */
	@Override 
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		Contants.LogI("onReceive : action = " + intent.getAction());
		
		if(Contants.ACTION_FLASHLIGHT_WIGET_CLICK.equals(intent.getAction())){
			//这里是点击“窗口小部件”要执行的内容
		}
	}

	/**
	 * 每次窗口小部件被点击更新都调用一次该方法
	 */
	@Override 
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		Contants.LogI("onUpdate");

		final int counter = appWidgetIds.length;
		Contants.LogD("counter = " + counter);
		for (int i = 0; i < counter; i++) {
			int appWidgetId = appWidgetIds[i];
			onWidgetUpdate(context, appWidgetManager, appWidgetId);
		}
		
	}
	
	/**
	 * 窗口小部件更新
	 * @param context
	 * @param appWidgeManger
	 * @param appWidgetId
	 */
	private void onWidgetUpdate(Context context,
			AppWidgetManager appWidgeManger, int appWidgetId) {
		
		Contants.LogD("appWidgetId = " + appWidgetId);
		mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.flashlight_widget_layout);
		
		//"窗口小部件"点击事件发送的Intent广播
		Intent intentClick = new Intent();
		intentClick.setAction(Contants.ACTION_FLASHLIGHT_WIGET_CLICK);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);
		mRemoteViews.setOnClickPendingIntent(R.id.imageview_widget, pendingIntent);
		appWidgeManger.updateAppWidget(appWidgetId, mRemoteViews);
	}

}


说明见注释内容。

之后是清单文件AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.flashlight"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />
    <application
        android:label="@string/app_name" >
        <receiver android:name=".FlashSwitchWidgetProvider"
            android:icon="@drawable/flashlight_icon">
			<meta-data android:name="android.appwidget.provider"
				android:resource="@xml/widget_info"></meta-data>
			<intent-filter>
			    <action android:name="com.lenovo.flashlight.action.WIGET_CLICK"/>
				<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
			</intent-filter>
		</receiver>
        
    </application>

</manifest>


在这里面注册一个广播接收器,接收此“窗口小部件”的点击事件

其中,res/xml/widget_info.xml:

<?xml version="1.0" encoding="utf-8"?>  
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
    android:minWidth="100dp"  
    android:minHeight="40dp"  
    android:updatePeriodMillis="86400000"  
    android:initialLayout="@layout/flashlight_widget_layout"  > 
</appwidget-provider>  

就这么多内容。

源码连接:http://download.csdn.net/detail/liu_zhen_wei/4797651





(转发请注明转自:学PHP)    


  相关推荐



1楼 Latesha说: 2016-12-22 05:44:12
Love your outfit! The shoes are heavene and that skirt look faH!pousubole you are having a great time in NYC &#8211; I would looove to be there right now!XX
2楼 Alex说: 2016-12-22 14:00:40
I never would have know about this had you not reblogged. What a beautiful thing story th#1&a82t7;s unfolding from a another beautiful story. This, my friends, is what makes me believe in Santa Claus. xo

  发表评论
昵称:
(不超过20个字符或10个汉字)
内容: