`

Android通信(拨号、发短信、发邮件)

 
阅读更多

        今天学习了安卓通信的内容,我们可以编写程序来调用系统的内置的API很容易就实现一些系统的自带的功能。下面讲一讲自己的实现过程。

       首先是首页main_activity的的设计:



 
       首页的功能主要有数字按键、添加联系人按键、拨号按键、发短信按键、发邮件按键。数字按键主要通过图片按钮添加背景图来实现,每个数字按键绑定一个数字,例如0,利用 android:tag="0"语句来绑定,获取数字0的时候就利用语句text=btnDigital.getTag().toString();来获取数字。

     然后说说布局,通过这次的设计让我对于布局有了进一步的了解,前两天对于布局都是利用直接拖动按钮来编写的,然后一知半解的,现在至少能懂怎么用了,特别是熟悉了表格布局。



 

   如上图,页面的布局主要是利用表格的布局来实现数字的排版,然后表格里面还通过TableRow来分行,其实现代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="82dp"
        android:ems="10"
        android:background="#ffffffff"
        android:textSize="40sp"
        android:textStyle="bold"
        android:textColor="#ff333333" >
        <requestFocus />
    </EditText>

    <TableLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"       
        >

        <TableRow
            android:id="@+id/TableRow04"
            android:layout_width="110dp"
            android:layout_height="wrap_content" >

            <ImageButton
                android:id="@+id/ImageButton01"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:onClick="digital_click"
                android:src="@drawable/n1"               
                android:tag="1" />

            <ImageButton
                android:id="@+id/ImageButton02"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:src="@drawable/n2"
                android:onClick="digital_click"              
                android:tag="2"/>

            <ImageButton
                android:id="@+id/ImageButton03"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:src="@drawable/n3"
                android:onClick="digital_click"               
                android:tag="3" />
        </TableRow>

        <TableRow
            android:id="@+id/TableRow03"
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <ImageButton
                android:id="@+id/ImageButton4"
                android:layout_width="100dp"
                android:layout_height="70dp"               
                android:onClick="digital_click"
                android:src="@drawable/n4"              
                android:tag="4" />

            <ImageButton
                android:id="@+id/ImageButton5"
                android:layout_width="100dp"
                android:layout_height="70dp"               
                android:onClick="digital_click"
                android:src="@drawable/n5"               
                android:tag="5" />

            <ImageButton
                android:id="@+id/ImageButton06"
                android:layout_width="100dp"
                android:layout_height="70dp"               
                android:onClick="digital_click"
                android:src="@drawable/n6"              
                android:tag="6"/>
        </TableRow>

        <TableRow
            android:id="@+id/TableRow02"
            android:layout_width="100dp"
            android:layout_height="wrap_content" >
            <ImageButton
                android:id="@+id/ImageButton07"
                android:layout_width="100dp"
                android:layout_height="70dp"               
                android:onClick="digital_click"
                android:src="@drawable/n7"
                android:tag="7"/>
            <ImageButton
                android:id="@+id/ImageButton08"
                android:layout_width="100dp"
                android:layout_height="70dp"                
                android:onClick="digital_click"
                android:src="@drawable/n8"
                android:tag="8" />
            <ImageButton
                android:id="@+id/ImageButton09"
                android:layout_width="100dp"
                android:layout_height="70dp"          
                android:onClick="digital_click"
                android:src="@drawable/n9"
                android:tag="9" />
        </TableRow>
        <TableRow
            android:id="@+id/TableRow01"
            android:layout_width="100dp"
            android:layout_height="wrap_content" >
            <ImageButton
                android:id="@+id/ImageButton10"
                android:layout_width="100dp"
                android:layout_height="70dp"               
                android:onClick="digital_click"
                android:src="@drawable/n10"
                android:tag="*"/>
            <ImageButton
                android:id="@+id/ImageButton11"
                android:layout_width="100dp"
                android:layout_height="70dp"            
                android:onClick="digital_click"
                android:src="@drawable/n11"
                android:tag="0"/>

            <ImageButton
                android:id="@+id/ImageButton12"
                android:layout_width="100dp"
                android:layout_height="70dp"           
                android:onClick="digital_click"
                android:src="@drawable/n12"
                android:tag="#" />

        </TableRow>
			
        
    </TableLayout>

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.23"
            android:onClick="newPeople"
            android:background="@drawable/people" />

        <ImageButton
            android:id="@+id/dialer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.17"
            android:onClick="dial" 
            android:background="@drawable/callpeople"
             />

        <ImageButton
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="delete"
            android:background="@drawable/delete" />

    </LinearLayout>
    
     <LinearLayout
         android:layout_width="300dp"
         android:layout_height="wrap_content" >

         <ImageButton
             android:id="@+id/msg"
             android:layout_width="50dp"
             android:layout_height="50dp"
             android:src="@drawable/msg" />

         

         <ImageButton
             android:id="@+id/email"
             android:layout_width="50dp"
             android:layout_height="50dp"
             android:layout_marginLeft="200dp"
             android:onClick="sendEmail"
             android:src="@drawable/email" />
         
     </LinearLayout>

</LinearLayout>

 
      主要功能的实现步骤如下:

    1.按键的时候调用函数添加号码:

	//按键之后执行的动作
	public void digital_click(View view){	  
 	   ImageButton btnDigital=(ImageButton) view;
 	   String text=btnDigital.getTag().toString();
        currentNum.append(text);//currentNum是StringBuffer来存号码
        display();//调用函数显示号码      
 }
//显示栏显示号码的函数
	public void display(){
	     textResult.setText(currentNum.toString());
	    }

 

2.按下拨号键的时候调用系统的API进行拨号:

//拨打功能调用函数
	public void dial(View view) {	
		 EditText text=(EditText)findViewById(R.id.editText1);
		 String number=text.getText().toString();
		Intent intent = new Intent();
		intent.setAction(intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:"+number));
		startActivity(intent);//方法内部会自动为Intent添加类别:  android.intent.category.DEFAULT
		 }
	

      tip:在调用系统的拨号功能需要添加拨号的权限,我们需要在Androidmanifest里面添加如下语句:

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

 3.添加联系人:

//新建联系人
	 public void newPeople(View view){  
		 //接口
         Intent it = new Intent(Intent.ACTION_INSERT,Uri.withAppendedPath(Uri.parse("content://com.android.contacts"),"contacts" ));  
         it.setType("vnd.android.cursor.dir/person");  
         String number = textResult.getText().toString();  
         it.putExtra(android.provider.ContactsContract.Intents.Insert.SECONDARY_PHONE,number);  
         startActivity(it);  
           
     }  

 4.删除号码数字删除键的函数:

 

//删除键的功能
	public void delete(View view){
    	if(currentNum.length()>=1){
    		currentNum.delete(currentNum.length()-1, currentNum.length());
    	}
    	if(currentNum.length()==0){
    		Toast toast=Toast.makeText(this,"请输入号码",100);
    		toast.show();
    		display();
    	}
    	textResult.setText(currentNum);
    }

 5.短信和邮件的页面跳转实现:

msg=(ImageButton)findViewById(R.id.msg);			 
		msg.setOnClickListener(new OnClickListener(){				
				public void onClick(View v)
				{
			         Intent it = new Intent();  
			         it.setClass(MainActivity.this, msg.class);
				     MainActivity.this.startActivity(it);
                          }
	});		
		email=(ImageButton)findViewById(R.id.email);			 
		email.setOnClickListener(new OnClickListener(){				
				public void onClick(View v)
				{
			         Intent it = new Intent();  
			         it.setClass(MainActivity.this, email.class);
				     MainActivity.this.startActivity(it);
          
                }
	});

 6.设计信页面和邮件;

7.短信的功能的实现:

public class msg  extends Activity {	
	private EditText tel=null;
	private EditText content=null;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.msg);
		this.tel=(EditText)super.findViewById(R.id.tel);
		this.content=(EditText)super.findViewById(R.id.content);
	}	
	public void send(View view){
		String telMsg=msg.this.tel.getText().toString();
		String contentMsg=msg.this.content.getText().toString();
		Uri uri=Uri.parse("smsto:"+telMsg);//接收人手机
		Intent it=new Intent();
		it.setAction(Intent.ACTION_SENDTO);//指定action,我要发短信
		it.putExtra("sms_body", contentMsg);//设置信息内容
		it.setType("vnd.android-dir/mms-sms") ;//设置MIME类型
		it.setData(uri);//设置数据,要去的地方
		msg.this.startActivity(it);
	}
}

 最后的结果:

 当我们点击发送的时候调用函数,系统会跳到你的发短信页面,内容都会写好在短信里面。


8.发邮件功能实现:

public class email extends Activity {
	private EditText rec=null;
	private EditText subject=null;
	private EditText emailText=null;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.email);
		this.rec=(EditText)super.findViewById(R.id.receive);
		this.subject=(EditText)super.findViewById(R.id.subject);
		this.emailText=(EditText)super.findViewById(R.id.emailText);
	}
	

public void sendEmail(View view){
	String recMsg=email.this.rec.getText().toString();
	String contentMsg=email.this.emailText.getText().toString();
	String subjectMsg=email.this.subject.getText().toString();
    Intent emailIntent = new Intent(Intent.ACTION_SEND) ;
	emailIntent.setType("plain/text") ;	// 设置类型
	String address[] = new String[] {recMsg} ;
	emailIntent.putExtra(Intent.EXTRA_EMAIL, address) ;//邮件地址
	emailIntent.putExtra(Intent.EXTRA_SUBJECT, subjectMsg) ;
	emailIntent.putExtra(Intent.EXTRA_TEXT, contentMsg) ;
	startActivity(emailIntent);	// 执行跳转
}}

 

输入相关内容的时候,点击发送,用手机的邮件app打开就会有相关内容。

     以上就是今天所做的所有内容,学会了怎么调用系统的API来实现通信功能,只要知道了相关的接口,实现相关功能总的来说还是比较简单的。
 




 
 
 
 


 
 

 

  • 大小: 91.6 KB
  • 大小: 44 KB
  • 大小: 64.8 KB
  • 大小: 35.7 KB
  • 大小: 14.5 KB
分享到:
评论

相关推荐

    Android开发实验---通讯录.docx

    3、通功能 用户在通录选择联系人进入联系详细信息界面,这时点击menu键打开通信功能框,选择打电话、发信息的功能进行操作。 4、菜单能 通过对menu按的点击,显示底部菜框,包含有增加查找、除、菜单、返回功能,...

    《Google Android SDK开发范例大全(第3版)》.pdf

     全书共分11章,主要以范例集的方式来讲述android的知识点,详细介绍了开发android的人机交互界面、android常用的开发控件、android手机收发短信等通信服务、开发android手机的自动服务功能和娱乐多媒体功能以及...

    Google Android SDK开发范例大全(第3版) 1/5

    全书共分11章,主要以范例集的方式来讲述Android的知识点,详细介绍了开发Android的人机交互界面、Android常用的开发控件、Android手机收发短信等通信服务、开发Android手机的自动服务功能和娱乐多媒体功能以及整合...

    Google Android SDK开发范例大全(第3版) 4/5

    全书共分11章,主要以范例集的方式来讲述Android的知识点,详细介绍了开发Android的人机交互界面、Android常用的开发控件、Android手机收发短信等通信服务、开发Android手机的自动服务功能和娱乐多媒体功能以及整合...

    Google Android SDK开发范例大全(第3版) 3/5

    全书共分11章,主要以范例集的方式来讲述Android的知识点,详细介绍了开发Android的人机交互界面、Android常用的开发控件、Android手机收发短信等通信服务、开发Android手机的自动服务功能和娱乐多媒体功能以及整合...

    Google Android SDK开发范例大全(第3版) 5/5

    全书共分11章,主要以范例集的方式来讲述Android的知识点,详细介绍了开发Android的人机交互界面、Android常用的开发控件、Android手机收发短信等通信服务、开发Android手机的自动服务功能和娱乐多媒体功能以及整合...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量——Environment加StatFs 6.9 访问本机内存与存储卡——File的创建与删除 6.10 实现可定时响起的闹钟——PendingIntent与...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量——Environment加StatFs 6.9 访问本机内存与存储卡——File的创建与删除 6.10 实现可定时响起的闹钟——PendingIntent与...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量——Environment加StatFs 6.9 访问本机内存与存储卡——File的创建与删除 6.10 实现可定时响起的闹钟——PendingIntent与...

    Google Android SDK开发范例大全的目录

    6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量——Environment加StatFs 6.9 访问本机内存与存储卡——File的创建与删除 6.10 实现可定时响起的闹钟——PendingIntent与...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量——Environment加StatFs 6.9 访问本机内存与存储卡——File的创建与删除 6.10 实现可定时响起的闹钟——PendingIntent与...

    google android sdk开发范例大全 第二版 PDF 光盘代码

    google android sdk开发范例大全 第二版 PDF 和 随书光盘代码 ISBN:9787115229649 目录 第1章 了解、深入、动手做   1.1 红透半边天的Android   1.2 本书目的及范例涵盖范围   1.3 如何阅读本书 ...

    Google Android SDK 开发范例大全01

    6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量——Environment加StatFs 6.9 访问本机内存与存储卡——File的创建与删除 6.10 实现可定时响起的闹钟——PendingIntent与...

    Google Android SDK 开发范例大全02

    6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量——Environment加StatFs 6.9 访问本机内存与存储卡——File的创建与删除 6.10 实现可定时响起的闹钟——PendingIntent与...

    Google+Android+SDK开发范例大全

    6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之onCallStateChanged 6.7 有来电,发送邮件通知——...

    Google Android sdk 开发范例大全 部分章节代码

    6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量——Environment加StatFs 6.9 访问本机内存与存储卡——File的创建与删除 6.10 实现可定时响起的闹钟——PendingIntent与...

    Google Android SDK开发范例大全(完整版)

    6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量——Environment加StatFs 6.9 访问本机内存与存储卡——File的创建与删除 6.10 实现可定时响起的闹钟——PendingIntent与...

Global site tag (gtag.js) - Google Analytics