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. How to Implement Bluetooth Low Energy (BLE) in Ice Cream Sandwich

    ShareThis - By Vikas Verma Bluetooth low energy (BLE) is a feature of Bluetooth 4.0 wireless radio t ...

  2. [algothrim]URL相似度计算的思考

    http://www.spongeliu.com/399.html http://in.sdo.com/?p=865

  3. 1070: [SCOI2007]修车 - BZOJ

    Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使 ...

  4. angular入门系列教程3

    主题: 本篇主要目的就是继续完善home页,增加tab导航的三个页index index1 index2 效果图: 细节: 初始化的JS就是咱们的home.js,仔细来看. angular的route ...

  5. 垃圾回收 GC

    垃圾回收器的回收的对象: 垃圾回收只回收托管堆中的内存   什么样的对象才会被回收? 没有变量引用的对象.没有变量引用的对象,表示可以被回收了(null.   什么时间回收? 不确定,当程序需要新内存 ...

  6. 首次push本地代码到github上出现的问题及解决方案

    刚创建的github版本库,在push代码时出错: $ git push -u origin masterTo git@github.com:******/Demo.git ! [rejected] ...

  7. Eclipse和intellij idea 快捷键对比

    Eclipse和intellij idea 快捷键对比

  8. tomcat 解析(一)-文件解析

    做web项目,最常用的服务器就是Apache的tomcat.虽然一直在用tomcat,但都是仅限在使用的阶段,一直没有深入学习过.想深入学习tomcat,首推的肯定是官网:http://tomcat. ...

  9. json 基础

    json格式 JSON格式:http://www.json.org/ python和JSON的关系请参考:http://docs.python.org/library/json.html JSON建构 ...

  10. Chp17: Moderate

    17.1 swap a number in place.(without temporary variables) a = a ^ b; b = a ^ b; a = a ^ b; 17.3 Writ ...