Android Process Communication Class As event listener

Source: Internet
Author: User

In Android, internal classes can be used to change the listener class in the current class. Because the listener class is an internal class of the external class, you can freely access all interface components of the external class. The following is an example of using the system internal class to send text messages:

SMS:

Package com. example. msmlistener;


Import android. app. Activity;

Import android. app. PendingIntent;

Import android. content. Intent;

Import android. telephony. SmsManager;

Import android. view. View;

Import android. view. View. OnLongClickListener;

Import android. widget. EditText;

Import android. widget. Toast;


Public class SMS implements OnLongClickListener {

Private Activity act;

Private EditText add;

Private EditText content;


Public SMS (Activity act, EditText addr, EditText content ){

This. act = act;

This. add = addr;

This. content = content;

}

@ Override

Public boolean onLongClick (View arg0 ){

// TODO Auto-generated method stub

String addressStr = add. getText (). toString ();

String contentStr = content. getText (). toString ();

SmsManager sm = SmsManager. getDefault ();

PendingIntent sentIntent = PendingIntent. getBroadcast (act, 0, new Intent (), 0 );

Sm. sendTextMessage (addressStr, null, contentStr, sentIntent, null );

Toast. makeText (act, "the text message has been sent! \ N ", Toast. LENGTH_LONG). show ();

Returnfalse;

}


}

Activity XML:

Package com. example. msmlistener;


Import android. app. Activity;

Import android. app. PendingIntent;

Import android. content. Intent;

Import android. telephony. SmsManager;

Import android. view. View;

Import android. view. View. OnLongClickListener;

Import android. widget. EditText;

Import android. widget. Toast;


Public class SMS implements OnLongClickListener {

Private Activity act;

Private EditText add;

Private EditText content;


Public SMS (Activity act, EditText addr, EditText content ){

This. act = act;

This. add = addr;

This. content = content;

}

@ Override

Public boolean onLongClick (View arg0 ){

// TODO Auto-generated method stub

String addressStr = add. getText (). toString ();

String contentStr = content. getText (). toString ();

SmsManager sm = SmsManager. getDefault ();

PendingIntent sentIntent = PendingIntent. getBroadcast (act, 0, new Intent (), 0 );

Sm. sendTextMessage (addressStr, null, contentStr, sentIntent, null );

Toast. makeText (act, "the text message has been sent! \ N ", Toast. LENGTH_LONG). show ();

Returnfalse;

}


}

Activity JAVA:

Package com. example. msmlistener;


Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. widget. Button;
Import android. widget. EditText;


Public class MainActivity extends Activity {

EditText address;
EditText content;


@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// Obtain page information
Address = (EditText) findViewById (R. id. address );
Content = (EditText) findViewById (R. id. content );
Button btnsend = (Button) findViewById (R. id. send );
Btnsend. setOnLongClickListener (new SMS (this, address, content ));
}


}

Note that you need to add the SMS sending permission to Manfest:

In fact, the time listener that contains the business logic will lead to the coupling of the actual logic of the program and the business logic, increasing the difficulty of program maintenance in the future.

If multiple listeners need to implement the same service logic function, you can use the business logic component to define the business logic function, and then let the event listener call the business logic method of the business logic component.

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.