通过AppWidget应用(一)的介绍,我们已经知道如何创建一个在主界面上显示一个appWidget窗口,但这并不是我们的目的,我们需要做到程序与用户之间进行交互;下面来介绍下如何通过appWidget启动一个Activity。

一、在appWidget的布局文件中添加一个按钮

<?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="vertical"> <TextView
android:id="@+id/txtapp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="test" >
</TextView> <Button
android:id="@+id/btnSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send" >
</Button> </LinearLayout>

二、在appWidget上为按钮添加监听函数

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
//遍历本程序启动的appWidget
for (int i = 0; i < appWidgetIds.length; i++) {
System.out.println("-----------appWidgetIds[] = " + appWidgetIds[i]);
// 创建一个Intent对象
Intent intent = new Intent(context, targetActivity.class); // 启动一个Activity
// 创建一个PendingIntent对象 打开一个Activity
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.appwidgetlayout);
// 为按钮绑定监听器
remoteViews.setOnClickPendingIntent(R.id.btnSend, pendingIntent);
// 更新App
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}

targetActivity 就是点击按钮时要启动的Activity

按照AppWidget应用(一)中的方法启动后的appWidget效果如图:

最后是代码的地址:

点击打开链接

AppWidget应用(二)---PendingIntent 之 getActivity的更多相关文章

  1. Notification(二)——PendingIntent的flag导致数据同样的问题

    MainActivity例如以下: package cc.cu; import android.os.Bundle; import android.view.View; import android. ...

  2. android app widget 创建调用周期

    1 ?Android widget 大小问题 2 ?RemoteViewService Android开发历程_15(AppWidget的使用) Appwidget就是手机应用中常常放在桌面(即hom ...

  3. 完全理解Android中的RemoteViews

    一.什么是RemoteViews RemoteViews翻译过来就是远程视图.顾名思义,RemoteViews不是当前进程的View,是属于SystemServer进程.应用程序与RemoteView ...

  4. notification 使用的基本方法

    当某个应用程序希望向用户发出一些提示信息,而应用程序又不在前台,可以借助Notification来实现.发出一条通知后,手机最上方额通知栏会显示一个图标,下来状态栏以后可以看到详细内容. 一.通知的基 ...

  5. Intent 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android Intent 是一个消息传递对象,主要用于组建之间的通讯,例如:启动Activit ...

  6. Android入门(十五)通知

    原文链接:http://www.orlion.ga/663/ 1.通知的基本用法 创建通知的步骤,首先需要一个NotificationManager来对通知进行管理,可以调用Context的getSy ...

  7. android: 使用通知

    8.1   使用通知 通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望向 用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现.发 ...

  8. Android开发之发送短信

    本实例通过SmsManager的sendTextMessage方法实现发送短信关于SmsManager的具体解释大家能够參照:Android开发之SmsManager具体解释 实例执行效果图: 程序代 ...

  9. 通知 Notification 详解

    效果 通知栏-刚收到通知时 通知栏-收到通知几秒后 标准视图 大视图-下滑前是标准视图 大视图-下滑后显示大视图 自定义通知 讲解 Notification,俗称通知,是一种具有全局效果的通知,它展示 ...

随机推荐

  1. HDU 1030 Delta-wave 数学题解

    给出一个数字塔,然后求沿着数字之间的边走,给出两个数字,问其路径最短的长度是多少. 看似一条搜索题目,只是有一定做题经验的人都知道,这个不是搜索题,直接搜索肯定超时. 这个是依据规律计算的数学题目. ...

  2. mssql 返回表的创建语句

    if OBJECT_ID('sp_create_table_sql','P') is not null drop proc sp_create_table_sql go create proc sp_ ...

  3. 内省(二)之BeanUtils工具类

    上一篇内省(Introspector)讲到的是采用JavaAPI中的类来操作bean及其属性,而Apache也开源了第三方框架来简化和丰富了对bean属性的操作,这个框架就是BeanUtils. 使用 ...

  4. 博弈论之Nim

    博弈论(一):Nim游戏 重点结论:对于一个Nim游戏的局面(a1,a2,...,an),它是P-position当且仅当a1^a2^...^an=0,其中^表示位异或(xor)运算. Nim游戏是博 ...

  5. PDCA模型的学习

    PDCA是广泛应用于质量控制中的一种管理学模型. P即plan,分析和了解当前的状况,然后作出改进的计划: D即do,执行所作出的计划: C即check,对执行的结果进行检查,要确认哪些是对的,哪些是 ...

  6. boost锁的概述

    ●     boost锁的概述 boost库中提供了mutex类与lock类,通过组合可以轻易的构建读写锁与互斥锁. ▲     mutex对象类 mutex类主要有两种:boost::mutex,b ...

  7. redhat6.3 jfreechar中文乱码解决途径

    最近使用到jfreechar的项目,在转移到linux上面时出现中文乱码(中文被显示为框框),网上查了一些资料,结合自身系统的特性,总结了一种安装字体的方式.在说字体安装之前首先上个测试的代码吧:we ...

  8. android图片压缩的3种方法实例

    android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...

  9. android电池充电以及电量检测驱动分析

    前段时间比较烦躁,各种不想学习不想工作,于是休息了几天.这几天又下来任务了--调试充电电路和电池电量检测电路,于是又开始工作,顺便把调试过程记录下来. 平台: cpu        飞思卡尔imx6q ...

  10. Android:通知栏的使用

    非常久没有使用Android的通知功能了,今天把两年前的代码搬出来一看.发现非常多方法都废弃了,代码中各种删除线看的十分不爽.于是乎,打开Google,查看官方文档.学习最新的发送通知栏消息的方法. ...