user1091368
user1091368

Reputation: 359

I'm trying to make a simple app that gives you the option to make a phone call

My droid app has a button on the main page that gives you an dialogue popup page and asks within the dialogue page whether you want to make a phone call or not

Ive tried and over and over and haven't quite figured out how to get the button to make a phone call

My opening java page package your.package2.phonecalls;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ButtonphonecallsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button TheCallButton = (Button) findViewById(R.id.phonecallbutton);  
    TheCallButton.setOnClickListener(new View.OnClickListener() {  

        public void onClick(View v) {
            final Dialog dialog = new Dialog(ButtonphonecallsActivity.this);
            dialog.setContentView(R.layout.alertphonecall);
            dialog.setTitle("Zohan contact number");

            Button Nosir = (Button) dialog.findViewById(R.id.nobutton);  
            Nosir.setOnClickListener(new View.OnClickListener() {  
                @Override               
                public void onClick(View v) {
                    dialog.cancel();
                    } 
            });   

            Button Yessir = (Button) dialog.findViewById(R.id.yesbutton);
            Yessir.setOnClickListener(new OnClickListener() {
                @Override               
                public void onClick(View v) {
                    dialog.cancel();
                    startActivity(new Intent(ButtonphonecallsActivity.this, makingaphonecall.class));        
                }
            });

            dialog.show();
        }
    });
}
}

The phone call activity

package your.package2.phonecalls;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class makingaphonecall extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    call();
}

private void call() {
try {
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:7578263678"));
    startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
     Log.e("helloandroid dialing example", "Call failed");
}
}

}

android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package2.phonecalls"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:debuggable="true" >

    <activity
        android:name=".ButtonphonecallsActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".makeaphonecall"
        android:label="@string/app_name" >
    </activity>

    </application>

</manifest>

Upvotes: 1

Views: 567

Answers (2)

Shashank Kadne
Shashank Kadne

Reputation: 8101

Why r u making a separate activity to make a phone call? Simply add below two lines into your onclick.

Intent dialer = new Intent(Intent.ACTION_CALL,Uri.parse("tel:7578263678"));
startActivity(dialer);

Upvotes: 1

boogie666
boogie666

Reputation: 650

Button b = (Button) this.findViewById(R.id.callButton);
b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_CALL);
        i.setData(Uri.parse("tel:123456"));
        CallTestAndroidActivity.this.startActivity(i);              
    }
});

This should work just fine. the manifest is just fine the way you have it

Hope this helps.

Upvotes: 0

Related Questions