通过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. 1.0.x-学习Opencv与MFC混合编程之---视频运动检测

    源代码地址: http://download.csdn.net/detail/nuptboyzhb/3961668 版本1.0.x新增内容 视频运动检测 Ø 新建菜单项,Learning OpenCV ...

  2. Hadoop基于文件的数据结构及实例

    基于文件的数据结构 两种文件格式: 1.SequenceFile 2.MapFile SequenceFile 1.SequenceFile文件是Hadoop用来存储二进制形式的<key,val ...

  3. linux下nginx负载均衡部署

    nginx负载均衡部署 Nginx("engine x") 是一个高性能的 HTTP 和 反向代理 server,也是一个 IMAP/POP3/SMTP 代理server. Ngi ...

  4. JS - 全屏滚动

    下载地址:http://www.lanrentuku.com/js/quanping-1141.html

  5. 进阶:案例六: Context Menu(静态 与 动态)

    实现: 1.add: 2.delete 3.add2 实现步骤: 1.新建属性display_text 2.创建layout 3.代码部分: add事件: METHOD onactionadd . D ...

  6. 使用JDK自带的工具将中文转换为ascii码

    有时候在MyEclipse中,文件只能保存为“ISO-8859-1”的类型,而这种类型的文件时无法保存中文数据的,那么我们只能将中文数据经过Unicode编码才能往文件中保存,这里可以使用JDK自带的 ...

  7. Lazy Math Instructor

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3721   Accepted: 1290 Description A m ...

  8. HTML——使用表格对表单进行布局

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3Vuc2h1bWlu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  9. openCV中cvSnakeImage()函数代码分析

    /*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...

  10. 一劳永逸解决UAC问题(修改QMAKE_LFLAGS_EXE的设置)

    如果你的程序跑在一个开启了UAC保护的系统中,而你的程序又没有"盾牌"的话,程序总是会受到各种阻挠的,比如读写文件,写注册表等. 有了"盾牌"的话就不会出现一些 ...