android 5. callphone and sendsms

简单的实现两个模拟器的拨打电话与发送短信功能

主界面:

android 5. callphone and sendsms_第1张图片

android 5. callphone and sendsms_第2张图片

android 5. callphone and sendsms_第3张图片

 

布局文件:

简单布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:numeric="decimal"
        android:text="拨打电话" />

    <EditText
        android:id="@+id/phoneText"
        android:layout_width="250dip"
        android:layout_height="wrap_content"
        android:hint="请输入电话号码..."
        android:phoneNumber="true" />

    <Button
        android:id="@+id/callButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拨打" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="发送短信" />

    <EditText
        android:id="@+id/smsText"
        android:layout_width="250dip"
        android:layout_height="120dip"
        android:hint="请输入短信内容..." />

    <Button
        android:id="@+id/smsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送短信" />

</LinearLayout>

程序主要代码:

package smh.demo;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class IntentDemoActivity extends Activity {
	/** Called when the activity is first created. */
	private EditText phoneNumberText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button callButton = (Button) findViewById(R.id.callButton);
		Button smsButton = (Button) findViewById(R.id.smsButton);

		phoneNumberText = (EditText) findViewById(R.id.phoneText);

		// 拨打电话
		callButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String phoneNumber = phoneNumberText.getText().toString();

				if (!"".equals(phoneNumber)) {
					Intent intent = new Intent(Intent.ACTION_CALL, Uri
							.parse("tel:" + phoneNumber));
					startActivity(intent);
				} else {
					Toast.makeText(IntentDemoActivity.this, "对不起, 电话号码不能为空!",
							Toast.LENGTH_LONG).show();
				}
			}
		});

		// 发送短信
		smsButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				EditText smsNumberText = (EditText) findViewById(R.id.smsText);
				String smsMessage = smsNumberText.getText().toString();
				String phoneNumber = phoneNumberText.getText().toString();

				if (!"".equals(phoneNumber)) {
					SmsManager sms = SmsManager.getDefault();
					PendingIntent mPI = PendingIntent.getBroadcast(
							IntentDemoActivity.this, 0, new Intent(), 0);
					sms.sendTextMessage(phoneNumber, null, smsMessage, mPI,
							null);

					Toast.makeText(IntentDemoActivity.this, "发送成功",
							Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(IntentDemoActivity.this, "对不起, 电话号码不能为空!",
							Toast.LENGTH_LONG).show();
				}
			}
		});

	}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="smh.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".IntentDemoActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.SEND_SMS" />

</manifest>

 

你可能感兴趣的:(android)