zoukankan      html  css  js  c++  java
  • Android学习笔记_1_拨打电话

    1、首先需要在AndroidManifest.xml文件中加入拨打电话的权限,对应的配置文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.test"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="10"
            android:targetSdkVersion="14" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.test.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-permission android:name="android.permission.CALL_PHONE"/>
        
    </manifest>
    View Code

    2、界面布局

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        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" >
    
        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:gravity="center" 
            android:textSize="35sp"/>
    
        <EditText
            android:id="@+id/editText1"
            android:layout_below="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/editText1"
            android:layout_below="@+id/editText1"
            android:text="@string/hello_btn" />
    
    </RelativeLayout>

    3、Activity类实现,点击事件的实现采用了匿名类的实现方式,

      

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn = (Button)this.findViewById(R.id.btn);
            final EditText phone = (EditText)this.findViewById(R.id.editText1);
            btn.setOnClickListener(new OnClickListener() {            
                @Override
                public void onClick(View arg0) {
                    String ph = phone.getText().toString();
                    if(ph!=null && !ph.trim().equals("")){
                        Intent intent = new Intent();
                        intent.setAction("android.intent.action.CALL");
                        //intent.addCategory("android.intent.category.DEFAULT");
                        intent.setData(Uri.parse("tel:"+ph));
                        startActivity(intent);//它内部会自动为intent添加类别,上面的内部可以不用写
                    }
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {    
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
  • 相关阅读:
    Java学习笔记2.Java标识符和基本数据类型
    Java面试题库(四)
    Eclipse最常用的一些快捷键技巧
    心理学166个现象(很多,别一次看完!)[119]
    Java学习笔记1.Java发展及JDK配置
    SQL操作全集
    J2EE面试题库
    Java面试题库(一)
    Java程序员,面试必读
    Java面试题库(二)
  • 原文地址:https://www.cnblogs.com/lbangel/p/3393294.html
Copyright © 2011-2022 走看看