android程序实现简单拨号器功能
来源:互联网 发布:淘宝女童模特可可真名 编辑:程序博客网 时间:2024/04/28 13:10
学习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实现拨号功能
- Android程序开发:简单电话拨号器
- Android之简单拨号器的实现
- Android项目:简单拨号器的实现
- Android开发之实现拨号功能
- [重写]Android中的简单拨号程序
- 简单的实现拨号器
- Android:程序锁功能的简单实现
- Android简单电话拨号器
- Android应用程序:简单拨号器
- android 简单拨号器 代码
- Android 电话拨号器程序
- android虚拟机拨号程序的实现
- 手机实现拨号功能,
- android拨号器的实现
- Android拨号器的实现
- 6.087 Practical Programming in C, lec14
- POSIX线程 (1)
- Git一分钟上手
- pb函数modify
- 关系代数——除法
- android程序实现简单拨号器功能
- 第六届全国ITAT教育工程就业技能大赛复赛试题 Java程序设计(B卷)答案
- 重定向
- Java线程的传说(3)——如何关闭一个正在accept的ServerSocket?
- URL重定向
- PB常用程序汇总4
- 测试高亮插件
- php引用(&)详解及注意事项
- Unix--系统下Shell语法分析