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的使用的更多相关文章
随机推荐
- 从InputStream到String_写成函数
String result = readFromInputStream(inputStream);//调用处 //将输入流InputStream变为String public String readF ...
- html5游戏引擎-Pharse.js学习笔记(一)
1.前言 前几天随着flappy bird这样的小游戏的火爆,使我这种也曾了解过html5技术的js业余爱好者也开始关注游戏开发.研究过两个个比较成熟的html5游戏引擎,感觉用引擎还是要方便一些.所 ...
- win7旗舰版安装office2007后打开文件提示找不到proplusww.msi
今天第一次打开2007的excel,出现错误如下: 解决办法: 转自:http://blog.163.com/huacai9420@126/blog/static/521585422011911524 ...
- Web应用中的轻量级消息队列
Web应用中为什么会需要消息队列?主要原因是由于在高并发环境下,由于来不及同步处理,请求往往会发生堵塞,比如说,大量的insert,update之类的请求同时到达mysql,直接导致无数的行锁表锁,甚 ...
- [转载]Spring Bean Definition Inheritance
Following is the configuration file Beans.xml where we defined "helloWorld" bean which has ...
- CF 86D Powerful array
离线+分块 将n个数分成sqrt(n)块. 对所有询问进行排序,排序标准: 1. Q[i].left /block_size < Q[j].left / block_size (块号 ...
- MAC OS JAVA环境变量配置
在 /etc/profile 中 加上这些 #临时提权 sudo su #输入密码 vi /etc/profile #配置JAVA_HOME,此处路径根据自己的版本填写 JAVA_HOME=&quo ...
- Java并发:Callable、Future和FutureTask
Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...
- Orcle数据库编程:一
1.PL/SQL是一种块结构的语言,一个PL/SQL程序包含了一个或者多个逻辑块,逻辑块中可以声明变量,变量在使用之前必须先声明. declare mstu student%ROWTYPE;--定义参 ...
- 打败Google的灵童今在何方?
微软和雅虎宣布在搜索和广告上10年合作,这事儿不知是不是前不久虚惊一场的微软收购雅虎案的好戏重演之序幕. 从表面上看,这次的合作改变不了搜索和广告目前的世界格局,也构不成对Google的致命威胁,反倒 ...