搭建好安卓环境后,做了第一个简单应用
电话拨号器 :
主要两个地方
1 main.xml 布局
<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="请输入电话号码" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phone" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拔打此号码"
android:id="@+id/button" />
</LinearLayout>
2 MainActivity中的onCreate方法
private EditText phone=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone=(EditText)this.findViewById(R.id.phone);
Button button=(Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
String tel=phone.getText().toString();
//方法一, 使用Intent目的: 激活android组件
//Intent intent=new Intent();
//intent.setAction("android.intent.action.CALL");
//intent.setData(Uri.parse("tel:"+tel));
//方法二
Intent intent=new Intent("android.intent.action.CALL", Uri.parse("tel:"+tel));
//方法的内部会自动为intent对象设置类别:android.intent.category.DEFAULT
startActivity(intent);
}
});
}
但是运行后一直在报错
错误如下
03-06 01:52:34.773: E/AndroidRuntime(284): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:5556 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{460089b0 284:com.example.phonecall/10037} (pid=284, uid=10037) requires android.permission.CALL_PHONE
网上百度了下
由于要拨打电话,所以需要配置相应的权限信息
所以要在AndroiManifest.xml
Application外添加<uses-permission android:name="android.permission.CALL_PHONE"/>
再次运行,成功