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的使用的更多相关文章
随机推荐
- apache-tomcat-9安装以及与eclipse结合
apache-tomcat-下载:http://tomcat.apache.org/download-90.cgi 安装(转载):http://jingyan.baidu.com/article/60 ...
- WPF 多线程处理(4)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 开始一个线程处理读取的文件并且更新到list ...
- windows下编译ffmpeg
windows 编译ffmpeg 搞过很多次,每次总是磕磕碰碰,从头到尾不能一直顺利,浪费一些时间.终究起原因,都是当时记得,过段时间就忘了.好记性不如烂笔头,大好周末晚上,闲暇无事,记录最近一次编译 ...
- 设置DIV隐藏与显示,表格滑动条
问题描述: 现在希望使用JS设置DIV块的显示与隐藏,当某一个事件触发是,自动显示DIV块,显示表格数据,但是要求表格显示滑动条 问题解决: (1)DIV块的隐藏与显示 如上所示, ...
- [CFgym]2015-2016 ACM-ICPC Pacific Northwest Regional Contest小结
*感谢两位浙江大佬带我飞 贴下成绩 div2 div1 *div2不是我打的上个厕所就5/11了 比赛小结 A [题目大意] 有n(n<=500)个机场,两两之间距离是g[i][j],每经停一个 ...
- sv_target_output dx11
http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx
- windows service 安装和卸载指令
添加服务: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319InstallUtil.exe D:\OneKeyWebSiteDeployment\Ser ...
- Javascript实现两张图片的延迟加载
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- java基础知识回顾之---java String final类 容易混淆的java String常量池内存分析
/** * 栈(Stack) :存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存放在堆(new 出来的对象)或者常量池中(字符串常量对象存放 在常量池中). 堆(heap):存 ...
- iOS开发中@selector的理解
@selector 是什么? 1一种类型 SEL2代表你要发送的消息(方法), 跟字符串有点像, 也可以互转.: NSSelectorFromString() / NSSelectorFromStri ...