Android学习笔记(四)-简单例子:电话拨号器与短信发送器

一、创建项目,设置好相应属性后,点击Finish完成创建,如下图所示:


二、设置界面布局,界面如下:


通过修改项目res/layout/main.xml文件可以修改界面,一共需要三个控件,一个文本、一个文本输入框、一个按钮,按照垂直排列

界面中得文本不要直接写在main.xml文件里,最好是写在values下的xml文件里,可以自己新建一个xml文件,也可以写在strings.xml文件中,注意资源的name不能重复,这样做有两个好处:

1、便于国际化

2、节省内存,如果一段文本在很多地方都用到,则不用再每个地方都输入文本内容,只需要使用引用就可以了

string.xml数据文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">电话拨号器</string>
    <string name="mobile">请输入手机号</string>
    <string name="button">拨打此号码</string>
</resources>

main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/mobile"
    />
<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/mobile"
    />
<Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/button"
    android:id="@+id/button"
    />
</LinearLayout>
这里面android:id="@+id/mobile"中@+id/mobile表示在R.java文件中会新建一个名为id的内部类,该类中会有一个属性叫mobile

public static final class id {
        public static final int button=0x7f050001;
        public static final int mobile=0x7f050000;
    }


为了是应用程序有拨打电话的权限,还需要修改AndroidManifest.xml文件,其中红色部分为新增的

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.itcast.phone"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  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-sdk android:minSdkVersion="8" />
	<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest> 

最后是写MainActivity文件

package cn.itcast.phone;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //找到按钮控件
        Button button = (Button)this.findViewById(R.id.button);
        //为按钮添加点击事件
        button.setOnClickListener(new ButtonListener());
    }
    
    private final class ButtonListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			//获取用户输入的电话号码
			EditText mobileText = (EditText)findViewById(R.id.mobile);
			String mobile = mobileText.getText().toString();
			//拨打电话
			//参考android-sdk-windows\platforms\android-8\sources\phone下AndroidManifest.xml
			//文件中<activity android:name="OutgoingCallBroadcaster"开头的那一段
			Intent intent = new Intent();
			intent.setAction("android.intent.action.CALL");
			intent.setData(Uri.parse("tel:"+mobile));
			startActivity(intent);
		}
    	
    }
}

MainActivity类还有两个可以改进的地方:

一是android.intent.action.CALL可以用一个常量Intent.ACTION_CALL代替

二是将内部类改为匿名内部类,更简洁

package cn.itcast.phone;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //找到按钮控件
        Button button = (Button)this.findViewById(R.id.button);
        //为按钮添加点击事件
        button.setOnClickListener(new View.OnClickListener() {
			
        	@Override
    		public void onClick(View v) {
    			//获取用户输入的电话号码
    			EditText mobileText = (EditText)findViewById(R.id.mobile);
    			String mobile = mobileText.getText().toString();
    			//拨打电话
    			//参考android-sdk-windows\platforms\android-8\sources\phone下AndroidManifest.xml
    			//文件中<activity android:name="OutgoingCallBroadcaster"开头的那一段
    			//android.intent.action.CALL可以用一个常量Intent.ACTION_CALL代替
    			Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+mobile));
    			startActivity(intent);//内部会添加android.intent.category.DEFAULT
    		}
		});
    }
    
}


其中ButtonListener中拨打电话部分可以参考android-sdk-windows\platforms\android-8\sources\phone下AndroidManifest.xml中的这一段

<activity android:name="OutgoingCallBroadcaster"
                android:permission="android.permission.CALL_PHONE"
                android:theme="@android:style/Theme.NoDisplay"
                android:configChanges="orientation|keyboardHidden">
            <!-- CALL action intent filters, for the various ways
                 of initiating an outgoing call. -->
            <intent-filter>
                <action android:name="android.intent.action.CALL" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="tel" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.CALL" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="voicemail" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.CALL" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.item/phone" />
                <data android:mimeType="vnd.android.cursor.item/phone_v2" />
                <data android:mimeType="vnd.android.cursor.item/person" />
            </intent-filter>
        </activity>
程序中只需要设置action和data,catagory会自动添加

开启两个模拟器,将程序运行在其中一个模拟器上,在电话号码输入框中输入另外一个模拟器的端口号(在模拟器左上角),点击拨打此号码按钮


短信发送器

首先创建项目,创建过程同上,这里省略,创建完成后如下图所示:


界面:



然后是设计界面

layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/mobile"
    />
<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/mobile"
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/content"
    />
<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:minLines="3"
    android:id="@+id/content"
    />    
<Button  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button"
    android:id="@+id/button"
    />   
</LinearLayout>

数据文件

values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, SMSActivity!</string>
    <string name="app_name">短信发送器</string>
    <string name="mobile">请输入手机号</string>
    <string name="content">请输入短信内容</string>
    <string name="button">发送短信</string>
    <string name="success">发送短信成功</string>
</resources>

SMSActivity

package cn.itcast.sms;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMSActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button = (Button)this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//获取手机号和内容
				EditText mobileText = (EditText)findViewById(R.id.mobile);
				EditText contentText = (EditText)findViewById(R.id.content);
				String mobile = mobileText.getText().toString();
				String content = contentText.getText().toString();
				
				//发送短信
				SmsManager smsManager = SmsManager.getDefault();
				List<String> texts = smsManager.divideMessage(content);//如果超过70个汉字,自动拆分短信
				for(String text : texts){
					smsManager.sendTextMessage(mobile, null, text, null, null);
				}
				
				//使用吐司通知
				Toast.makeText(SMSActivity.this, R.string.success, Toast.LENGTH_LONG).show();
			}
		});
    }
}

为了是程序能够发送短信,还需要添加权限

在AndroidManifest.xml中添加<uses-permission android:name="android.permission.SEND_SMS"/>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.itcast.sms"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SMSActivity"
                  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-sdk android:minSdkVersion="8" />
	<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest> 

程序完成,启动两台模拟器,运行界面如下:



在模拟器上运行时,发送中文会出现乱码,在真实手机上面运行时不会出现这种情况,有兴趣的可以安装到自己手机上面试试~


短信发送器界面还可以采用另一种布局方式:RelativeLayout,效果如下:


修改main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
     	android:orientation="horizontal"
     	android:layout_width="fill_parent"
     	android:layout_height="wrap_content"
     	android:id="@+id/lineLayout"
     >    
		<TextView  
		    android:layout_width="100dip" 
		    android:layout_height="wrap_content" 
		    android:text="@string/mobile"
		    android:textSize="16sp"
		    />
	    <EditText
		    android:layout_width="fill_parent" 
		    android:layout_height="wrap_content" 
		    android:phoneNumber="true"
		    android:id="@+id/mobile"
		    />
	 </LinearLayout>
	 
	<TextView  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="@string/content"
	    android:layout_below="@id/lineLayout"
	    android:id="@+id/contentLabel"
	    android:textSize="16sp"
	    />
    <EditText
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:minLines="3"
	    android:layout_below="@id/contentLabel"
	    android:id="@+id/content"
	    />
	<Button
		android:layout_width="wrap_content" 
	    android:layout_height="wrap_content"
	    android:text="@string/button"
	    android:layout_below="@id/content"
	    android:layout_alignParentRight="true"
	    android:id="@+id/button"
	    />
</RelativeLayout>


  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值