Android_PendingIntent的使用
PendingIntent介绍
PendingIntent可以看作是对Intent的一个封装,但它不是立刻执行某个行为,而是满足某些条件或触发某些事件后才执行指定的行为。
PendingIntent举例
1. 发送短信
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Test1Activity extends Activity implements
OnClickListener {
Button btn1 = null;
private
SmsManager sm = null;
private
IntentFilter sendIntentFilter = null;
private
SmsBroadcastReceiver sendReceiver = null;
private
IntentFilter deliverIntentFilter = null;
private
SmsBroadcastReceiver deliverReceiver = null;
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) this.findViewById(R.id.btn1);
btn1.setOnClickListener(this);
sm = SmsManager.getDefault();
sendIntentFilter = new IntentFilter("send_sms");
sendReceiver = new SmsBroadcastReceiver();
this.registerReceiver(sendReceiver, sendIntentFilter);
deliverIntentFilter = new IntentFilter("deliver_sms");
deliverReceiver = new SmsBroadcastReceiver();
this.registerReceiver(deliverReceiver, deliverIntentFilter);
}
@Override
public void
onClick(View v) {
switch(v.getId()) {
case R.id.btn1:
send_sms();
break;
default:
break;
}
}
private void
send_sms() {
String destinationAddress = "1341024977";
String text = "宝贝";
Intent sIntent = new Intent("send_sms");
PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0,
sIntent, 0);//短信成功发送后才发送该广播
Intent dIntent = new Intent("deliver_sms");
PendingIntent deliveryIntent = PendingIntent.getBroadcast(this, 1,
dIntent, 0);//短信成功接收后才发送该广播
sm.sendTextMessage(destinationAddress, null, text, sentIntent,
deliveryIntent);
}
private
class SmsBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction() == "send_sms") {
Toast.makeText(Test1Activity.this, "send sms successfully",
Toast.LENGTH_LONG).show();
}
if(intent.getAction() == "deliver_sms") {
Toast.makeText(Test1Activity.this, "deliver sms successfully",
Toast.LENGTH_LONG).show();
}
}
}
}
2. 通知
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Test2Activity extends Activity implements
OnClickListener {
private
Button btnNotify = null;
private
NotificationManager nm = null;
private
Notification notification = null;
private
Intent intent = null;
private
PendingIntent pi = null;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
btnNotify = (Button) this.findViewById(R.id.notify);
btnNotify.setOnClickListener(this);
}
@Override
public void
onClick(View v) {
switch(v.getId()) {
case R.id.notify:
testNotify();
}
}
@SuppressWarnings("deprecation")
private void
testNotify() {
nm = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "你也是通知";
notification.defaults = Notification.DEFAULT_SOUND;
intent = new Intent(this, Test1Activity.class);
pi = PendingIntent.getActivity(this, 0, intent,
0);//用户点击该notification后才启动该activity
notification.setLatestEventInfo(this, "title22", "text33",
pi);
nm.notify(1, notification);
}
}
Android_PendingIntent的使用的更多相关文章
随机推荐
- select * from table where 1=1
转自:http://www.dzwebs.net/2418.html 我们先来看看这个语句的结果:select * from table where 1=1,其中where 1=1,由于1=1永远是成 ...
- Python环境搭建和开发工具的配置
本文转自http://237451446.blog.51cto.com/2307663/766781 因为要学习python了,第一步当然是环境搭建和开发工具的配置了,下边开始了. 我的开发环境是在w ...
- 一点关于Ajax和一个等待图标的显示
一点关于Ajax和一个等待图标的显示 1.首先Ajax是asynchronous Java-Script and XML的简写.翻译过来就是异步的JS和XML. 2它的优点就是能不更新页面的情况下,得 ...
- Careercup - Facebook面试题 - 5110993575215104
2014-04-30 16:12 题目链接 原题: The beauty of a number X is the number of 1s in the binary representation ...
- html+css学习笔记 2[标签]
img标签/a标签 <img src="图片地址" alt="图片名"/> 图片(单标签)alt属性 是图片名字,是给百度搜索引擎抓取使用: ...
- find查找指定类型文件并删除
问题描述: 查找当前目录下指定类型的文件 问题解决: (1)find命令 ...
- [设计模式] 7 桥接模式 bridge
#include<iostream> using namespace std; class AbstractionImp { public: virtual ~AbstractionImp ...
- Understanding and Using Servlet Filters
Overview of How Filters Work This section provides an overview of the following topics: How the Serv ...
- iOS开发中@selector的理解
@selector 是什么? 1一种类型 SEL2代表你要发送的消息(方法), 跟字符串有点像, 也可以互转.: NSSelectorFromString() / NSSelectorFromStri ...
- 黑马程序员-C#学习笔记(二)
---------------------- ASP.Net+Android+IOS开发..Net培训.期待与您交流! ---------------------- - C# 学习笔记 一.变量与表达 ...