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的使用的更多相关文章
随机推荐
- 使用OPTIMIZE TABLE命令来整理表碎片实践
操作环境:ubuntu 14.10 mysql 5.6.25 对含有BLOB或TEXT字段的表,若经常做修改或删除类的操作,需要定期执行OPTIMIZE TABLE命令来整理碎片. 1.creat ...
- Ubuntu 下为 Idea 创建启动图标.
默认情况下,ubuntu将自动安装的软件快捷方式保存在/usr/share/applications目录下,如果我们要创建桌面快捷方式,需要在该目录下创建一个名为“Idea.desktop”的文件.通 ...
- Extjs4 使用store的post方法
Extjs4 使用store的post方法 引用官网的一句话 Now when we call store.load(), the AjaxProxy springs into action, mak ...
- CSS3属性box-shadow使用教程,css3box-shadow
CSS3的box-shadow属性可以让我们轻松实现图层阴影效果.我们来实战详解一下这个属性. 1. box-shadow属性的浏览器兼容性先来看一个这个属性的浏览器兼容性: Opera: 不知道是从 ...
- 剑指offer--面试题19
题目:求二叉树镜像 根据作者思路,自己所写代码如下: void BinaryTreeMirror(BinaryTreeNode* pRoot) { if(pRoot == NULL) return; ...
- ios开发之网络访问的数据类型
1> JSON 特点:1. [ ] 表示数组 {} 表示字典 - 对象模型建立关系 2. 应用非常多,基本上移动开发的主要数据传输都是JSON 要使用JSON,从网络上获取到数据data后,直 ...
- 【hadoop2.6.0】用C++ 编写mapreduce
hadoop通过hadoop streaming 来实现用非Java语言写的mapreduce代码. 对于一个一点Java都不会的我来说,这真是个天大的好消息. 官网上hadoop streaming ...
- **【ci框架】精通CodeIgniter框架
http://blog.csdn.net/yanhui_wei/article/details/25803945 一.大纲 1.codeigniter框架的授课内容安排 2.codeigniter框架 ...
- hdu2011
http://acm.hdu.edu.cn/showproblem.php?pid=2011 #include<iostream> #include<math.h> #incl ...
- 欧拉工程第58题:Spiral primes
题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; impo ...