Android编程简单实现拨号器功能的方法

 更新时间:2017年07月26日 09:37:36   作者:青蛙小王子  
这篇文章主要介绍了Android编程简单实现拨号器功能的方法,结合实例形式较为详细的分析了Android拨号器功能的实现原理、步骤、操作技巧与相关注意事项,需要的朋友可以参考下

本文实例讲述了Android编程简单实现拨号器功能的方法。分享给大家供大家参考,具体如下:

学习Android已经有2天时间了,没学习的时候觉得android可能很枯燥,但是学过之后我发觉其实这个比什么javaweb好玩多了。学习android可以见到一些很有趣的东西,这里呢也建议学习javaME的人不要在煎熬了,学习android吧。在写程序之前也需要知道android的工作原理

1.获取组件清单
2.登记或注册组件
3.将组件封装成意图
4.把意图交给意图处理器进行处理
5.把界面显示给用户

看过网上android的开发流程,好多人都说可以把界面和activity并行开发,因为android也是遵循mvc设计模式,也就是说android也可有自己的业务层DAO。由于android发展历史比较短,目前的分工还不是很明确,对于界面和后台可以选择其中一个作为自己的发展方向,对于android的任何一块来说薪水都比较高。废话就不多说了,来一步一步的实现功能吧。

1.编写“文字”的配置文件,默认的配置文件是strings.xml,这里也可以重新写一个配置文件,格式要保持一致就来写这个配置文件(mystring.xml)吧

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="tip">输入号码</string>
  <string name="bottonname">拨打</string>
</resources>

2.编写控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent"> <!-- 线性布局 -->
  <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/tip" />
  <EditText android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/phonenumber"/>  <!-- 显示一个文本框 id为phonenumber-->
  <Button android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/bottonname"
    android:id="@+id/botton"
    />  <!-- 显示一个按钮 -->
</LinearLayout>

为了让大家看的更清楚,我把R文件的内容也给大家

/* AUTO-GENERATED FILE. DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found. It
 * should not be modified by hand.
 */
package org.lxh.phone;
public final class R {
  public static final class attr {
  }
  public static final class drawable {
    public static final int icon=0x7f020000;
  }
  public static final class id {
    public static final int botton=0x7f050001;
    public static final int phonenumber=0x7f050000;
  }
  public static final class layout {
    public static final int main=0x7f030000;
  }
  public static final class string {
    public static final int app_name=0x7f040003;
    public static final int bottonname=0x7f040001;
    public static final int hello=0x7f040002;
    public static final int tip=0x7f040000;
  }
}

3.编写activity

package org.lxh.phone;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class PhoneActivity extends Activity {
   private EditText edit;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    edit=(EditText)this.findViewById(R.id.phonenumber);  //通过id取得文本输入框
    Button but=(Button)this.findViewById(R.id.botton);  //通过id取得按钮
    but.setOnClickListener(new MyListener()); //给按钮添加监听器
  }
  public final class MyListener implements View.OnClickListener{  //自定义的监听器
    public void onClick(View v) {
      //实例化一个意图(动作),用来拨打电话
      Intent intent=new Intent("android.intent.action.CALL",Uri.parse("tel:"+edit.getText().toString()));
      startActivity(intent); //封装一个意图
    }
  }
}

上面是内部类的写法,也可以使用下面的写法

package org.lxh.activity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CallPhoneActivity extends Activity {
  private EditText edittext;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //取得输入框和按钮
    edittext=(EditText)this.findViewById(R.id.phonenum);
    Button but=(Button)this.findViewById(R.id.button);
    but.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        String number=edittext.getText().toString();
        //封装一个意图,用来拨打电话
        Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number));
        startActivity(intent);
      }
    });
  }
}

开发的时候要注意Uri.parse不能少,tel:也不能少,少了就会出错

这里要实现这个功能,首先要来看一下xml

<activity android:name="OutgoingCallBroadcaster"
    android:permission="android.permission.CALL_PHONE"
    android:theme="@android:style/Theme.NoDisplay"
    android:configChanges="orientation|keyboardHidden">
  <!-- CALL action intent filters, for the various ways
    of initiating an outgoing call. -->
  <intent-filter>
    <action android:name="android.intent.action.CALL" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="tel" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.CALL" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="voicemail" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.CALL" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="vnd.android.cursor.item/phone" />
    <data android:mimeType="vnd.android.cursor.item/phone_v2" />
    <data android:mimeType="vnd.android.cursor.item/person" />
  </intent-filter>
</activity>

这里只需要看第一个filter,这里只需使用2条,那个默认的不用我们去管,另外这个也是需要获得打电话的许可的,所以在组件清单里要加一点东西,如下所示

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="org.lxh.phone"
   android:versionCode="1"
   android:versionName="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".PhoneActivity"
         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-sdk android:minSdkVersion="7" />
  <uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>

准备工作差不多做好了,来测试一下吧,这里为了测试方便,我弄了2个虚拟手机

电话打通了

这个比较好玩吧,至于那个应用图标自己可以换成喜欢的,我就不改了

现在把那个strings.xml配置文件给大家

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">Hello World, PhoneActivity!</string>
  <string name="app_name">我的手机拨号器</string>
</resources>

OK了,程序写好了。

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android资源操作技巧汇总》、《Android文件操作技巧汇总》、《Android开发入门与进阶教程》、《Android编程之activity操作技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android利用SurfaceView实现下雨的天气动画效果

    Android利用SurfaceView实现下雨的天气动画效果

    这篇文章主要介绍了Android利用SurfaceView实现下雨天气效果的相关资料,文中详细介绍 SurfaceView 和 View 的区别,以及一些需要使用到 SurfaceView 的场景。需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-03-03
  • Android入门之Style与Theme用法实例解析

    Android入门之Style与Theme用法实例解析

    这篇文章主要介绍了Android入门之Style与Theme用法,非常实用的功能,需要的朋友可以参考下
    2014-08-08
  • 解决video标签在安卓webview下无法自动播放问题

    解决video标签在安卓webview下无法自动播放问题

    这篇文章主要介绍了video标签在安卓webview下无法自动播放问题的解决方法 ,需要的朋友可以参考下
    2014-03-03
  • Android实现Bitmap位图旋转效果

    Android实现Bitmap位图旋转效果

    这篇文章主要为大家详细介绍了Android实现Bitmap位图旋转效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • 解决Android Studio 出现“Cannot resolve symbol” 的问题

    解决Android Studio 出现“Cannot resolve symbo

    今天在调试的时候,Android Studio报了一个莫名其妙的错误Cannot resolve symbol'R'让人不知所措,因为这东西根本不归我管啊,怎么会出现 Cannot resolve symbol 这种错误呢?下面给大家分享Android Studio 出现“Cannot resolve symbol”解决方案,需要的朋友可以参考下
    2023-03-03
  • android SectorMenuView底部导航扇形菜单的实现代码

    android SectorMenuView底部导航扇形菜单的实现代码

    这篇文章主要介绍了android SectorMenuView底部导航扇形菜单的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Android自定义Dialog原理实例解析

    Android自定义Dialog原理实例解析

    这篇文章主要介绍了Android自定义Dialog原理实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Android中recyclerView底部添加透明渐变效果

    Android中recyclerView底部添加透明渐变效果

    这篇文章主要给大家介绍了关于Android中recyclerView如何实现底部添加透明渐变效果的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2018-04-04
  • Android Studio 1.2版安装设置图文教程

    Android Studio 1.2版安装设置图文教程

    这篇文章主要介绍了Android Studio 1.2版安装设置图文教程,本文详细讲解了下载、安装Android Studio 1.2教程,以及常用设置详细图文教程,需要的朋友可以参考下
    2015-05-05
  • 深入浅析Android坐标系统

    深入浅析Android坐标系统

    这篇文章主要介绍了 深入浅析Android坐标系统的相关资料,需要的朋友可以参考下
    2016-01-01

最新评论