Reputation: 225
I am developing an application to dial a number by giving the number into edit text box. i have used implicit intetns and passed the edit text number through setData.
i am able to run it successfully. But it is working good to dial 9 digits. if i give 10 digits to dial it is throwing an error. Please find my code below.
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>
Upvotes: 0
Views: 1029
Reputation: 1
Try this after entering the number such as "tel:"+1234567890 add an extra l on ie,"tel:"+1234567890l or we can also use L
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+1234567890l));
startActivity(intent);
Upvotes: -1
Reputation: 95618
It is pretty clear what is broken here. Looking at this code:
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);
you are trying to parse a 10-digit number by calling Integer.parseInt()
. The results should be stored in a variable that is declared as int
. Now int
is a signed 32-bit quantity and the range of values you can store in a signed 32-bit quantity is -2,147,483,648 to 2,147,483,647. That means that your code will work as long as your 10-digit telephone number is less than 2,147,483,647. Otherwise, you will either get a negative number or you will get an exception when trying to parse the number (because it is too big to fit into an int
).
You are going about this the wrong way. You don't need to parse the number if you are just going to create an URL out of it anyway. Try this instead:
Intent call=new Intent();
EditText num=(EditText) findViewById(R.id.Number);
call.setAction(android.content.Intent.ACTION_CALL);
call.setData(Uri.parse("tel:"+num.getText().toString()));
startActivity(call);
Upvotes: 3