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

 

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


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


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

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

1、便于国际化

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

string.xml数据文件

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

main.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/mobile"  
  11.     />  
  12. <EditText    
  13.     android:layout_width="fill_parent"   
  14.     android:layout_height="wrap_content"   
  15.     android:id="@+id/mobile"  
  16.     />  
  17. <Button    
  18.     android:layout_width="fill_parent"   
  19.     android:layout_height="wrap_content"   
  20.     android:text="@string/button"  
  21.     android:id="@+id/button"  
  22.     />  
  23. </LinearLayout>  
这里面android:id="@+id/mobile"中@+id/mobile表示在R.java文件中会新建一个名为id的内部类,该类中会有一个属性叫mobile

  1. public static final class id {  
  2.         public static final int button=0x7f050001;  
  3.         public static final int mobile=0x7f050000;  
  4.     }  


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

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="cn.itcast.phone"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".MainActivity"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.   
  15.     </application>  
  16.     <uses-sdk android:minSdkVersion="8" />  
  17. <span style="white-space:pre">    </span><uses-permission android:name="android.permission.CALL_PHONE"/>  
  18. </manifest>   

最后是写MainActivity文件

  1. package cn.itcast.phone;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.net.Uri;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.   
  11. public class MainActivity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.           
  18.         //找到按钮控件   
  19.         Button button = (Button)this.findViewById(R.id.button);  
  20.         //为按钮添加点击事件   
  21.         button.setOnClickListener(new ButtonListener());  
  22.     }  
  23.       
  24.     private final class ButtonListener implements View.OnClickListener {  
  25.   
  26.         @Override  
  27.         public void onClick(View v) {  
  28.             //获取用户输入的电话号码   
  29.             EditText mobileText = (EditText)findViewById(R.id.mobile);  
  30.             String mobile = mobileText.getText().toString();  
  31.             //拨打电话   
  32.             //参考android-sdk-windows\platforms\android-8\sources\phone下AndroidManifest.xml   
  33.             //文件中<activity android:name="OutgoingCallBroadcaster"开头的那一段   
  34.             Intent intent = new Intent();  
  35.             intent.setAction("android.intent.action.CALL");  
  36.             intent.setData(Uri.parse("tel:"+mobile));  
  37.             startActivity(intent);  
  38.         }  
  39.           
  40.     }  
  41. }  

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

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

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

  1. package cn.itcast.phone;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.net.Uri;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.   
  11. public class MainActivity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.           
  18.         //找到按钮控件   
  19.         Button button = (Button)this.findViewById(R.id.button);  
  20.         //为按钮添加点击事件   
  21.         button.setOnClickListener(new View.OnClickListener() {  
  22.               
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 //获取用户输入的电话号码   
  26.                 EditText mobileText = (EditText)findViewById(R.id.mobile);  
  27.                 String mobile = mobileText.getText().toString();  
  28.                 //拨打电话   
  29.                 //参考android-sdk-windows\platforms\android-8\sources\phone下AndroidManifest.xml   
  30.                 //文件中<activity android:name="OutgoingCallBroadcaster"开头的那一段   
  31.                 //android.intent.action.CALL可以用一个常量Intent.ACTION_CALL代替   
  32.                 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+mobile));  
  33.                 startActivity(intent);//内部会添加android.intent.category.DEFAULT   
  34.             }  
  35.         });  
  36.     }  
  37.       
  38. }  


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

  1. <activity android:name="OutgoingCallBroadcaster"  
  2.                 android:permission="android.permission.CALL_PHONE"  
  3.                 android:theme="@android:style/Theme.NoDisplay"  
  4.                 android:configChanges="orientation|keyboardHidden">  
  5.             <!-- CALL action intent filters, for the various ways  
  6.                  of initiating an outgoing call. -->  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.CALL" />  
  9.                 <category android:name="android.intent.category.DEFAULT" />  
  10.                 <data android:scheme="tel" />  
  11.             </intent-filter>  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.CALL" />  
  14.                 <category android:name="android.intent.category.DEFAULT" />  
  15.                 <data android:scheme="voicemail" />  
  16.             </intent-filter>  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.CALL" />  
  19.                 <category android:name="android.intent.category.DEFAULT" />  
  20.                 <data android:mimeType="vnd.android.cursor.item/phone" />  
  21.                 <data android:mimeType="vnd.android.cursor.item/phone_v2" />  
  22.                 <data android:mimeType="vnd.android.cursor.item/person" />  
  23.             </intent-filter>  
  24.         </activity>  
程序中只需要设置action和data,catagory会自动添加

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


短信发送器

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


界面:



然后是设计界面

layout/main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/mobile"  
  11.     />  
  12. <EditText    
  13.     android:layout_width="fill_parent"   
  14.     android:layout_height="wrap_content"   
  15.     android:id="@+id/mobile"  
  16.     />  
  17. <TextView    
  18.     android:layout_width="fill_parent"   
  19.     android:layout_height="wrap_content"   
  20.     android:text="@string/content"  
  21.     />  
  22. <EditText    
  23.     android:layout_width="fill_parent"   
  24.     android:layout_height="wrap_content"   
  25.     android:minLines="3"  
  26.     android:id="@+id/content"  
  27.     />      
  28. <Button    
  29.     android:layout_width="wrap_content"   
  30.     android:layout_height="wrap_content"   
  31.     android:text="@string/button"  
  32.     android:id="@+id/button"  
  33.     />     
  34. </LinearLayout>  

数据文件

values/strings.xml

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

SMSActivity

  1. package cn.itcast.sms;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.telephony.SmsManager;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.Toast;  
  12.   
  13. public class SMSActivity extends Activity {  
  14.     /** Called when the activity is first created. */  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.           
  20.         Button button = (Button)this.findViewById(R.id.button);  
  21.         button.setOnClickListener(new View.OnClickListener() {  
  22.               
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 //获取手机号和内容   
  26.                 EditText mobileText = (EditText)findViewById(R.id.mobile);  
  27.                 EditText contentText = (EditText)findViewById(R.id.content);  
  28.                 String mobile = mobileText.getText().toString();  
  29.                 String content = contentText.getText().toString();  
  30.                   
  31.                 //发送短信   
  32.                 SmsManager smsManager = SmsManager.getDefault();  
  33.                 List<String> texts = smsManager.divideMessage(content);//如果超过70个汉字,自动拆分短信   
  34.                 for(String text : texts){  
  35.                     smsManager.sendTextMessage(mobile, null, text, nullnull);  
  36.                 }  
  37.                   
  38.                 //使用吐司通知   
  39.                 Toast.makeText(SMSActivity.this, R.string.success, Toast.LENGTH_LONG).show();  
  40.             }  
  41.         });  
  42.     }  
  43. }  

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

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

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="cn.itcast.sms"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".SMSActivity"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.   
  15.     </application>  
  16.     <uses-sdk android:minSdkVersion="8" />  
  17.     <uses-permission android:name="android.permission.SEND_SMS"/>  
  18. </manifest>   

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



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


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


修改main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     >  
  6.     <LinearLayout  
  7.         android:orientation="horizontal"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/lineLayout"  
  11.      >      
  12.         <TextView    
  13.             android:layout_width="100dip"   
  14.             android:layout_height="wrap_content"   
  15.             android:text="@string/mobile"  
  16.             android:textSize="16sp"  
  17.             />  
  18.         <EditText  
  19.             android:layout_width="fill_parent"   
  20.             android:layout_height="wrap_content"   
  21.             android:phoneNumber="true"  
  22.             android:id="@+id/mobile"  
  23.             />  
  24.      </LinearLayout>  
  25.        
  26.     <TextView    
  27.         android:layout_width="fill_parent"   
  28.         android:layout_height="wrap_content"   
  29.         android:text="@string/content"  
  30.         android:layout_below="@id/lineLayout"  
  31.         android:id="@+id/contentLabel"  
  32.         android:textSize="16sp"  
  33.         />  
  34.     <EditText  
  35.         android:layout_width="fill_parent"   
  36.         android:layout_height="wrap_content"   
  37.         android:minLines="3"  
  38.         android:layout_below="@id/contentLabel"  
  39.         android:id="@+id/content"  
  40.         />  
  41.     <Button  
  42.         android:layout_width="wrap_content"   
  43.         android:layout_height="wrap_content"  
  44.         android:text="@string/button"  
  45.         android:layout_below="@id/content"  
  46.         android:layout_alignParentRight="true"  
  47.         android:id="@+id/button"  
  48.         />  
  49. </RelativeLayout>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值