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的使用的更多相关文章
随机推荐
- oracle 自定义函数
函数和存储过程类似,可以简单的理解为一段可以执行某个活动/动作的子程序,可以作为一个系统对象被存储在数据库中,可以重复调用.与存储过程不同的是,函数总是向调用者返回一个值,而存储过程不能有返回值. C ...
- JAVASCRIPT、ANDROID、C#分别实现普通日期转换多少小时前、多少分钟前、多少秒
貌似最近很流行这个,就写了个js函数实现之 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ...
- mysql merge
merge 是一组 myisam 表的组合, 锁住一个 merge 表它会吧底下所有的表全给锁住. 创建只读表 )) engine = merge union (t1,t2); 创建可插入的表, (以 ...
- QT for android 比较完美解决 全屏问题
项目用到QT qml,需要在android下面全屏显示,折腾了一晚上,搞定,分享下,希望能帮助他人. 参考 Qt on Android:让 Qt Widgets 和 Qt Quick 应用全屏显示 该 ...
- Matlab与微积分计算
一.极限问题的解析解 1.1 单变量函数的极限 格式1: L= limit( fun, x, x0) 格式2: L= limit( fun, x, x0, ‘left’ 或 ‘right’) > ...
- httpsClient抓取证书
在执行webservice的过程中,出现如下异常: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExcep ...
- 如何恢复SQL Server 中的Master库
如何恢复SQL Server 2005中的Master库 2011-05-10 16:34 Vegas Lee 博客园 我要评论(0) 字号:T | T master库对于SQLServer来说, ...
- node-firefox 二三事
编者按:本文作者为 Soledad Penadés, Sole 在 Mozilla 的 Tech Evangelism 团队工作,帮助人们在网络上创造神奇的东西.本文主要介绍node-firefox的 ...
- POJ2104 K-th Number Range Tree
又是区间第k大,这次选择这道题是为以后写线段树套平衡树铺路的.Range Tree可以理解成线段树套vector吧,相当于每个结点多存了对应区间的一个排好序的序列.画一下就会知道空间的消耗是nlogn ...
- zoj 2974 Just Pour the Water (矩阵快速幂,简单)
题目 对于案例的解释请见下图: 这道要变动提取一下矩阵,之后就简单了 具体解释可看代码: #include <string.h> #include <stdio.h> #inc ...