我正在开发一个应用程序,通过将号码输入编辑文本框中来拨打号码。我使用了隐式 intetns 并通过 setData 传递了编辑文本编号。
我能够成功运行它。但拨 9 位数效果很好。如果我给 10 位数字拨号,则会引发错误。请在下面找到我的代码。
Activity:
public class InvokeImplicitIntent extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewContacts();
}
private void ViewContacts() {
try {
Button viewContacts = (Button)findViewById(R.id.ViewContacts);
viewContacts.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent call=new Intent();
EditText num=(EditText) findViewById(R.id.Number);
int numberIn=Integer.parseInt(num.getText().toString());
call.setAction(android.content.Intent.ACTION_CALL);
call.setData(Uri.parse("tel:"+numberIn));
startActivity(call);
}
});
}catch (ActivityNotFoundException anfe) {
Log.e("ViewContacts","Viewing of Contacts failed", anfe);
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.collabera.labs.sai"
android:versionCode="1"
android:versionName="1.0">
<!-- <uses-permission android:name="android.permission.READ_CONTACTS"
/>
-->
<uses-permission
android:name="android.permission.CALL_PHONE"
/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".InvokeImplicitIntent"
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-sdk android:minSdkVersion="7" />
</manifest>
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText android:id="@+id/Number"
android:hint="Enter number"
android:inputType="number"
android:maxLength="10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<Button
android:text="@string/InvokeImplicit"
android:id="@+id/ViewContacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
</Button>
</LinearLayout>