[Android]电话拨号器开发
继续今天的Android,经过昨天大体了解了Android开发的一些基本文件结构,今天来做一个电话拨号器!
创新互联是一家专注于成都网站制作、成都做网站与策划设计,孟津网站建设哪家好?创新互联做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:孟津等地区。孟津做网站价格咨询:18980820575
预期达到的效果
实现过程
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
接着在activity_main.xml新建输入文本框,添加特殊的id属性android:id="@+id/et_number",这里‘+’表示动态向R文件添加一个静态的内部类id,然后在R文件中动态的添加了一个et_number,以便后面进行操作
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:lines = "1"
android:hint="@string/hint"
android:id="@+id/et_number"
/>
其中在
android:numeric="integer"
android:lines = "1"
接下来处理按钮点击事件
在MainActivity中添加this.findViewById(R.id.bt_dail);就能找到该按钮,它返回的其实是一个view,继承关系是button继承自TextView,TextView又继承自View
注册MainActivity的tag
public static final String tag = "MainActivity";
添加点击事件
Button button = (Button)this.findViewById(R.id.bt_dail);
button.setOnClickListener((OnClickListener) new MyButtonClickListener());
privateclass MyButtonClickListenerimplements OnClickListener
{
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
System.out.println("按钮被点击了");
Log.i(tag, "button被点击了");
Log.e(tag, "error");
Log.d(tag, "debug");
Log.v(tag,"verbios");
Log.w(tag, "warm");
}
}
点击按钮会在Log日志中会输对应的日志
然后实现拨打电话的功能
privateclass MyButtonClickListenerimplements OnClickListener
{
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
//拿到电话号码
EditText mEditText = (EditText)MainActivity.this.findViewById(R.id.et_number);
String number = mEditText.getText().toString();
Log.i(tag, number);
//拨打电话号码
Intent intent = new Intent();//意图 代表一个要执行动作的意图
//拨打动作 110
intent.setAction(Intent.ACTION_CALL);//Intent.ACTION_DAL是拨号的操作
//输入的话就拨打输入的,不属于的话就拨打作者的号码
if(!"".equals(number))
{
intent.setData(Uri.parse("tel:"+number));
}
else
{
Log.i(tag, "我的号码");
intent.setData(Uri.parse("tel:"+"13262983383"));
}
//激活意图
startActivity(intent);
}
到目前为止代码是差不多了点击运行,会发现报错了
我们来查看权限错误的Log日志
04-0911:31:19.945: E/AndroidRuntime(516): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:110 cmp=com.android.phone/.OutgoingCallBroadcaster } from Proce***ecord{45ec9830516:com.example.phonecall/10038} (pid=516, uid=10038) requires android.permission.CALL_PHONE
04-0911:31:19.945: E/AndroidRuntime(516): at android.os.Parcel.readException(Parcel.java:1247)
04-0911:31:19.945: E/AndroidRuntime(516): at android.os.Parcel.readException(Parcel.java:1235)
04-0911:31:19.945: E/AndroidRuntime(516): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1298)
04-0911:31:19.945: E/AndroidRuntime(516): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
04-0911:31:19.945: E/AndroidRuntime(516): at android.app.Activity.startActivityForResult(Activity.java:2817)
04-0911:31:19.945: E/AndroidRuntime(516): at android.app.Activity.startActivity(Activity.java:2923)
04-0911:31:19.945: E/AndroidRuntime(516): at com.example.phonecall.MainActivity$MyButtonClickListener.onClick(MainActivity.java:45)
04-0911:31:19.945: E/AndroidRuntime(516): at android.view.View.performClick(View.java:2408)
04-0911:31:19.945: E/AndroidRuntime(516): at android.view.View$PerformClick.run(View.java:8816)
04-0911:31:19.945: E/AndroidRuntime(516): at android.os.Handler.handleCallback(Handler.java:587)
04-0911:31:19.945: E/AndroidRuntime(516): at android.os.Handler.dispatchMessage(Handler.java:92)
04-0911:31:19.945: E/AndroidRuntime(516): at android.os.Looper.loop(Looper.java:123)
04-0911:31:19.945: E/AndroidRuntime(516): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-0911:31:19.945: E/AndroidRuntime(516): at java.lang.reflect.Method.invokeNative(Native Method)
04-0911:31:19.945: E/AndroidRuntime(516): at java.lang.reflect.Method.invoke(Method.java:521)
04-0911:31:19.945: E/AndroidRuntime(516): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-0911:31:19.945: E/AndroidRuntime(516): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-0911:31:19.945: E/AndroidRuntime(516): at dalvik.system.NativeStart.main(Native Method)
java.lang.SecurityException: Permission Denial
解决办法,添加权限
找到ANdroidManifest.xml清单文件,找到Permissions
Add->android.permission.CALL_PHONE_CALL
网页标题:[Android]电话拨号器开发
路径分享:http://scyanting.com/article/iippep.html