Android组件通信——PendingIntent(二十八)

1. PendingIntent

1.1 知识点

(1)了解PendingIntent与Intent的区别;

(2)可以完成Notification功能的开发;

(3)可以使用PendingIntent进行短信的发送;

1.2 具体内容

Intent的功能主要是完成:一个Activity跳转到另外一个Activity或者是Service,表示的是一种操作意图;表示立即执行

PendingIntent:表示暂不执行的操作意图,是一种在某一个事件之后才进行操作的Intent对象。表示暂缓执行,遇到特殊的条件才执行。

范例:发送通知:Notifaction,表示的是提示用户操作组件。

package com.example.notificationproject;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Bundle;

public class NotificationActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_notification);
		NotificationManager notificationManager = (NotificationManager) super
				.getSystemService(Context.NOTIFICATION_SERVICE);// 取得系统服务
		Notification notification = new Notification(R.drawable.logo,// 显示图标
				"来自毛栗子消息",// 信息提示
				System.currentTimeMillis());// 显示时间
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				this.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);// 取得PendingIntent对象
		notification.setLatestEventInfo(this, "maolizi", "毛栗子(www.csdn.com)", pendingIntent);//设置信息
		notificationManager.notify("maolizi",R.drawable.logo,notification);//发送消息
	}
}

 本程序就是取得通知服务,而后将通知发送出去,可以发现,当用户打开这个通知之后,就可以立刻回到指定的Intent,就是说是用户触发的时候才会进行跳转。

这次只是针对这个服务进行一些基本的讲解,以后大家去学习手机服务的时候,会有更多更好玩的东西。

public void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent) 

·String destinationAddress:表示收件人的地址

·String scAddress:设置短信的号码。如果为null的话,表示是手机中心号码

·String text:短信内容

·PendingInteng sentIntent:当信息发出之后,会通过PendingIntent来接受成功或者失败的信息报告,如果此参数为空,则会检查所有未知的应用程序,会消耗很长的事件

·PendingIntent deliveryIntent:当信息发送到收件处时,该PendingIntent触发。 

package com.example.notificationproject;

import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.PendingIntent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.widget.Toast;

public class NotificationActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_notification);
		String content = "毛栗子CSDN博客于2017年开始,现在码龄6年,已写博客90多篇,上传资源4个,阅读量快突破四万。主要撰写关于Java、Android、Linux方面的内容。";
		SmsManager smsManager = SmsManager.getDefault();//短信管理类
		PendingIntent sentIntent =  PendingIntent.getActivity(this, 0, super.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
		if(content.length()>70){
			List<String> res = smsManager.divideMessage(content);//拆分短信
			Iterator<String> it = res.iterator();
			while(it.hasNext()){
				String msg = it.next();
				smsManager.sendTextMessage("15555215554", null, msg, sentIntent, null);//发送短信
			}
		}else{
			smsManager.sendTextMessage("15555215554", null, content, sentIntent, null);//发送短信
		}
		Toast.makeText(this, "短信发送完成", Toast.LENGTH_LONG).show();
	}
}

配置权限:

<uses-permission android:name="android.permission.SEND_SMS"/>

 1.3 小结

(1)PendingIntent与Intent的最大区别在于一个将要执行,一个立即执行;

(2)在Android中的系统服务有许多的操作都是需要在某一特定环境下才会执行的Intent,这种操作就使用PendingIntent完成;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毛栗子201105

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值