Widget详解

程序运行截图:


一、Widget的创建步骤

widget:桌面小控件
1 写一个类extends AppWidgetProvider 
2 在清单文件件中注册:
<receiver android:name=".ExampleAppWidgetProvider" >
   <intent-filter>
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
   </intent-filter>
   <meta-data android:name="android.appwidget.provider"
              android:resource="@xml/example_appwidget_info" />
</receiver>
3 在res/xml创建example_appwidget_info.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:initialLayout="@layout/example_appwidget"
    >
</appwidget-provider>
4 指定布局  example_appwidget.xml


以上所涉及到的内容都可以在android的官网中找到,其实这里我只是作揖了以下简单翻译而已............


二、生命周期

生命周期:
1 添加到桌面:
  onEnabled() --> onUpdate() 
2 删除
  onDeleted()----> onDisabled()


如果桌面已经有一个了widget的实例存在,再次添加onUpdate()
删除之后,如果桌面上还有widget的实例存在,只会调用onDeleted().


三、以更新时间为例,讲解widget的使用

按照一中所说的方法创建好一个widget

然后

1)ExampleAppWidgetProvider

package com.njupt.widget3;

import java.text.SimpleDateFormat;

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 ExampleAppWidgetProvider extends AppWidgetProvider {

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
	   
		Intent intent = new Intent(context,MyService.class);
		context.startService(intent);
	}
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
		long date = System.currentTimeMillis();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String text = format.format(date);
		views.setTextViewText(R.id.tv_time,text);
		
		Intent intent =  new Intent(context,MainActivity.class);
		PendingIntent pendingIntent =  PendingIntent.getActivity(context, 100, intent, 0);
		views.setOnClickPendingIntent(R.id.tv_time, pendingIntent);
		
		appWidgetManager.updateAppWidget(appWidgetIds, views);
	}
	
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onDeleted(context, appWidgetIds);
	}
	
	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
		
		Intent intent = new Intent(context,MyService.class);
		context.stopService(intent);
	}
}


2)MyService

package com.njupt.widget3;

import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;

public class MyService extends Service {

	private Timer timer;
	private TimerTask task = new TimerTask() {
		
		@Override
		public void run() {
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
			
			
			RemoteViews views = new RemoteViews(getPackageName(), R.layout.example_appwidget);
			long date = System.currentTimeMillis();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String text = format.format(date);
			views.setTextViewText(R.id.tv_time,text);
			
			Intent intent =  new Intent(getApplicationContext(),MainActivity.class);
			PendingIntent pendingIntent =  PendingIntent.getActivity(getApplicationContext(), 100, intent, 0);
			views.setOnClickPendingIntent(R.id.tv_time, pendingIntent);
			
			ComponentName provider = new ComponentName(getApplicationContext(), ExampleAppWidgetProvider.class);
			appWidgetManager.updateAppWidget(provider, views);
		}
	};
	
	public void onCreate() {
		timer = new Timer();
		timer.schedule(task, 1000,1000);
	};
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	    
		timer.cancel();
		task = null;
	}
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}

3)最后不要忘了去AndroidManifest.xml中把service给注册上。。。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值