简体   繁体   中英

How to make calls from android app

Im trying to make an app with buttons to call different phone numbers. I have this: package com.BigTooth.Apps.Recromax;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.Intent;
import android.content.*;
import android.net.Uri;
import android.view.View.OnClickListener;


public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    private void phoneCall1()
    {
        String phoneCallUri = "tel:4078421430";
        Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
        phoneCallIntent.setData(Uri.parse(phoneCallUri));
        startActivity(phoneCallIntent);
    }
    // add button listener
    button.setOnClickListener(new OnClickListener() {
    private void phoneCall()
    {
        String phoneCallUri = "tel:8889807091";
        Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
        phoneCallIntent.setData(Uri.parse(phoneCallUri));
        startActivity(phoneCallIntent);}
    } 
}

In my MainActivity.java file. Its telling me that isn't correct. Please help!!!

button.setOnClickListener is in the wrong place. Also, the onClickListener is a bit wring. Should be:

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.Intent;
import android.content.*;
import android.net.Uri;
import android.view.View.OnClickListener;


public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.youButtonId);
        Button button1 = (Button) findViewById(R.id.youButtonId1);
        // add button listener
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                phoneCall();
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                phoneCall1();
            }
        });
    }
    private void phoneCall()
    {
        String phoneCallUri = "tel:8889807091";
        Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
        phoneCallIntent.setData(Uri.parse(phoneCallUri));
        startActivity(phoneCallIntent);
    }
    private void phoneCall1()
    {
        String phoneCallUri = "tel:4078421430";
        Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
        phoneCallIntent.setData(Uri.parse(phoneCallUri));
        startActivity(phoneCallIntent);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM