当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程简单实现拨号器功能的方法

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网