Android电话拨号

package cn.jinyejun.androiddail;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity implements
		android.view.View.OnClickListener {

	private Button btnCall = null;
	private Button btnSend = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btnCall = (Button) this.findViewById(R.id.btn_dail);
		btnCall.setOnClickListener(this);
		btnSend = (Button) this.findViewById(R.id.btn_send);
		btnSend.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void callNumber() {
		EditText etNumber = (EditText) this.findViewById(R.id.et_number);
		String number = etNumber.getText().toString().trim();
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:" + number));
		startActivity(intent);
	}
	private void sendMessage(){
		EditText etNumber = (EditText) this.findViewById(R.id.et_number);
		String number = etNumber.getText().toString().trim();
		EditText etContent = (EditText)this.findViewById(R.id.et_content);
		String content = etContent.getText().toString().trim();
		SmsManager smsManager = SmsManager.getDefault();
		smsManager.sendTextMessage(number, null, content, null, null);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_dail:
			callNumber();
			break;
		case R.id.btn_send:
			sendMessage();
			break;
		default:
			break;
		}

	}

}

相关推荐