注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

StackView Widget示例程序(StackWidgetProvider.java)  

2011-08-03 13:47:35|  分类: 开发专题 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
StackWidgetProvider.java文件
he file containing the source code shown below is located in the corresponding directory in <sdk>/samples/android-<version>/...
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.example.android.stackwidget;

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

public class StackWidgetProvider extends AppWidgetProvider {
   
public static final String TOAST_ACTION = "com.example.android.stackwidget.TOAST_ACTION";
   
public static final String EXTRA_ITEM = "com.example.android.stackwidget.EXTRA_ITEM";

   
@Override
   
public void onDeleted(Context context, int[] appWidgetIds) {
       
super.onDeleted(context, appWidgetIds);
   
}

   
@Override
   
public void onDisabled(Context context) {
       
super.onDisabled(context);
   
}

   
@Override
   
public void onEnabled(Context context) {
       
super.onEnabled(context);
   
}

   
@Override
   
public void onReceive(Context context, Intent intent) {
       
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
       
if (intent.getAction().equals(TOAST_ACTION)) {
           
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                   
AppWidgetManager.INVALID_APPWIDGET_ID);
           
int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0);
           
Toast.makeText(context, "Touched view " + viewIndex, Toast.LENGTH_SHORT).show();
       
}
       
super.onReceive(context, intent);
   
}

   
@Override
   
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
       
// update each of the widgets with the remote adapter
       
for (int i = 0; i < appWidgetIds.length; ++i) {

           
// Here we setup the intent which points to the StackViewService which will
           
// provide the views for this collection.
           
Intent intent = new Intent(context, StackWidgetService.class);
            intent
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
           
// When intents are compared, the extras are ignored, so we need to embed the extras
           
// into the data so that the extras will not be ignored.
            intent
.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
           
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            rv
.setRemoteAdapter(appWidgetIds[i], R.id.stack_view, intent);

           
// The empty view is displayed when the collection has no items. It should be a sibling
           
// of the collection view.
           
rv.setEmptyView(R.id.stack_view, R.id.empty_view);//@1

           
// Here we setup the a pending intent template. Individuals items of a collection
           
// cannot setup their own pending intents, instead, the collection as a whole can
           
// setup a pending intent template, and the individual items can set a fillInIntent
           
// to create unique before on an item to item basis.
           
Intent toastIntent = new Intent(context, StackWidgetProvider.class);
            toastIntent
.setAction(StackWidgetProvider.TOAST_ACTION);
           
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);//@2
            intent
.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
           
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,
                   
PendingIntent.FLAG_UPDATE_CURRENT);
           
rv.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);

            appWidgetManager
.updateAppWidget(appWidgetIds[i], rv);
       
}
       
super.onUpdate(context, appWidgetManager, appWidgetIds);
   
}
}
注意1:@1处表示当R.id.stack_view这个集合View的数据集为空时(即没有数据项时),用R.id.empty_view进行替代显示.
注意2:@2处把widget Id放在在了toastIntent,以区分是哪个widget实例。
关于StackView Widget示例程序的更多内容请参考《StackView Widget示例程序
  评论这张
 
阅读(1401)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017