源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-08-27 13:53 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要: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个虚拟手机 [img]http://files.jb51.net/file_images/article/201707/201772694558558.png?201762694651[/img] 电话打通了 [img]http://files.jb51.net/file_images/article/201707/201772694656255.png?201762694725[/img] 这个比较好玩吧,至于那个应用图标自己可以换成喜欢的,我就不改了 现在把那个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相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/381.htm]Android基本组件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/371.htm]Android布局layout技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/423.htm]Android资源操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/325.htm]Android文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/367.htm]Android编程之activity操作技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部