在清单文件中需要用到权限:
<uses-permission android:name="android.permission.CALL_PHONE"/>
获取到输入的号码后需要用intent 启动拨号器:
Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse(("tel:"+infos))); startActivity(intent);
以下全部代码:
package com.example.callphone; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private EditText my_info; private Button my_call; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { // TODO Auto-generated method stub my_call = (Button) findViewById(R.id.call); my_info = (EditText) findViewById(R.id.info); my_call.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String infos = my_info.getText().toString().trim(); Intent intent = new Intent(Intent.ACTION_CALL, Uri .parse(("tel:" + infos))); startActivity(intent); } }); } }
xml布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" 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="com.example.callphone.MainActivity" > <EditText android:id="@+id/info" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入号码" /> <Button android:id="@+id/call" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="拨打" /> </LinearLayout>
时间: 2024-10-12 08:12:56