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. w3c_html_study_note_5.26

    xhtml+css 正确的说法 “DIV+CSS”叫法将网页制作者引入两大误区 [误区一]网页中用了Table,页面就不标准,甚至觉着用Table丢人,Table成为了判定页面是否标准的关键点. [误 ...

  2. Sublime text 取消记住上一次打开的,这功能太墨迹了!

    比较恨,这sublime text的配置全部都是配置文件. 选择菜单:Preferences->Settings-User,增加配置项 //热退出,其实实现一种模拟没有退出的状态,当程序再次启动 ...

  3. VisionTimer BUG && Start

    void Start() { vp_Timer.In(0.0f, delegate() { Debug.Log("Start"); }, 10, 1.0f); } Version ...

  4. POJ2406 Power Strings KMP算法

    给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的  譬 ...

  5. POJ 3641

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6044   Accepted: 24 ...

  6. 编写高性能JavaScript【转】

    英文链接:Writing Fast, Memory-Efficient JavaScript 很多JavaScript引擎,如Google的V8引擎(被Chrome和Node所用),是专门为需要快速执 ...

  7. SDUT1061Binomial Showdown(组合数)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1061 题意 : 表示这个题的英文没看懂,就看懂 ...

  8. SDUT2141数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2141&cid=1186 #include<cstdio> #include& ...

  9. MYSQL日常操作命令再熟悉

    1,创建用户及密码: CREATE USER 'user'@'%' IDENTIFIED BY 'password'; 2,创建数据库: create database PDB_chengang de ...

  10. Linux如何修改文件/文件夹内所有文件的权限

    一.修改文件权限 修改文件权限前,需要了解一下权限中的”rwx”与数字的对应关系,其中r=4,w=2,x=1. 例如:”drwxr-xr-x”,第一个”d”是代表文件夹,这里不用考虑,后面九个字符,每 ...