Widget代码片段

别人代码,鉴于可以参考,复制与此

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.text.format.Time;
import android.widget.RemoteViews;

public class TodayDateSmall extends AppWidgetProvider
{
	   @Override
	   public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
	      RemoteViews updateView = buildUpdate(context);
	      appWidgetManager.updateAppWidget(appWidgetIds, updateView);
	      super.onUpdate(context, appWidgetManager, appWidgetIds);
	   }
	   
	   private String[] months = {"一月", "二月", "三月", "四月",
               "五月", "六月", "七月", "八月",
               "九月", "十月", "十一月", "十二月"};
	   
	   
	   private RemoteViews buildUpdate(Context context) {
	      RemoteViews updateView = null;
	      Time time = new Time();
	      time.setToNow();
	      String month = months[time.month];
	      updateView = new RemoteViews(context.getPackageName(), R.layout.widget_layout_small);
	      updateView.setTextViewText(R.id.Date, new Integer(time.monthDay).toString());
	      updateView.setTextViewText(R.id.Month, month);
	      Intent launchIntent = new Intent();
	      launchIntent.setComponent(new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity"));
	      launchIntent.setAction(Intent.ACTION_MAIN);
	      launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
	      launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
	      PendingIntent intent = PendingIntent.getActivity(context, 0, launchIntent, 0);
	      updateView.setOnClickPendingIntent(R.id.SmallBase, intent);
	      return updateView;
	   }
}
其中配置文件中配置如下:

<receiver
			android:label="@string/app_name"
			android:name=".TodayDate" >
			<intent-filter >
				<action android:name="android.appwidget.action.APPWIDGET_UPDATE" >
				</action>
			</intent-filter>

			<meta-data
				android:name="android.appwidget.provider"
				android:resource="@xml/widget" >
			</meta-data>
		</receiver>

另外,还可以做其他操作,如重写如下四个方法,优化体验(省电)等

	@Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context) {
        // TODO Auto-generated method stub
        super.onDisabled(context);
    }

    @Override
    public void onEnabled(Context context) {
        // TODO Auto-generated method stub
        super.onEnabled(context);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值