android应用开发实践三----Atcity 通过Intent 来启动另一个Activity来实现简易拨号

用到的字符串资源

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Myphone</string>
    <string name="hello_world">Hello world!</string>
    <string name="hint">请输入号码:</string>
    <string name="action_settings">Settings</string>
    <string name="dial"> 拨打号码 </string>
    <string name="unicom"> 联通热线 </string>
     <string name="pay"> 充值热线</string>
     
      
      
</resources>

注意在Android4.0以上中,字符串推荐在string.xml中先定义再使用,不推荐直接使用字符串赋值方式,而使用@引用方式,否则会报警

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
     <EditText
         android:id="@+id/text"  
         android:layout_width="fill_parent"
         android:layout_height="140dip"
         android:background="#c0c0c0"
         android:inputType="phone"
         android:textSize="40sp"  
         android:gravity="center"  
         android:textColor="#7cfc00"
         android:paddingLeft="18dp"  
         android:paddingRight="24dp"
         android:layout_margin="10dip"
        />
       <Button
           android:id="@+id/button1"
           android:layout_width="110dip"
           android:layout_height="wrap_content"
           android:layout_below="@+id/text"
            android:background="#c0c0c0"
            android:layout_margin="10dip"
           android:textColor="#b8860b"
           android:text="@string/dial"
           
           android:textSize="60sp" />
       
       <Button
           android:id="@+id/button2"
           android:layout_width="110dip"
           android:layout_height="wrap_content"
           android:layout_toRightOf="@id/button1"
           android:layout_below="@+id/text"
           android:layout_margin="10dip"
           android:background="#c0c0c0"
           android:text="@string/unicom"
           android:textColor="#7fff00"
           android:textSize="60sp" />

       <Button
           android:id="@+id/button3"
           android:layout_width="110dip"
           android:layout_height="wrap_content"
           android:layout_below="@+id/text"
            android:layout_toRightOf="@id/button2"
           android:layout_margin="10dip"
           android:background="#c0c0c0"
           android:text="@string/pay"
           android:textColor="#ff4500"
           android:textSize="60sp" />
     
 
  </RelativeLayout>
  

在android4.0以上,有一个framement_main.xml和activiy_main.xml,后者才是正在的布局文件,相当于android2.0中的main.xml.


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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
   <uses-permission  android:name="android.permission.CALL_PHONE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/myphone"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myphone.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>

</manifest>


注意电话呼叫涉及用户的隐私权限,需要采用该语句提示用户获得许可,否则程序无法安装

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

java源代码

package com.example.myphone;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
	Button bt1,bt2,bt3;
	String s;
	TextView v;
	TextView v2;
	EditText number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
    	bt1=(Button)findViewById(R.id.button1);
    	bt2=(Button)findViewById(R.id.button2);
    	bt3=(Button)findViewById(R.id.button3);
    	number=(EditText)findViewById(R.id.text);
    	bt1.setOnClickListener(listener);
    	bt2.setOnClickListener(listener);
    	bt3.setOnClickListener(listener);
    }
    private OnClickListener listener =new OnClickListener()
    {
    	@Override
		public void onClick(View arg0) 
		{
			Button btx=(Button)arg0;
			
			switch(btx.getId())
						{

							case R.id.button1:
								
			                  	s=number.getText().toString();
			                  	if(s.equals("")==true)
			                  		return;
			                    	break;
			              
							case R.id.button2:
							  s="10010";
							  break;
							case R.id.button3:
							  s="10011";
							   break;
						}
			    Uri data;
				Intent intent=new Intent();
				intent.setAction("android.intent.action.CALL");
				intent.addCategory("android.intent.category.DEFAULT");
				data=Uri.parse("tel:"+ s);
				intent.setData(data);
				startActivity(intent);
	    }
			
		  
    };
    
}

注意Intent使用要点:

1.行为 action

2.类别 category

3.数据 data,所有的data采用Uri格式,注意转换

4.通过intent启动另外一个activity


以下是程序拨号的效果



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江海细流

如该文章对你有帮助,请支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值