Use of Android_pendingintent

Source: Internet
Author: User

Pendingintent Introduction
Pendingintent can be seen as a encapsulation of intent, but it does not execute a behavior immediately, but rather satisfies certain conditions or triggers certain events before executing the specified behavior.
pendingintent Example
1. Send SMS
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 = "Baby";

Intent sintent = new Intent ("send_sms");
Pendingintent sentintent = Pendingintent.getbroadcast (this, 0,sintent, 0);//SMS sent only after the message was sent successfully

Intent dintent = new Intent ("deliver_sms");
Pendingintent deliveryintent = Pendingintent.getbroadcast (this, 1,dintent, 0);//text message successfully received before sending the broadcast

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. Notice
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 = "You are also informed";
Notification.defaults = Notification.default_sound;

Intent = new Intent (this, test1activity.class);
PI = pendingintent.getactivity (this, 0, intent,0);//The activity is activated only after the user taps the notification

Notification.setlatesteventinfo (This, "Title22", "Text33", pi);
Nm.notify (1, notification);
}
}

Use of Android_pendingintent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.