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 {

   private
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);
    }
}

最新文章

  1. docker tomcat7 dubbo-admin monitor
  2. jfinal和httl结合
  3. Linux操作系统奥秘02-系统引导(GRUB)
  4. Haproxy安装与配置
  5. Android使用自带JSONObject解析JSON数据
  6. 基于VMware的eCos环境编译redboot(脚本配置redboot)
  7. 判断指定进程是否为x64的方法(在ntdll判断某个x64函数是否存在)
  8. 【分享】改变未来的九大算法[pdf][清晰扫描版]
  9. 好久没发贴了,最近捣鼓了个基于node的图片压缩小网站解析。
  10. 使用FormatMessage函数编写一个内核错误码查看器
  11. Struts 1 之<bean>标签库
  12. 利用XShell上传、下载文件(使用sz与rz命令),超实用!
  13. [Linux]安装pyenv
  14. 3.GUI Skin和自定义风格的组件 --《UNITY 3D 游戏开发》笔记
  15. 《Go语言网络编程》第一章:体系
  16. 解决默写浏览器中点击input输入框时,placeholder的值不消失的方法
  17. Calendar 日历类的时间操作
  18. Java读取文件加锁代码Demo(利用Java的NIO)
  19. IO流-递归删除带内容的目录
  20. tomcat在debug模式启动直接提示:弹框无法启动,无报错信息;但直接启动的话,就会有报错信息

热门文章

  1. svn:Repository UUID 'XXX' doesn't match expected UUID 'YYY'
  2. NOI2014 全国互测Round2
  3. shell find and rm
  4. K短路
  5. 01-05-01-1【Nhibernate (版本3.3.1.4000) 出入江湖】延迟加载及其class和集合(set、bag等)的Lazy属性配置组合对Get和Load方法的影响
  6. WPF Loader
  7. js刷新
  8. zabbix3.0 安装方法,一键实现短信、电话、微信、APP 告警
  9. 解决在windows的eclipse上面运行WordCount程序出现的一系列问题详解
  10. 【Linux高频命令专题(14)】nl