简体   繁体   中英

How to make a phone call from a list view in Android Studio?

I have a list of peoples names and each is linked with a certain phone number from a JSON database. I am trying to make it so that every time they are clicked, it will call their number. Any suggestions on how to approach this?

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.ListActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.widget.Button;




import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    private String TAG = MainActivity.class.getSimpleName();
    private ListView lv;



    ArrayList<HashMap<String, String>> legislatorList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        legislatorList = new ArrayList<>();
        lv = (ListView) findViewById(R.id.list);
        lv.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+phone));

                if (ActivityCompat.checkSelfPermission(MainActivity.this,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                startActivity(callIntent);
            }
        });
   new GetLegislators().execute();

}

Request Permission in manifest

uses-permission android:name="android.permission.CALL_PHONE"

For calling use this code on listView itemClick.

private void dialContactPhone(final String phoneNumber) {
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
    startActivity(intent);
}
listViewUsers.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            User user = userList.get(position);
            String newNumber;
            newNumber = user.getPhone();
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + newNumber));// Initiates the Intent
            startActivity(intent);

            return true;
        }
    });

This how you dial from listView, but you can also call directly.

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