需求:
在editText中输入号码,点击button,自动拨号
代码:
public class MainActivity extends Activity {
private Button mButton;
private EditText mEditText;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button1);
mEditText = (EditText)findViewById(R.id.editText1);
mButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try
{
String strInput = mEditText.getText().toString();
if(1 == 1)
{
Intent myIntentDial = new
Intent("android.intent.action.CALL", Uri.parse("tel:"+strInput));
startActivity(myIntentDial);
mEditText.setText("");
}
else
{
mEditText.setText("");
Toast.makeText(MainActivity.this, "input num:", Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
@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;
}
}
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calltest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.calltest.MainActivity"
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-permission android:name="android.permission.CALL_PHONE">
</uses-permission>
</manifest>
重点:
1.添加拨号权限;
2.启动另一个活动的方式;
3.如何设置为拨号