Android_PendingIntent的使用

來源:互聯網
上載者:User

標籤:des   android   blog   io   ar   os   使用   sp   strong   

   

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 implementsOnClickListener {
 

   privateButton btn1 = null;
    privateSmsManager sm = null;
    privateIntentFilter sendIntentFilter = null;
    privateSmsBroadcastReceiver sendReceiver = null;
    privateIntentFilter deliverIntentFilter = null;
    privateSmsBroadcastReceiver deliverReceiver = null;
   
   @Override
    public voidonCreate(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 voidonClick(View v) {
       switch(v.getId()) {
       case R.id.btn1:
           send_sms();
           break;
       default:
           break;
       }
    }
    private voidsend_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);
    }
    privateclass 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 implementsOnClickListener {
    privateButton btnNotify = null;
    privateNotificationManager nm = null;
    privateNotification notification = null;
    privateIntent intent = null;
    privatePendingIntent pi = null;
   @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.test2);
       
       btnNotify = (Button) this.findViewById(R.id.notify);
       btnNotify.setOnClickListener(this);
    }
   @Override
    public voidonClick(View v) {
       switch(v.getId()) {
       case R.id.notify:
           testNotify();
       }
    }
   @SuppressWarnings("deprecation")
    private voidtestNotify() {
       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的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.