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

Android_PendingIntent的使用的更多相关文章

随机推荐

  1. javascript之流程控制 和函数的容易忽略点

    1.流程控制 1> for in  仅用于 对象的遍历: var box={ "name":'小红', 'age':18, 'height':165 }; for(var b ...

  2. 瞧一瞧,看一看呐,用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!!

    瞧一瞧,看一看呐用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!! 现在要写的呢就是,用MVC和EF弄出一个CRUD四个页面和一个列表页面的一个快速DEMO,当然是在不 ...

  3. FZU 2016 summer train I. Approximating a Constant Range 单调队列

    题目链接: 题目 I. Approximating a Constant Range time limit per test:2 seconds memory limit per test:256 m ...

  4. 【CodeForces】【#286】Div.2

    T_T越来越水了,这次只做出A+B. A题为了代码简单直接枚举(插入位置和插入字符) //CF #286 Div.2 A #include<vector> #include<stri ...

  5. centos6.5安装vmware-tools

    一.问题描述 为什么使用vmware-tools:传文件方便,可以从主机中直接拖拽文件到centos中(但实际上我们可以用:WinSCP):鼠标切换方便,鼠标指到什么地方,表名当前正在操作哪个环境(但 ...

  6. zend studio 10 字体,颜色,快捷键等相关设置

    一.修改字体 没想到zend studio 10中对中文显示不太好看,似乎有点小了.修改如下:打开 Window->Preferences->General->Appearance- ...

  7. xargs 加 gm批量转换图片

    %x50% @ ../-\ 南澳西涌_50%/@ 看了很多说明上都在用-i,这是一个已经废弃了的参数

  8. 翻译:用Javascript的Function构造器伪造上下文 by Ben Nadel

    在我的jQuery模板标记语言(JTML)项目中,我需要一种方式将JTML模板编译到JS函数,这样它们就可以如期地在任何时候转换成新的HTML标记.但这是一个严峻的问题,因为JTML代码涉及非作用域( ...

  9. React vs Angular 2: 冰与火之歌

    黄玄 · 3 个月前 本文译自 Angular 2 versus React: There Will Be Blood ,其实之前有人翻译过,但是翻得水平有一点不忍直视,我们不希望浪费这篇好文章. 本 ...

  10. .net中的认证(authentication)与授权(authorization)

    “认证”与“授权”是几乎所有系统中都会涉及的概念,通俗点讲: 1.认证(authentication) 就是 "判断用户有没有登录?",好比windows系统,没登录就无法使用(不 ...