android widget实例
来源:互联网 发布:法国电影预先知剧情 编辑:程序博客网 时间:2024/06/07 14:50
这个例子是做一个android widget,widget布局很简单,就是一个button。当长按屏幕,出现widget小部件时,选中创建的widget,就会出现这个button样子的小部件安装到手机上。点击button后就会跳转到一个Activiity,提供号码和短信内容输入的地方,再发送短信。短信发出后,会根据发送和接受状态提示用户是否发送成功或接收成功。当对方发来的短信成功接收后,再通过一个对话框形式的Activity展示到屏幕上。这里在src下面主要创建三个.java文件,一个类用于继承AppWidgetProvider,一个用来提供用户输入页面的逻辑操作的Activity,还有一个对话框形式的Activity用来显示接收到的信息。
步骤:
首先,新建AppWidgetProviderInfo。这个是用来描述AppWidget的大小,更新频率以及初始页面等信息的,是以一个XML文件的形式。
1、在res下新建名为XML的文件夹,在该文件夹下面创建一个.xml的文件,如sendWidgetInfo.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/sendwidget" android:minHeight="72dp" android:minWidth="100dp" android:updatePeriodMillis="1000" ></appwidget-provider>
2、这里android:initialLayout="@layout/sendwidget"是制定widget的布局文件。这个例子的布局很简单,就是一件简单的Button。这个布局文件是在layout文件夹下面创建的,名为sendweidget.xml。与描述这个布局的AppWidgetProviderInfo文件相对应。代码如下:
<?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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送短信" android:layout_gravity="center_horizontal" /></LinearLayout>
3,写一个类继承AppWidgetProvider,重写onUpdate()和onReceive()方法:
package com.example.sendwidget;import android.app.Activity;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.os.Bundle;import android.telephony.SmsManager;import android.telephony.SmsMessage;import android.widget.RemoteViews;import android.widget.Toast;public class SendMsm extends AppWidgetProvider { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if ("android.provider.Telephony.SMS_RECEIVED".equals(action)){ switch (getResultCode()) { case Activity.RESULT_OK: // 短信接收成功 Toast.makeText(context, "短信接收成功", Toast.LENGTH_LONG).show(); String sender = ""; String content = ""; Bundle bundle = intent.getExtras(); if (bundle != null) { Object pdus[] = (Object[]) bundle.get("pdus"); SmsMessage messages[] = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); sender = messages[i].getOriginatingAddress(); content += messages[i].getMessageBody(); } Intent in = new Intent("com.example.sendwidget.action.diag"); in.setClass(context, ReadActivity.class); in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP); in.putExtra("sender", sender); in.putExtra("content", content); context.startActivity(in); RemoteViews remoteviews = new RemoteViews(context.getPackageName(), R.layout.sendwidget); AppWidgetManager appwidget_manager = AppWidgetManager.getInstance(context); ComponentName component_name = new ComponentName(context, SendMsm.class); appwidget_manager.updateAppWidget(component_name, remoteviews); } break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: // 发送短信失败 Toast.makeText(context, "接收短信失败", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: break; case SmsManager.RESULT_ERROR_NULL_PDU: break; } }else { super.onReceive(context, intent); } } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { for (int i = 0; i < appWidgetIds.length; i++) { int appWidget = appWidgetIds[i]; Intent clickintent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clickintent, 0); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.sendwidget); remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent); appWidgetManager.updateAppWidget(appWidget, remoteViews); } super.onUpdate(context, appWidgetManager, appWidgetIds); }}
这里,onUpdate()方法里面通过PendingIntent.getActivity()方法,跳转到一个新的Activity,用于提供用户填写号码和内容的,当然此widget也可以做成直接点击按钮发送短信到指定号码,不用getActivity()方法,而是直接用PendingIntent.getBroadcast(context, 0 , intentClick, 0);将内容发送出去。这里用页面跳转的方法,就可以向任何号码发送信息。RemoteView 用于找到组件的布局文件,提供输入页面的布局文件如下:
<LinearLayout 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:orientation="vertical" 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" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="号码" /> <EditText android:id="@+id/numEdt" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容" /> <EditText android:id="@+id/msmEdt" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" /> </LinearLayout> <Button android:id="@+id/sendBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="发送" /></LinearLayout>
- Android中Widget实例
- android widget 开发实例
- android widget 开发实例
- android widget开发实例
- android widget 开发实例
- android widget实例
- Android中Widget实例
- Android的Widget编写实例
- 解析Android Widget开发实例
- Android Widget实例代码解析
- Android App Widget开发实例
- android之App Widget开发实例
- Android之App Widget开发实例
- Android Widget 实例(世界杯倒计时)
- android用户界面之Widget教程实例汇总
- android用户界面之Widget教程实例汇总
- android用户界面之Widget教程实例汇总
- android用户界面之Widget教程实例汇总
- struts2——学习笔记1——struts2封装表单数据方式(三种)
- C语言中的预处理详解
- 正则表达式元字符
- 设定虚拟WiFi网卡
- 嵌入汇编与CPUID指令
- android widget实例
- 黑马程序员_java基础day24
- 挨踢江湖之一
- C语言文件操作函数大全(转)
- 人人都来写算法 之 选择排序
- struts+ajax验证注册用户名是否存在
- 对Chrome OS 仅有的一点了解
- SWING设置背景图片
- 血色城堡和恶魔广场