你的位置:首页 > 操作系统

[操作系统]Android实战


今天跟着黑马视频建立一个android app--电话拨号器

首先新建一个android项目

activity_main_

<RelativeLayout ="http://schemas.android.com/apk/res/android"  ="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  tools:context=".MainActivity" >  <!-- 文本框 -->  <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="请输入号码" />  <EditText     android:layout_width="fill_parent"    android:layout_height="wrap_content"    /></RelativeLayout>

运行一下:

可以看到布局出现问题,文字出现重叠,简单的解决方法如下:

<LinearLayout ="http://schemas.android.com/apk/res/android"  ="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  tools:context=".MainActivity"   android:orientation="vertical"  >  <!-- 文本框 -->  <TextView    android:id="@+id/textView1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="请输入号码" />  <EditText    android:id="@+id/et_number"    android:layout_width="match_parent"    android:layout_height="wrap_content"    />  <Button     android:id="@+id/bt_call"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="拨打"    /></LinearLayout>

界面如下所示:

MainActivity中添加下面的代码:

package com.wuyudong.dial;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    // 通过资源id获取到布局文件中的 对应的组件    Button bt = (Button) findViewById(R.id.bt_call);    // 给按钮设置监听    bt.setOnClickListener(new MyListener());  }  class MyListener implements OnClickListener {    // 当按钮被点击的时候,此方法会被调用    @Override    public void onClick(View v) {      // TODO Auto-generated method stub      System.out.println("按钮被点击了!");      // 通过资源id获取到输入框      EditText et = (EditText) findViewById(R.id.et_number);      // 获取输入框的内容      String number = et.getText().toString().trim();      // 告诉系统我们的动作是打电话      Intent intent = new Intent();      // 设置动作      intent.setAction(Intent.ACTION_CALL);      // 设置打给谁      intent.setData(Uri.parse("tel:" + number));      //启动打电话应用,把意图告诉系统      startActivity(intent);    }  }  @Override  public boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.main, menu);    return true;  }}

拨打电话后,出现如下异常:

05-26 19:43:02.155: D/dalvikvm(2042): Not late-enabling CheckJNI (already on)
05-26 19:43:02.775: D/gralloc_goldfish(2042): Emulator without GPU emulation detected.
05-26 19:43:22.015: I/System.out(2042): 按钮被点击了!
05-26 19:43:22.015: D/AndroidRuntime(2042): Shutting down VM
05-26 19:43:22.015: W/dalvikvm(2042): threadid=1: thread exiting with uncaught exception (group=0xb0e8d648)
05-26 19:43:22.015: E/AndroidRuntime(2042): FATAL EXCEPTION: main
05-26 19:43:22.015: E/AndroidRuntime(2042): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxx cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{b1362a50 2042:com.wuyudong.dial/u0a10047} (pid=2042, uid=10047) requires android.permission.CALL_PHONE
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.os.Parcel.readException(Parcel.java:1431)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.os.Parcel.readException(Parcel.java:1385)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1947)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.app.Activity.startActivityForResult(Activity.java:3390)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.app.Activity.startActivityForResult(Activity.java:3351)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.app.Activity.startActivity(Activity.java:3587)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.app.Activity.startActivity(Activity.java:3555)
05-26 19:43:22.015: E/AndroidRuntime(2042): at com.wuyudong.dial.MainActivity$MyListener.onClick(MainActivity.java:44)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.view.View.performClick(View.java:4240)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.view.View$PerformClick.run(View.java:17721)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.os.Handler.handleCallback(Handler.java:730)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.os.Looper.loop(Looper.java:137)
05-26 19:43:22.015: E/AndroidRuntime(2042): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-26 19:43:22.015: E/AndroidRuntime(2042): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 19:43:22.015: E/AndroidRuntime(2042): at java.lang.reflect.Method.invoke(Method.java:525)
05-26 19:43:22.015: E/AndroidRuntime(2042): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-26 19:43:22.015: E/AndroidRuntime(2042): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-26 19:43:22.015: E/AndroidRuntime(2042): at dalvik.system.NativeStart.main(Native Method)

抛出安全异常,提示没有权限

解决办法如下:

打开 AndroidManifest.

点击Add,选择Users Permission

右边下拉单选择:android.permission.CALL_PHONE

Ctrl+S

重新运行,搞定