Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android進程間通信之內部類作為事件監聽器

Android進程間通信之內部類作為事件監聽器

編輯:關於Android編程

在Android中,使用內部類可以在當前類裡面發用改監聽器類,因為監聽器類是外部類的內部類,所以可以自由訪問外部類的所有界面組件。以下是一個調用系統內部類實現短信發送的一個例子:

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, "短信發送完成!\n", Toast.LENGTH_LONG).show();

return false;

}


}

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, "短信發送完成!\n", Toast.LENGTH_LONG).show();

return false;

}


}

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);
//獲取頁面信息
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));
}


}

注意需要添加發送短信的權限到Manfest中:

實際上,包含業務邏輯的時間監聽器將導致程序的現實邏輯和業務邏輯耦合,從玩兒增加程序後期的維護難度。

如果確實有多個實現監聽器需要實現相同的業務邏輯功能,可以考慮使用業務邏輯組件來定義業務邏輯功能,再讓事件監聽器來調用業務邏輯組件的業務邏輯方法。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved