Android interprocess communication within class as event listener

Source: Internet
Author: User

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.

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.