android开发之APP Widget

android开发之APP Widget

本博文主要讲述的android开发中的桌面程序的开发--APP widget,主要用的是PendingIntent和RemoteViews。

PendingIntent主要用来设置桌面程序的相应方式。此对象可以有三种Intent方式,第一个是开始一个新的Activity,第二个是发送一个广播,第三个是开始一个service。

RemoteViews的作用:因为桌面程序和app程序不是属于一个进程,不能通过正常的操作控件的方式来操作。需要使用系统提供的RemoteViews对象来操作桌面程序。在这个类中有设置APP Widget中控件属性的方法。

下面我们来看看代码,在代码中有分别包含了上述所说的PendingInetnt的两种方式(part 1:开始一个新的Activity;part 2:发送一个广播)和RemoteViews的使用方法:

主程序的Activity,MainActivity.java:

package com.example.appwidget;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

appwidget桌面程序的类AppWidgetProviderTest.java:

package com.example.appwidget;

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.widget.RemoteViews;

public class AppWidgetProviderTest extends AppWidgetProvider {

//自定义一个ACTION常量

private final String USER_ACTION = "com.example.appwiget.USER_ACTION";

//当APP widget实例被删除时,会调用

@Override

public void onDeleted(Context context, int[] appWidgetIds) {

// TODO Auto-generated method stub

super.onDeleted(context, appWidgetIds);

System.out.println("onDeleted()");

}

//当最后一个app widget实例被删除时,会调用

@Override

public void onDisabled(Context context) {

// TODO Auto-generated method stub

super.onDisabled(context);

System.out.println("onDisabled()");

}

//当第一个App widget实例被创建时,会调用

@Override

public void onEnabled(Context context) {

// TODO Auto-generated method stub

super.onEnabled(context);

System.out.println("onEnabled()");

}

//接收app收到的所有广播事件

@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

System.out.println("onReceive()");

//比较下,如果收到的广播是USER_ACTION,则输出信息,且更新桌面控件

if(USER_ACTION.equals(intent.getAction())){

System.out.println("onReceive --> " + USER_ACTION);

//获得RemoteViews对象

RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.appwidget_provider);

//更新桌面TextView控件的text

remoteView.setTextViewText(R.id.myText, "onReceive");

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

ComponentName componentName = new ComponentName(context, AppWidgetProviderTest.class);

appWidgetManager.updateAppWidget(componentName, remoteView);

}else{

//调用父类的方法

super.onReceive(context, intent);

}

}

//在到达指定的更新时间之后或者向桌面添加App widget实例时,会调用此方法

@Override

public void onUpdate(Context context, AppWidgetManager appWidgetManager,

int[] appWidgetIds) {

// TODO Auto-generated method stub

super.onUpdate(context, appWidgetManager, appWidgetIds);

System.out.println("onUpdate()");

//为每一个app widget实例绑定一个remoteViews监听器,每创建的一个app widget实例都有一个对应的ID

for(int i = 0; i < appWidgetIds.length; i ++){

System.out.println("appWidgetIds -->" + appWidgetIds[i]);

//创建一个Intent对象

Intent intent = new Intent();

//part 1:pendingIntent用来打开一个新的Activity

/*intent.setClass(context, MainActivity.class);

//使用getActivity方法创建一个pendingIntent对象,此对象用于跳转Activity,将intent打包给pendingIntent

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

*/

//end part1

//part 2:pendingIntent用来发送一个广播

//设置action

intent.setAction(USER_ACTION);

//使用getBroadcast()方法创建一个pendingIntent对象

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

//end part2

//创建一个RemoteViews对象

RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider);

//RemoteViews对象将Button和pendingIntent绑定。通过pendingIntent获得intent

remoteView.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent);

appWidgetManager.updateAppWidget(appWidgetIds[i], remoteView);

}

}

}

主程序的主布局文件main.xml:(程序的主界面)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

</RelativeLayout>

app widget的界面布局文件appwidget_provider.xml:(在桌面看到的界面)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<TextView

android:id="@+id/myText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="welcome appwidget"

android:background="#ff0000"

/>

<Button

android:id="@+id/WidgetButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Widget Button"

/>

</LinearLayout>

appwidget_provider的元数据的文件appwidget_provider_info.xml:

<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider

xmlns:android="http://schemas.android.com/apk/res/android"

android:minWidth="180dp"

android:minHeight="40dp"

android:updatePeriodMillis="1000"

android:initialLayout="@layout/appwidget_provider"

>

</appwidget-provider>

AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.appwidget"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="17" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.example.appwidget.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<receiver

android:name="com.example.appwidget.AppWidgetProviderTest">

<intent-filter>

<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>

</intent-filter>

<intent-filter ><!--自定义的过滤器  -->

<action android:name="com.example.appwiget.USER_ACTION"/>

</intent-filter>

<meta-data

android:name="android.appwidget.provider"

android:resource="@xml/appwidget_provider_info"/>

</receiver>

</application>

</manifest>

实现的效果如下:

主程序界面:

app widget界面:

app widget需要在桌面上添加我们创建的app,才会在桌面上显示出来。可以空白处长按,就会出来添加widget选项,选中我们的app既会在桌面显示上图。

如上程序,点击按钮,就会发送广播,并且修改widget中的TextView控件的Text,点击后的效果如下:

时间: 2024-10-11 20:55:02

android开发之APP Widget的相关文章

Android开发之App Widget的学习及使用

(以下为本人对于官方文档的理解,能力有限,有错还望指出) 1.首先,我们先阅读下官方文档,后面活给出Demo程序 App Widgets(小工具,窗口小部件) App Widgets are miniature application views that can be embedded in other applications (such as the Home screen) and receive periodic(周期的,定期的) updates. These views are re

android开发之MediaPlayer+Service MP3播放器

http://blog.csdn.net/zzy916853616/article/details/6450753 [java] view plaincopy import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.apps.service.Player

Android开发之InstanceState详解

Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceState(),并且在介绍这两个方法之后,再分别来实现使用InstanceState保存和恢复数据功能.Android实现屏幕旋转异步下载效果这样两个示例. 首先来介绍onSaveInstanceState() 和 onRestoreInstanceState() .关于这两个方法,一些朋友可能在Andr

Android开发之WebView详解

概述: 一个显示网页的视图.这个类是你可以滚动自己的Web浏览器或在你的Activity中简单地显示一些在线内容的基础.它使用了WebKit渲染引擎来显示网页,包括向前和向后导航的方法(通过历史记录),放大和缩小,执行文本搜索等. 需要注意的是:为了让你的应用能够使用WebView访问互联网和加载网页,你必须添加Internet的权限在Android Manifest文件中: <uses-permission android:name="android.permission.INTERNE

Android开发之UI更新交互机制与实例解析

android开发过程中,经常需要更新UI的状态和文案等.这是就需要对UI进行 更新.在android中更新UI一般有三种方法,handler机制.RunOnUiThread方法以及AsyncTask异步类方法等 本文下面就这三种方法进行了演示和代码实现. a.Handler机制通过使用消息机制来实现 b.RunOnUiThread方法是通过运行UI线程来达到更新UI的目的 c.AsyncTask是异步类,通过异步更新来更新UI 效果图如下:           (1)Java功能实现代码如下:

Android开发之IPC进程间通信-AIDL介绍及实例解析

一.IPC进程间通信 IPC是进程间通信方法的统称,Linux IPC包括以下方法,Android的进程间通信主要采用是哪些方法呢? 1. 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信:   2. 信号(Signal):信号是比较复杂的通信方式,用于通知接受进程有某种事件发生,除了用于进程间通信外,进程还可以发送信号给进程本身:linux除了支持Unix早期

Android开发之Handler的使用方法(源代码分享)

Handler主要接受子线程发送的数据, 并用此数据配合主线程更新UI.. 当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件,进行事件分发.比如说, 你要是点击一个 Button ,Android会分发事件到Button上,来响应你的操作.  如果此时需要一个耗时的操作,例如: 联网读取数据或者读取本地较大的一个文件的时候,你不能把这些操作放在主线程中,,如果你放在主线程中的话,界面会出现假死现象, 如果5秒钟还没有完成的话,,会收到An

android开发之Animations的使用(三)

android开发之Animations的使用(三) 本博文主要讲述的是,Animations在android开发中的用来循环播放动画的效果: MainActivity.java: package com.example.animationtest3; import android.os.Bundle; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.v

Android开发之PopupWindow

/* *  Android开发之PopupWindow * *  Created on: 2011-8-8 *  Author: blueeagle *  Email: [email protected] */ 聪明的人善于总结,记录,不知道这是谁说的了,反正要当一个聪明人,我得先学会总结,记录.最近在Android的学习过程中,发现 PopupWindow也是值得研究一番的一个东东,因此拿过来说道说道.与其相似的就归纳到一起说道吧,那就是AlertDialog和Toast. PopupWind