StackView Widget示例程序(StackWidgetProvider.java)
来源:互联网 发布:科比2012赛季数据 编辑:程序博客网 时间:2024/05/17 15:59
StackWidgetProvider.java文件
原文地址:http://developer.android.com/resources/samples/StackWidget/src/com/example/android/stackwidget/StackWidgetProvider.html
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示例程序(StackWidgetProvider.java)
- StackView Widget示例程序(StackWidgetService.java)
- StackView Widget示例程序(WidgetItem.java)
- StackView Widget示例程序
- StackView Widget示例程序(stackwidgetinfo.xml)
- StackView Widget示例程序(AndroidManifest.xml)
- StackView Widget示例程序(widget_item_background.xml)
- StackView Widget示例程序(strings.xml)
- StackView
- StackView
- Android Widget 小部件(四---完结) 使用ListView、GridView、StackView、ViewFlipper展示Widget
- java 枚举类型 enum (示例程序)
- Java小程序示例
- java小程序 示例
- Java基础示例程序
- android studio StackView控件的源码解释和简单示例
- WIDGET滚动设置示例代码(例中WIDGET绑定在IUTKFORM中)
- StackView滑动预览图片(LG_stackView)---bug
- StackView Widget示例程序(stackwidgetinfo.xml)
- 软件架构师高级实践
- 文本相似度算法(余弦定理)
- 回馈给以后的学弟、学妹——从山科到北航的跨越
- 回忆那些楼市成交的黄金岁月(全)
- StackView Widget示例程序(StackWidgetProvider.java)
- 工业和信息化部全国信息技术水平考试
- unity用socket(TcpLitsener)来操作sqlserver
- mysql对表字段的操作【添加,修改,删除】
- Android中.9.png格式的图片的制作
- 2013年计算机技术与软件专业技术资格(水平)考试安排
- StackView Widget示例程序(StackWidgetService.java)
- MIPS 指令
- MIPS 通用寄存器