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.