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. getHeight returns 0 for all Android UI objects

    It's 0 because in both onCreate and onStart, the view hasn't actually been drawn yet. You can get ar ...

  2. android中设置Animation 动画效果

    在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...

  3. SQL Server数据库事务日志序列号(LSN)介绍

    原文:http://blog.csdn.net/tjvictor/article/details/5251463     日志序列编号(LSN)是事务日志里面每条记录的编号. 当你执行一次备份时,一些 ...

  4. STM32的SPI问题。

    问题描述: 之前一直使用的单片机是LPC2109,对其SPI很熟悉.基本就是原本拿来稍作修改就用.由于某种原因需要使用STM32,然后设备的驱动是之前写好的,只修改了一些硬件控制端口,由于硬件驱动使用 ...

  5. oracle 删除表、数据

        truncate ddl语句,删除表中数据,速度要比delete快:且所有自增字段重新开始计数:删除数据保留表结构:删除的数据不进入rollback segment,无法恢复.例: trunc ...

  6. 腾讯QQ企业邮箱在ruby on rails 框架中的mailer配置

    在编写ruby on rails程序时,我们可能会需要用到发送邮件的程序,如果使用gmail进行smtp发送一般问题不大,但很多企业使用的是腾讯QQ企业邮箱.使用该邮箱进行链接时出现各种错误,goog ...

  7. WinForm 资源文件的使用

    1. 创建资源文件: 2.双击资源文件,打开如下图:添加一个字符串: 名称为cnnstr 值为-- 3.添加文本文件和图像 4. 调用代码 MessageBox.Show(Resource1.cnns ...

  8. 深入浅出ES6(四):模板字符串

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 反撇号(`)基础知识 ES6引入了一种新型的字符串字面量语法,我们称之为模板字符 ...

  9. java基础知识回顾之---java String final类之intern方法

    public class StringObjectDemo { /** * @param args */ public static void main(String[] args) { String ...

  10. REST_FRAMEWORK加深记忆-三种CLASS VIEW的进化史

    一层一层的封装,又能到底层,就会有全局感啦... from rest_framework import status from rest_framework.response import Respo ...