android桌面时钟小控件开发记录

所谓桌面小控件,就是能之间显示在Android系统桌面的小程序。

桌面小控件的实现是基于Broadcast的形式实现的,因此,每一个桌面小控件都对应于一个BroadcastReceiver类。Android系统提供了一个AppWidgetProvider类(它就是BroadcastReceiver的子类),这个类很关键,你在写桌面小控件时只需继承这个类就行。继承了AppWidgetProvider类之后,你可以根据自己的需要覆盖它的不同的生命周期的方法,来达到自己的目的。AppWidgetProvider类的主要提供如下不同生命周期的方法:

  1. void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) { }
  2. // 这个方法字面意思是负责更新桌面小控件,但貌似只有在小控件被用户放到桌面上时被调用了一次,读者可以自己通过输出Log来测试
  3. // 实现桌面控件是通常都会考虑重写该方法
  4. void onDeleted(Context context, int[] appWidgetIds)
  5. // 在小控件被删除时调用该方法
void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) { }
// 这个方法字面意思是负责更新桌面小控件,但貌似只有在小控件被用户放到桌面上时被调用了一次,读者可以自己通过输出Log来测试
// 实现桌面控件是通常都会考虑重写该方法 

void onDeleted(Context context, int[] appWidgetIds)
// 在小控件被删除时调用该方法

一般来说,开发桌面小控件只需要定义一个AppWidgetProvider的子类,并重新写它的pnUpdate方法即可。

下面上代码,有不足之处请指教:

DesktopClock.java,这个类继承了上面说的AppWidgetProvider类(记住它是继承自BroadcastReceiver类):

  1. package org.ls;
  2. import android.appwidget.AppWidgetManager;
  3. import android.appwidget.AppWidgetProvider;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.util.Log;
  7. import android.widget.RemoteViews;
  8. public class DesktopClock extends AppWidgetProvider {
  9. @Override
  10. public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  11. int[] appWidgetIds) {
  12. Log.e("appwidget", "--update--");
  13. // 创建RemoteViews对象
  14. RemoteViews views = new RemoteViews(context.getPackageName(),
  15. R.layout.main);
  16. views.setImageViewResource(R.id.double_dot,
  17. R.drawable.blue_modern_middle);
  18. // 将刷新UI的service的必要的数据设置好(此处没有使用Bundle传递数据)
  19. UpdateUIService.appWidgetManager = appWidgetManager;
  20. UpdateUIService.context = context;
  21. UpdateUIService.remoteViews = views;
  22. // 启动刷新UI的Service
  23. Intent intent = new Intent(context, UpdateUIService.class);
  24. context.startService(intent);
  25. }
  26. // 在小控件被删除时调用该方法停止Service
  27. @Override
  28. public void onDeleted(Context context, int[] appWidgetIds) {
  29. super.onDeleted(context, appWidgetIds);
  30. Log.e("appwidget", "--deleted--");
  31. Intent intent = new Intent(context, UpdateUIService.class);
  32. context.stopService(intent);
  33. }
  34. }
package org.ls;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class DesktopClock extends AppWidgetProvider {

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		Log.e("appwidget", "--update--");
		// 创建RemoteViews对象
		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.main);
		views.setImageViewResource(R.id.double_dot,
				R.drawable.blue_modern_middle);
		// 将刷新UI的service的必要的数据设置好(此处没有使用Bundle传递数据)
		UpdateUIService.appWidgetManager = appWidgetManager;
		UpdateUIService.context = context;
		UpdateUIService.remoteViews = views;
		// 启动刷新UI的Service
		Intent intent = new Intent(context, UpdateUIService.class);
		context.startService(intent);
	}

	// 在小控件被删除时调用该方法停止Service
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
		Log.e("appwidget", "--deleted--");
		Intent intent = new Intent(context, UpdateUIService.class);
		context.stopService(intent);
	}
}


UpdateUIService.java,这个类继承自Service类,是一个后台运行的服务,它的主要职责是在它里面有一个动态注册的BroadcastReceiver的实例(也即调用了Context里面的registerReceiver( )方法注册的一个BroadcastReceiver的实例,而不是在xml中注册),负责监听系统的时间变化的广播,然后更新小控件的UI。

还有一点要注意的是,读者可能会有这样的疑问,不是说AppWidgetProvider本身就是一个BroadcastReceiver吗,为什么不让它直接监听系统的时间变化的广播呢?注意,虽然AppWidgetProvider本身就是一个BroadcastReceiver,但是它是一个“分化”了的BroadcastReceiver,不能再监听其它的系统广播了。一般的BroadcastReceiver是这样的:每次系统的Broadcast事件发生后,系统就会创建对应的BroadcastReceiver的实例,并自动触发它的onReceive( )方法,onReceive( )执行完后,BroadcastReceiver的实例就会被销毁。但是这个“分化”了的BroadcastReceiver——AppWidgetProvider显然做不到这样,也即系统尝试再创建它的实例时会发生异常,因此不能用它之间监听系统的Broadcast了。

那我为什么不在AppWidgetProvider里面新动态注册一个BroadcastReceiver,而是要新开一个Service呢?笔者也曾经尝试过这样做,但是当调用registerReceiver( )方法时编译通不过,此外,如果不在AppWidgetProvider里面新开一个Service,AppWidgetProvider并不是会一直在后台运行的,执行完onUpdate方法后就会退出执行,因此最好的方法就是在onUpdate里面新开一个Service。

  1. package org.ls;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import android.app.Service;
  5. import android.appwidget.AppWidgetManager;
  6. import android.content.BroadcastReceiver;
  7. import android.content.ComponentName;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.os.IBinder;
  12. import android.util.Log;
  13. import android.widget.RemoteViews;
  14. public class UpdateUIService extends Service {
  15. private SimpleDateFormat df = new SimpleDateFormat("HHmmss");
  16. public static Context context;
  17. public static AppWidgetManager appWidgetManager;
  18. public static RemoteViews remoteViews;
  19. // 数字图片的ID
  20. private int[] numberIcon = new int[] { R.drawable.blue_modern_zero,
  21. R.drawable.blue_modern_one, R.drawable.blue_modern_two,
  22. R.drawable.blue_modern_three, R.drawable.blue_modern_four,
  23. R.drawable.blue_modern_five, R.drawable.blue_modern_six,
  24. R.drawable.blue_modern_seven, R.drawable.blue_modern_eight,
  25. R.drawable.blue_modern_nine };
  26. // 用于显示数字的ImageView的ID
  27. private int[] numberView = new int[] { R.id.hour01, R.id.hour02,
  28. R.id.minute01, R.id.minute02 };
  29. // 覆盖基类的抽象方法
  30. @Override
  31. public IBinder onBind(Intent intent) {
  32. return null;
  33. }
  34. // 在本服务创建时将监听系统时间的BroadcastReceiver注册
  35. @Override
  36. public void onCreate() {
  37. super.onCreate();
  38. Log.e("service", "--service created--");
  39. IntentFilter intentFilter = new IntentFilter();
  40. intentFilter.addAction(Intent.ACTION_TIME_TICK); // 时间的流逝
  41. intentFilter.addAction(Intent.ACTION_TIME_CHANGED); // 时间被改变,人为设置时间
  42. registerReceiver(boroadcastReceiver, intentFilter);
  43. }
  44. @Override
  45. public int onStartCommand(Intent intent, int flags, int startId) {
  46. Log.e("service", "--service started--");
  47. updateUI(); // 开始服务前先刷新一次UI
  48. return START_STICKY;
  49. }
  50. // 在服务停止时解注册BroadcastReceiver
  51. @Override
  52. public void onDestroy() {
  53. super.onDestroy();
  54. unregisterReceiver(boroadcastReceiver);
  55. }
  56. // 用于监听系统时间变化Intent.ACTION_TIME_TICK的BroadcastReceiver,此BroadcastReceiver须为动态注册
  57. private BroadcastReceiver boroadcastReceiver = new BroadcastReceiver() {
  58. @Override
  59. public void onReceive(Context acontext, Intent intent) {
  60. // Log.e("time received", "--receive--");
  61. updateUI();
  62. }
  63. };
  64. // 根据当前时间设置小部件相应的数字图片
  65. private void updateUI() {
  66. String timeString = df.format(new Date());
  67. int num;
  68. for (int i = 0; i < numberView.length; i++) {
  69. num = timeString.charAt(i) - 48;
  70. remoteViews.setImageViewResource(numberView[i], numberIcon[num]);
  71. }
  72. // 将AppWidgetProvider的子类包装成ComponentName对象
  73. ComponentName componentName = new ComponentName(context,
  74. DesktopClock.class);
  75. // 调用AppWidgetManager将remoteViews添加到ComponentName中
  76. appWidgetManager.updateAppWidget(componentName, remoteViews);
  77. }
  78. }
package org.ls;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

public class UpdateUIService extends Service {

	private SimpleDateFormat df = new SimpleDateFormat("HHmmss");

	public static Context context;
	public static AppWidgetManager appWidgetManager;
	public static RemoteViews remoteViews;
	// 数字图片的ID
	private int[] numberIcon = new int[] { R.drawable.blue_modern_zero,
			R.drawable.blue_modern_one, R.drawable.blue_modern_two,
			R.drawable.blue_modern_three, R.drawable.blue_modern_four,
			R.drawable.blue_modern_five, R.drawable.blue_modern_six,
			R.drawable.blue_modern_seven, R.drawable.blue_modern_eight,
			R.drawable.blue_modern_nine };
	// 用于显示数字的ImageView的ID
	private int[] numberView = new int[] { R.id.hour01, R.id.hour02,
			R.id.minute01, R.id.minute02 };

	// 覆盖基类的抽象方法
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	// 在本服务创建时将监听系统时间的BroadcastReceiver注册
	@Override
	public void onCreate() {
		super.onCreate();
		Log.e("service", "--service created--");
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Intent.ACTION_TIME_TICK); // 时间的流逝
		intentFilter.addAction(Intent.ACTION_TIME_CHANGED); // 时间被改变,人为设置时间
		registerReceiver(boroadcastReceiver, intentFilter);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.e("service", "--service started--");
		updateUI(); // 开始服务前先刷新一次UI
		return START_STICKY;
	}

	// 在服务停止时解注册BroadcastReceiver
	@Override
	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(boroadcastReceiver);
	}

	// 用于监听系统时间变化Intent.ACTION_TIME_TICK的BroadcastReceiver,此BroadcastReceiver须为动态注册
	private BroadcastReceiver boroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context acontext, Intent intent) {
//			Log.e("time received", "--receive--");
			updateUI();
		}
	};

	// 根据当前时间设置小部件相应的数字图片
	private void updateUI() {
		String timeString = df.format(new Date());
		int num;
		for (int i = 0; i < numberView.length; i++) {
			num = timeString.charAt(i) - 48;
			remoteViews.setImageViewResource(numberView[i], numberIcon[num]);
		}
		// 将AppWidgetProvider的子类包装成ComponentName对象
		ComponentName componentName = new ComponentName(context,
				DesktopClock.class);
		// 调用AppWidgetManager将remoteViews添加到ComponentName中
		appWidgetManager.updateAppWidget(componentName, remoteViews);
	}
}

下面是一些xml文件:

布局文件main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="horizontal" >
  6. <ImageView
  7. android:id="@+id/hour01"
  8. android:layout_weight="1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content" />
  11. <ImageView
  12. android:id="@+id/hour02"
  13. android:layout_weight="1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content" />
  16. <ImageView
  17. android:id="@+id/double_dot"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content" />
  20. <ImageView
  21. android:id="@+id/minute01"
  22. android:layout_weight="1"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content" />
  25. <ImageView
  26. android:id="@+id/minute02"
  27. android:layout_weight="1"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content" />
  30. </LinearLayout>
<?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="horizontal" >

   <ImageView
	android:id="@+id/hour01"  
	android:layout_weight="1"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" />
   
   <ImageView
	android:id="@+id/hour02"  
	android:layout_weight="1"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" />
   
   <ImageView
	android:id="@+id/double_dot"  
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" />
   
   <ImageView
	android:id="@+id/minute01"  
	android:layout_weight="1"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" />
   
   <ImageView
	android:id="@+id/minute02"  
	android:layout_weight="1"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" />

</LinearLayout>

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="org.ls"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk android:minSdkVersion="10" />
  7. <application
  8. android:icon="@drawable/ic_launcher"
  9. android:label="@string/app_name" >
  10. <receiver android:name=".DesktopClock"
  11. android:label="@string/app_name">
  12. <intent-filter>
  13. <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  14. <category android:name="android.intent.category.DEFAULT" />
  15. </intent-filter>
  16. <meta-data android:name="android.appwidget.provider"
  17. android:resource="@xml/appwidget_provider" />
  18. </receiver>
  19. <service android:name=".UpdateUIService" >
  20. </service>
  21. </application>
  22. </manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.ls"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        
        <receiver android:name=".DesktopClock"	
			android:label="@string/app_name">
			
			<intent-filter>
				<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
				<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
			
			<meta-data android:name="android.appwidget.provider"
				android:resource="@xml/appwidget_provider" />

		</receiver>
		
        <service android:name=".UpdateUIService" >
            
        </service>
    
    </application>

</manifest>

用于配置时钟小控件属性的appwidget_provider.xml(在 AndroidManifest.xml中被引用):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:minWidth="270px"
  5. android:minHeight="103px"
  6. android:updatePeriodMillis="1000"
  7. android:initialLayout="@layout/main">
  8. </appwidget-provider>
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider 
	xmlns:android="http://schemas.android.com/apk/res/android" 
	android:minWidth="270px"
	android:minHeight="103px"
	android:updatePeriodMillis="1000"
	android:initialLayout="@layout/main">
</appwidget-provider>

效果图:同步更新时间,完全与系统时间同步:


欢迎讨论。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值