Android学习-----Android组件通信PendingIntent
2015-11-17 11:39
344 查看
Intent 的主要功能是表示用户的一种操作意图,使用 Intent 之后将立即执行用户所有需要的操作,但是在 Android 中也提供了一个 PendingIntent 操纵,表示将要发生的操作。苏伟将要发生的 Intent 是指在当前的 Activity 不立即使用此 Intent 进行处理,而将此 Intent 封装后传递给其他 Activity 程序,而其他 Activity 程序在需要使用此 Intent 时才进行操作。
Intent :表示立刻执行;
PendintIntent :表示的是暂缓执行,遇到特殊条件才执行;
PendingIntent 与 Intent 没有任何继承关系,所以这两个类表示两种不同的 Intent 操作,其方法和常量有:
在 Android 操作系统中,狠毒地方都要使用 PendingIntent 类,如发送一些用户的通知( Notification )或者为用户发送短信( SMS )等都会使用到此类。
一、发送通知: Notification
Androd.app.Notification 与 Toast 类似,可以直接在 Android 手机屏幕的最上面显示通知信息。使用 Notification 定义一条提示信息的标题、时间、内容以及具体的触发操作
然后我们再通过 android.app.NotificationManager 类,该类就相当于一个发布 Notification 信息的组件,如果把 NotificationManager 类看作一个新闻广播,那么每个 Notification 就可以看作一条条的新闻信息。 NotificationManager 常用方法如下:
范例:
PendingIntent01_NotificationActivity.java
Java代码
package com.iflytek.demo;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.os.Bundle;
public class PendingIntent01_NotificationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
NotificationManager notificationManager = (NotificationManager) super
.getSystemService(Activity.NOTIFICATION_SERVICE);// 取得系统服务
Notification notification = new Notification(R.drawable.ic_launcher,
"来自XDWANG的消息。", System.currentTimeMillis()); // 立刻发送一个消息,信息图标、信息提示、显示时间
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
super.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT); // 创建了一个PendingIntent对象
notification.setLatestEventInfo(this, "王旭东",
"http://xdwangiflytek.iteye.com", contentIntent);// 信息标题、信息内容、待发送的Intent
notificationManager.notify("XDWANG", R.drawable.ic_launcher,
notification);// 设置信息标签、设置图标、发送消息
}
}
效果:
二、 SMS 服务
前面我们说过 Intent 启动手机短信发送程序,其主要功能只是显示一个发送短信的窗口,而要想发送短信,用户需要手动进行,在 Android 中专门提供了一个 android.telephony.SmsManager 类,可以进行短信发送程序的调用
说明: destinationAddress :收件人地址
scAddress :设置短信中心的号码,如果设置为 null ,则为默认中心号码
text :指定发送短信的内容
sentIntent :当消息发出时,通过 PendingIntent 来广播发送成功或者失败的信息报告,如果该参数为空,则检查所有未知的应用程序,这样将导致发生发送时间延长
deliveryIntent :当信心发送到收件处时,该 PendingIntent 会进行广播
范例:
PendingIntent02_SMSActivity.java
Java代码
package com.iflytek.demo;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.app.PendingIntent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;
public class PendingIntent02_SMSActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
// 短信内容
String content = "在昨天国防部例行记者会上,钓鱼岛问题依然是关注焦点。有记者提到,有媒体报道日本航空自卫队近半年来出动战斗机达到200余次,在日本政府宣布购岛行为之后剧增到54次,为前三个月的3.6倍,请问如何评论,在这方面中方采取了哪些措施?近年来,日本自卫队飞机针对中国的侦巡力度不断加大,损害了中国的主权权益和安全利益,也是引发中日海空安全问题的根源。国防部新闻事务局副局长、国防部新闻发言人杨宇军说,我们要求日方停止侵犯中国的主权权益,同时采取有效措施,避免和防止海空事故和不测事件的发生。";// 超过了70个字
// 短信管理类
SmsManager smsManager = SmsManager.getDefault();
// 取得PendingIntent
PendingIntent sentIntent = PendingIntent.getActivity(this, 0,
super.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
if (content.length() > 70) { // 大于70个字,拆分
List<String> msgs = smsManager.divideMessage(content); // 拆分信息
Iterator<String> iterator = msgs.iterator();// 实例化Iterator
while (iterator.hasNext()) {// 迭代输出
String msg = iterator.next();// 取出每一个子信息
smsManager.sendTextMessage("13956027313", null, msg,
sentIntent, null);// 发送文字信息
}
} else {//如果不大于70,则直接全部发送
smsManager.sendTextMessage("13956027313", null, content,
sentIntent, null);
}
Toast.makeText(this, "短信发送完成", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
sentIntent.cancel();
super.onDestroy();
}
}
Xml代码
<uses-permission android:name="android.permission.SEND_SMS" />
Intent :表示立刻执行;
PendintIntent :表示的是暂缓执行,遇到特殊条件才执行;
PendingIntent 与 Intent 没有任何继承关系,所以这两个类表示两种不同的 Intent 操作,其方法和常量有:
No. | 常量及方法 | 描述 |
1 | Public static final int FLAG_CANCEL_CURRENT | 重新生成一个新的 PendingIntent 对象 |
2 | Public static final int FLAG_NO_CREATE | 如果不存在 PendingIntent 对象,则创建一个新的 |
3 | Public static final int FLAG_ONE_SHOT | 创建的 PendingIntent 对象只使用一次 |
4 | Public static final int FLAG_UPDATE_CURRENT | 如果 PendingIntent 对象已经存在,则直接使用,并且实例化一个新的 Intent 对象 |
5 | Public static PendingIntent getActivity(Context context, Int requestCode,Intent intent,int flags) | 通过 PendingIntent 启动一个新的 Activity |
6 | Public static PendingIntent getBroadcast(Context context, Int requestCode,Intent intent,int flags) | 通过 PendingIntent 启动一个新的 Broadcast |
7 | Public static PendingIntent getService(Context context, Int requestCode,Intent intent,int flags) | 通过 PendingIntent 启动一个新的 Service |
一、发送通知: Notification
Androd.app.Notification 与 Toast 类似,可以直接在 Android 手机屏幕的最上面显示通知信息。使用 Notification 定义一条提示信息的标题、时间、内容以及具体的触发操作
No. | 方法 | 类型 | 描述 |
1 | Public Notification(nt icon,CharSequence tickerText,long when) | 构造 | 创建一个新的 Notification 对象,并指定提示的图标、信息内容及显示的时间,如果为立刻显示,则直接使用 System.currentTimeMillis() 设置 |
2 | Public void setLatestEventInfo(Context context,CharSequence contentTitle,CharSequence contentText,PendingIntent contentIntent) | 普通 | 设置通知的标题、内容以及指定的 PendingIntent |
No. | 方法 | 描述 |
1 | Public void notify(String tag,int id, Notification notification) | 指定发送信息的标签、显示图标、 Notification 对象 |
2 | Public void notify(int id, Notification notification) | 指定发送信息的显示图标、 Notification 对象 |
3 | Public void cancel(String tag,int id) | 取消指定标签、显示图标的信息 |
4 | Public void cancel(int id) | 取消指定图标的信息 |
5 | Public void cancelAll() | 取消所有信息 |
PendingIntent01_NotificationActivity.java
Java代码
package com.iflytek.demo;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.os.Bundle;
public class PendingIntent01_NotificationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
NotificationManager notificationManager = (NotificationManager) super
.getSystemService(Activity.NOTIFICATION_SERVICE);// 取得系统服务
Notification notification = new Notification(R.drawable.ic_launcher,
"来自XDWANG的消息。", System.currentTimeMillis()); // 立刻发送一个消息,信息图标、信息提示、显示时间
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
super.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT); // 创建了一个PendingIntent对象
notification.setLatestEventInfo(this, "王旭东",
"http://xdwangiflytek.iteye.com", contentIntent);// 信息标题、信息内容、待发送的Intent
notificationManager.notify("XDWANG", R.drawable.ic_launcher,
notification);// 设置信息标签、设置图标、发送消息
}
}
package com.iflytek.demo;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.os.Bundle;public class PendingIntent01_NotificationActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); NotificationManager notificationManager = (NotificationManager) super .getSystemService(Activity.NOTIFICATION_SERVICE);// 取得系统服务 Notification notification = new Notification(R.drawable.ic_launcher, "来自XDWANG的消息。", System.currentTimeMillis()); // 立刻发送一个消息,信息图标、信息提示、显示时间 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, super.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT); // 创建了一个PendingIntent对象 notification.setLatestEventInfo(this, "王旭东", "http://xdwangiflytek.iteye.com", contentIntent);// 信息标题、信息内容、待发送的Intent notificationManager.notify("XDWANG", R.drawable.ic_launcher, notification);// 设置信息标签、设置图标、发送消息 }}
效果:
二、 SMS 服务
前面我们说过 Intent 启动手机短信发送程序,其主要功能只是显示一个发送短信的窗口,而要想发送短信,用户需要手动进行,在 Android 中专门提供了一个 android.telephony.SmsManager 类,可以进行短信发送程序的调用
No. | 方法 | 描述 |
1 | Public ArrayList<String>divideMessage(String text) | 拆分短信内容 |
2 | Public static SmsManager getDefault() | 取得默认手机的 SmsManager 对象 |
3 | Public void sendTextMessage(String destinationAddress,String scAddress,String text,PendingIntent sendIntent, PendingIntent diliveryIntent) | 发送文字信息 |
4 | Public void sendMulipartTextMessage(String destinationAddress, String scAddress,ArrayList<String>parts,ArrayList<PendingIntent> sentIntents,ArrayList<PendingIntent> deliveryIntents) | 发送多条文字信息 |
5 | Public void sendDataMessage(String destinationAddress,String scAddress,short destinationPort,byte[] data,PendintIntent sendIntent,PendingIntent deliveryIntent) | 发送二进制数据信息 |
scAddress :设置短信中心的号码,如果设置为 null ,则为默认中心号码
text :指定发送短信的内容
sentIntent :当消息发出时,通过 PendingIntent 来广播发送成功或者失败的信息报告,如果该参数为空,则检查所有未知的应用程序,这样将导致发生发送时间延长
deliveryIntent :当信心发送到收件处时,该 PendingIntent 会进行广播
范例:
PendingIntent02_SMSActivity.java
Java代码
package com.iflytek.demo;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.app.PendingIntent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;
public class PendingIntent02_SMSActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
// 短信内容
String content = "在昨天国防部例行记者会上,钓鱼岛问题依然是关注焦点。有记者提到,有媒体报道日本航空自卫队近半年来出动战斗机达到200余次,在日本政府宣布购岛行为之后剧增到54次,为前三个月的3.6倍,请问如何评论,在这方面中方采取了哪些措施?近年来,日本自卫队飞机针对中国的侦巡力度不断加大,损害了中国的主权权益和安全利益,也是引发中日海空安全问题的根源。国防部新闻事务局副局长、国防部新闻发言人杨宇军说,我们要求日方停止侵犯中国的主权权益,同时采取有效措施,避免和防止海空事故和不测事件的发生。";// 超过了70个字
// 短信管理类
SmsManager smsManager = SmsManager.getDefault();
// 取得PendingIntent
PendingIntent sentIntent = PendingIntent.getActivity(this, 0,
super.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
if (content.length() > 70) { // 大于70个字,拆分
List<String> msgs = smsManager.divideMessage(content); // 拆分信息
Iterator<String> iterator = msgs.iterator();// 实例化Iterator
while (iterator.hasNext()) {// 迭代输出
String msg = iterator.next();// 取出每一个子信息
smsManager.sendTextMessage("13956027313", null, msg,
sentIntent, null);// 发送文字信息
}
} else {//如果不大于70,则直接全部发送
smsManager.sendTextMessage("13956027313", null, content,
sentIntent, null);
}
Toast.makeText(this, "短信发送完成", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
sentIntent.cancel();
super.onDestroy();
}
}
Xml代码
<uses-permission android:name="android.permission.SEND_SMS" />
相关文章推荐
- Android的广播使用
- Android_收回键盘
- WiFi信息获取简单demo
- Android Dialog底部弹窗
- .9.png图片说明及制作
- androidStudio使用反射机制
- Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
- android工程混淆时要注意的
- Android成长(一)——环境搭建
- android自定义控件(三) 增加内容 自定义属性 format详解
- android scrollView滚到底部
- android:windowSoftInputMode属性详解
- Android 系统文件认识(Qualcomm Platfrom)
- Android优秀文章链接
- android 计算listview的高度
- 【Android】 LayoutInflater和SetContentView详解
- Android TextView跑马灯一直执行
- android系统镜像裁剪定制---实战篇
- 野人学Android第二弹——自定义控件的分析与实例
- android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!