zoukankan      html  css  js  c++  java
  • 简易的安卓拨号器

    一、视频笔记:

    1.

    当用户点击应用图标——>

    首先创建一个应用进程——>

    再创建一个主线程——>

    主线程中实例化Activity【注:OS会把应用有关的信息(Context)存放进Activity】——>

    调用onCreate()【注:OS调用,而不是用户调用,一个生命周期内只被调用一次】

    2.单位的使用

    文字:sp

    非文字:dp(=dip)

    3.

    开发软件时在一个项目中首先将界面(.xml)的内容写好。

    二、进行实践:

    1.界面:

    最终要实现的布局:

     

    activity_main.xml中:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:orientation="vertical"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent" >
     5 
     6     <TextView
     7         android:layout_width="match_parent"
     8         android:layout_height="wrap_content"
     9         android:text="@string/mobile" />
    10     
    11     <EditText 
    12         android:layout_width="match_parent"
    13         android:layout_height="wrap_content"
    14         android:id="@+id/mobile"
    15         />
    16     
    17     <Button 
    18         android:layout_width="wrap_content"
    19         android:layout_height="wrap_content"
    20         android:text="@string/button" 
    21         android:id="@+id/button"
    22         />
    23 
    24 </LinearLayout>

    string.xml中(为了更加国际化):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">电话拨号器</string>
        <string name="hello_world">Hello world!</string>
        <string name="action_settings">Settings</string>
        <string name="mobile">请输入手机号</string>
        <string name="button">拨号</string>
    
    </resources>

    2.清单文件中申请使用权限:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.phone"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="21" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <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>
    <!--权限:google为保护用户的信息安全和隐私数据,当使用到与此有关的数据时,
    必须申请相关权限,在软件安装时会出现权限提示  -->
        <uses-permission android:name="android.permission.CALL_PHONE"/>
    </manifest>

    3.代码实现:

    MainActivity.java:

    package com.example.phone;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends Activity {
        private EditText mobileText;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            mobileText = (EditText)findViewById(R.id.mobile);
            
            Button button  = (Button)this.findViewById(R.id.button);
            button.setOnClickListener(new ButtonClickListener());//或者使用匿名内部类:new实现了接口的类对象
        }
    
        //内部类:大量使用,提交软件的加载(到虚拟机的)速度
        private final class ButtonClickListener implements View.OnClickListener
        {
            @Override
            public void onClick(View v) {//输入参数是当前被点击的按钮对象
                // TODO Auto-generated method stub
                //每次点击拨号,下面一句都会使用findViewById查找一次资源id,造成耗时
    //            EditText mobileText = (EditText)findViewById(R.id.mobile);
                String number = mobileText.getText().toString();
                Intent intent = new Intent();
                intent.setAction("android.intent.action.CALL");
    //            intent.addCategory("android.intent.category.DEFAULT");//1代码
                intent.setData(Uri.parse("tel:"+number));
                //将意图传给OS,由OS发现匹配的activity,进行激活
                startActivity(intent);//注意:方法内部会自动为Intent添加android.intent.category.DEFAULT,所以1代码注释不要
            }
            
        }
    }

     

  • 相关阅读:
    [转]SIFT特征提取分析
    OSGEARTH三维地形开源项目
    使用C#改变鼠标的指针形状
    检测到 LoaderLock:DLL"XXXX"正试图在OS加载程序锁内执行
    未能加载文件或程序集“XXXXX”或它的某一个依赖项。试图加载格式不正确的程序。
    未能进入中断模式,原因如下:源文件“XXXXXX”不属于正在调试的项目。
    C# 版本的 计时器类:精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出
    OpenGL2.0及以上版本中glm,glut,glew,glfw,mesa等部件的关系
    OpenGL 4.3配置教程
    ubuntu maven环境安装配置
  • 原文地址:https://www.cnblogs.com/ttzm/p/7222478.html
Copyright © 2011-2022 走看看