In Android, you can use an inner class to send a change listener class inside the current class, because the listener class is the inner class of the outer class, so you have free access to all the interface components of the external class. Here is an example of a call to the system's internal class for SMS sending:
SMS class:
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,"SMS send complete! \ 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,"SMS send complete! \ 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);
Get 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 You need to add the right to send SMS to Manfest:
< uses-permission android:name="Android.permission.SEND_SMS"/>
In fact, the time listener that contains the business logic will cause the actual logic of the program to be coupled with the business logic, which can be more difficult to maintain later in the program.
If you do have multiple implementation listeners that need to implement the same business logic functionality, consider using business logic components to define business logic features, and then let event listeners invoke the business logic methods of the business logic components.