android 5. callphone and sendsms

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

主界面:

 

布局文件:

简单布局文件:

<?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>

 

转载于:https://my.oschina.net/myter7/blog/42750

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值