Android Development notes-pendingintent

Source: Internet
Author: User

Package com. Mobile. Main;

Import Android. App. activity;

Import Android. App. alertdialog;

Import Android. App. pendingintent;

Import Android. content. broadcastreceiver;

Import Android. content. context;

Import Android. content. dialoginterface;

Import Android. content. intent;

Import Android. content. intentfilter;

Import Android. database. cursor;

Import android.net. Uri;

Import Android. OS. Bundle;

Import Android. provider. Contacts. People;

Import Android. telephony. GSM. smsmanager;

Import Android. View. layoutinflater;

Import Android. View. view;

Import Android. View. View. onclicklistener;

Import Android. widget. Button; import Android. widget. edittext;

Import Android. widget. imagebutton;

Import Android. widget. Toast;

Public class ex12_3_2 extends Activity

{

/** Button control */private button system_sms_button;

Private button customize_sms_button;

@ Override

Public void oncreate (bundle savedinstancestate)

{

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

System_sms_button = (button) This. findviewbyid (R. Id. main_button1 );

System_sms_button.setonclicklistener (New onclicklistener ()

{Public void onclick (view v) {sendsms ("123456789", "system SMS function ");}});

Customize_sms_button = (button) This. findviewbyid (R. Id. main_button2 );

Customize_sms_button.setonclicklistener (New onclicklistener () {public void onclick (view v) {sendsms ("","");}});}

/*** Call the system to send a text message

* @ Param address

* @ Param body content */

Private void sendsms (string address, string body)

{Try {intent = new intent (intent. action_view );

Intent. putextra ("Address", address );

Intent. putextra ("sms_body", body );

Intent. settype ("Vnd. Android-DIR/MMS-SMS ");

Startactivity (intent);} catch (exception e) {e. printstacktrace ();}}

/** Dialog box instance */

Private alertdialog sendsmsdialog;

/** Custom Action constant, used as the intent filter for broadcast recognition constant */

Private string sms_send_actioin = "sms_send_actioin ";

Private string sms_delivered_action = "sms_delivered_action ";

/** Create two Broadcast objects */private mybroadcastreceiver mybroadcastreceiver1;

Private mybroadcastreceiver mybroadcastreceiver2;

/** Select the status code returned by the contact */

Private Final int backcode = 1;

/*** Recommended friend */

Private void sendsms (final string content1, final string content2)

{Layoutinflater factory = layoutinflater. From (this );

Final view textentryview = factory. Inflate (R. layout. showdialog_smsentry_layout, null );

Final edittext edittext_phone = (edittext) textentryview. findviewbyid (R. Id. showdialog_smsentry_edittext_phone );

Edittext_phone.settext (content1 );

Final edittext edittext_content = (edittext) textentryview. findviewbyid (R. Id. showdialog_smsentry_edittext_content );

Edittext_content.settext (content2 );

Final imagebutton = (imagebutton) textentryview. findviewbyid (R. id. showdialog_smsentry_imagebutton); imagebutton. setonclicklistener (New onclicklistener () {public void onclick (view v) {// jump to the system contact list URI uri = Uri. parse ("content: // contacts/People"); intent = new intent (intent. action_pick, Uri );

Startactivityforresult (intent, backcode );

Sendsmsdialog. Dismiss ();}});

Alertdialog. Builder = new alertdialog. Builder (this );

Builder. seticon (Android. R. drawable. ic_dialog_info );

Builder. settitle (getstring (R. String. Title ));

Builder. setview (textentryview );

Builder. setpositivebutton (getstring (R. string. certain), new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {string number = edittext_phone.gettext (). tostring ();

String content = edittext_content.gettext (). tostring ();

If (number = NULL | number. Length () = 0)

{

// Determine whether the phone number is empty. Toast. maketext (ex12_3_2.this, "the phone number cannot be blank! ", Toast. length_long). Show ();

Sendsms (number, content );}

Else {

// The Custom intentfilter is sent_sms_actioin receiver intentfilter mfilter01;

Mfilter01 = new intentfilter (sms_send_actioin );

Mybroadcastreceiver1 = new mybroadcastreceiver ();

// Enable the broadcast registerreceiver (mybroadcastreceiver1, mfilter01 );

// Custom

Intentfilter is delivered_sms_action receiver mfilter01 = new intentfilter (sms_delivered_action );

Mybroadcastreceiver2 = new mybroadcastreceiver ();

// Enable the broadcast registerreceiver (mybroadcastreceiver2, mfilter01 );

Try {// create smsmanager object smsmanager = smsmanager. getdefault ();

// Create an intent for the custom action constant (for the pendingintent parameter)

Intent itsend = new intent (sms_send_actioin );

Intent itdeliver = new intent (sms_delivered_action); // The sentintent parameter is the broadcast information received after transmission.

Pendingintent sendpi = pendingintent. getbroadcast (getapplicationcontext (), 0, itsend, 0 );

// The deliveryintent parameter is the broadcast information received after delivery.

Pendingintent deliverpi = pendingintent. getbroadcast (getapplicationcontext (), 0, itdeliver, 0 );

// Send SMS messages. Pay attention to the two pendingintent parameters.

Smsmanager. sendtextmessage (number, null, content, sendpi, deliverpi );

} Catch (exception e) {e. printstacktrace ();}}}});

Builder. setnegativebutton (getstring (R. String. Cancel), new dialoginterface. onclicklistener ()

{Public void onclick (dialoginterface dialog, int whichbutton ){}});

Sendsmsdialog = builder. Create (); sendsmsdialog. Show (); sendsmsdialog. setcanceledontouchoutside (true );

}

/*** Listen to the SMS status */

Public class mybroadcastreceiver extends broadcastreceiver

{@ Override public void onreceive (context, intent)

{Try {string action = intent. getaction ();

If (action. Equals (sms_send_actioin) {} else if (action. Equals (sms_delivered_action )){}

If (getresultcode () = activity. result_ OK) {toast. maketext (ex12_3_2.this, "sent successfully! ", Toast. length_long). Show ();

} Else {toast. maketext (ex12_3_2.this, "failed to send! ", Toast. length_long). Show ();}

// Cancel Broadcast

Unregisterreceiver (mybroadcastreceiver1 );

Unregisterreceiver (mybroadcastreceiver2 );}

Catch (exception e) {e. getstacktrace ();}}}

/*** Receive the data returned by the contact */

Protected void onactivityresult (INT requestcode, int resultcode, intent data ){

Switch (requestcode)

{

Case backcode: If (Data! = NULL)

{Final URI uriret = data. getdata ();

If (uriret! = NULL ){

Try {cursor c = managedquery (uriret, null );

C. movetofirst ();

/* Capture the address book name */

String strname = C. getstring (C. getcolumnindexorthrow (people. Name ));

/* Capture the phone number of the address book */

String strphone = C. getstring (C. getcolumnindexorthrow (people. Number ));

If (strname! = NULL) {toast. maketext (ex12_3_2.this, strname, Toast. length_long). Show ();}

Else {toast. maketext (ex12_3_2.this, "the contact is blank! ", Toast. length_long). Show ();}

Sendsms (strphone, "");} catch (exception e) {e. printstacktrace (); sendsms ("","");

Toast. maketext (ex12_3_2.this, "error! ", Toast. length_long). Show ();

}}} Else {sendsms ("","");

Toast. maketext (ex12_3_2.this, "the contact is empty! ", Toast. length_long). Show ();} break;

}

Super. onactivityresult (requestcode, resultcode, data );}}

Related Article

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.