Android学习-----Android组件通信PendingIntent

Intent 的主要功能是表示用户的一种操作意图,使用 Intent 之后将立即执行用户所有需要的操作,但是在 Android 中也提供了一个 PendingIntent 操纵,表示将要发生的操作。苏伟将要发生的 Intent 是指在当前的 Activity 不立即使用此 Intent 进行处理,而将此 Intent 封装后传递给其他 Activity 程序,而其他 Activity 程序在需要使用此 Intent 时才进行操作。

 

\

 

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

在 Android 操作系统中,狠毒地方都要使用 PendingIntent 类,如发送一些用户的通知( Notification )或者为用户发送短信( SMS )等都会使用到此类。

一、发送通知: 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

然后我们再通过 android.app.NotificationManager 类,该类就相当于一个发布 Notification 信息的组件,如果把 NotificationManager 类看作一个新闻广播,那么每个 Notification 就可以看作一条条的新闻信息。 NotificationManager 常用方法如下:

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代码  
  1. package com.iflytek.demo;   
  2.   
  3. import android.app.Activity;   
  4. import android.app.Notification;   
  5. import android.app.NotificationManager;   
  6. import android.app.PendingIntent;   
  7. import android.os.Bundle;   
  8.   
  9. public class PendingIntent01_NotificationActivity extends Activity {   
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {   
  13.         super.onCreate(savedInstanceState);   
  14.         super.setContentView(R.layout.main);   
  15.         NotificationManager notificationManager = (NotificationManager) super  
  16.                 .getSystemService(Activity.NOTIFICATION_SERVICE);// 取得系统服务   
  17.         Notification notification = new Notification(R.drawable.ic_launcher,   
  18.                 "来自XDWANG的消息。", System.currentTimeMillis()); // 立刻发送一个消息,信息图标、信息提示、显示时间   
  19.         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,   
  20.                 super.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT); // 创建了一个PendingIntent对象   
  21.         notification.setLatestEventInfo(this, "王旭东",   
  22.                 "http://xdwangiflytek.iteye.com", contentIntent);// 信息标题、信息内容、待发送的Intent   
  23.         notificationManager.notify("XDWANG", R.drawable.ic_launcher,   
  24.                 notification);// 设置信息标签、设置图标、发送消息   
  25.     }   
  26. }  
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)

发送二进制数据信息

说明: destinationAddress :收件人地址

scAddress :设置短信中心的号码,如果设置为 null ,则为默认中心号码

text :指定发送短信的内容

sentIntent :当消息发出时,通过 PendingIntent 来广播发送成功或者失败的信息报告,如果该参数为空,则检查所有未知的应用程序,这样将导致发生发送时间延长

deliveryIntent :当信心发送到收件处时,该 PendingIntent 会进行广播

范例:

PendingIntent02_SMSActivity.java

Java代码  
  1. package com.iflytek.demo;   
  2.   
  3. import java.util.Iterator;   
  4. import java.util.List;   
  5.   
  6. import android.app.Activity;   
  7. import android.app.PendingIntent;   
  8. import android.os.Bundle;   
  9. import android.telephony.SmsManager;   
  10. import android.widget.Toast;   
  11.   
  12. public class PendingIntent02_SMSActivity extends Activity {   
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {   
  16.         super.onCreate(savedInstanceState);   
  17.         super.setContentView(R.layout.main);   
  18.         // 短信内容   
  19.         String content = "在昨天国防部例行记者会上,钓鱼岛问题依然是关注焦点。有记者提到,有媒体报道日本航空自卫队近半年来出动战斗机达到200余次,在日本政府宣布购岛行为之后剧增到54次,为前三个月的3.6倍,请问如何评论,在这方面中方采取了哪些措施?近年来,日本自卫队飞机针对中国的侦巡力度不断加大,损害了中国的主权权益和安全利益,也是引发中日海空安全问题的根源。国防部新闻事务局副局长、国防部新闻发言人杨宇军说,我们要求日方停止侵犯中国的主权权益,同时采取有效措施,避免和防止海空事故和不测事件的发生。";// 超过了70个字   
  20.         // 短信管理类   
  21.         SmsManager smsManager = SmsManager.getDefault();   
  22.         // 取得PendingIntent   
  23.         PendingIntent sentIntent = PendingIntent.getActivity(this, 0,   
  24.                 super.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);   
  25.         if (content.length() > 70) { // 大于70个字,拆分   
  26.             List<String> msgs = smsManager.divideMessage(content); // 拆分信息   
  27.             Iterator<String> iterator = msgs.iterator();// 实例化Iterator   
  28.             while (iterator.hasNext()) {// 迭代输出   
  29.                 String msg = iterator.next();// 取出每一个子信息   
  30.                 smsManager.sendTextMessage("13956027313", null, msg,   
  31.                         sentIntent, null);// 发送文字信息   
  32.             }   
  33.         } else {//如果不大于70,则直接全部发送   
  34.             smsManager.sendTextMessage("13956027313", null, content,   
  35.                     sentIntent, null);   
  36.         }   
  37.         Toast.makeText(this, "短信发送完成", Toast.LENGTH_SHORT).show();   
  38.     }   
  39.   
  40.     @Override  
  41.     protected void onDestroy() {   
  42.         sentIntent.cancel();   
  43.         super.onDestroy();   
  44.            
  45.     }   
  46. }  
Xml代码  
  1. <uses-permission android:name="android.permission.SEND_SMS" />  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android输入手机号发送短信示例,EditText number框中的是电话号码,EditText body框中的是短信内容:   public void onCreate(Bundle savedInstanceState) {//重写的onCreate方法    super.onCreate(savedInstanceState);    setContentView(R.layout.main);//设置当前的用户界面    send = (Button) this.findViewById(R.id.send);//得到发送按钮的引用    number = (EditText) this.findViewById(R.id.number);//得到电话号码文本框的引用    body = (EditText) this.findViewById(R.id.body);//得到短信内容文本框的引用    send.setOnClickListener(this);//添加监听   IntentFilter myIntentFilter = new IntentFilter("SMS_SEND_ACTION");//创建过滤器   MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();//创建BroadcastReceiver   registerReceiver(myBroadcastReceiver, myIntentFilter);//注册BroadcastReceiver    }   @Override   public void onClick(View v) {//监听方法   if(v == send){//按下发送按钮    send.setEnabled(false);//设置按钮为不可用    String strNumber = number.getText().toString();//得到电话号码    String strBody = body.getText().toString();//得到需要发送的信息    SmsManager smsManager = SmsManager.getDefault();//得到SmsManager    Intent intentSend = new Intent("SMS_SEND_ACTION");//创建Intent    PendingIntent sendPI = PendingIntent.getBroadcast(getApplicationContext(), 0, intentSend, 0);    smsManager.sendTextMessage(strNumber, null, strBody, sendPI, null);//发送短信    send.setEnabled(true);//设置按钮为可用   }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值