(android 基础知识) PendingIntent

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);
      }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值