如何从Android应用程序拨打电话?我在详细活动中这样做

时间:2016-04-06 10:43:38

标签: android android-studio

我想从我的Android应用程序(来自Detail Activity)拨打电话。我正在使用此代码,它没有任何错误,但当我在我的虚拟设备上运行应用程序时,“呼叫”按钮不起作用。 如果有人得到这个,请帮助。

这是我的详细活动类

    package com.example.sj.dgapps3;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

public class DetailActivity extends AppCompatActivity {
    protected String name;
    Button b;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;
    //protected String pos;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        name = getIntent().getStringExtra(MainActivity.Name);
        TextView textView = (TextView) findViewById(R.id.name);
        textView.setText(name);

        String pos = getIntent().getStringExtra(MainActivity.Position);
        TextView textView1 = (TextView) findViewById(R.id.position);
        textView1.setText(pos);


        String qual = getIntent().getStringExtra(MainActivity.Qualification);
        TextView textView2 = (TextView) findViewById(R.id.qualification1);
        textView2.setText(qual);

        String exp = getIntent().getStringExtra(MainActivity.Expertise);
        TextView textView3 = (TextView) findViewById(R.id.expertise1);
        textView3.setText(exp);

        final String con = getIntent().getStringExtra(MainActivity.Contact);
        TextView textView4 = (TextView) findViewById(R.id.contact1);
        textView4.setText(con);

        String pic1 = getIntent().getStringExtra(MainActivity.Pic);
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        int pic = getResources().getIdentifier(pic1, "drawable", getPackageName());
        imageView.setImageResource(pic);

        b = (Button) findViewById(R.id.button);
        b.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View view) {

                        String phNum = "tel:" + con;
                        Intent myIntent = new Intent(Intent.ACTION_CALL, Uri.parse(phNum));
                        //myIntent.setData(Uri.parse(phNum));
                        if (ActivityCompat.checkSelfPermission(DetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        startActivity(myIntent);
                    }
                });
        /*b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(Intent.ACTION_CALL);
                String phone = "tel:" + con;
                in.setData(Uri.parse(phone));
                startActivity(in);
            }
        });
*/
        /*Button b = (Button) this.findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override*//*
            public void onClick(View v) {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        String phone = "tel:" + con;
        callIntent.setData(Uri.parse(phone));
        startActivity(callIntent);
    }*/

                /*if (ActivityCompat.checkSelfPermission(DetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }*/


        /*});*/

        /*ImageView imageView = (ImageView) findViewById(R.id.imageView);
        int res = getResources().getIdentifier(TeamDetails.getPic(), "drawable", context.getPackageName());
        imageView.setImageResource(res);*/


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Detail Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.sj.dgapps3/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Detail Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.sj.dgapps3/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}

这是我的XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.sj.dgapps3.DetailActivity"
    tools:showIn="@layout/activity_detail">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView"
        android:layout_marginTop="45dp"
        android:layout_marginLeft="40dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/position"
        android:layout_below="@+id/name"
        android:layout_toRightOf="@+id/imageView"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="50dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/qualification"
        android:text="Qualification:"
        android:layout_below="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="43dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/expertise"
        android:text="Expertise:"
        android:layout_below="@+id/qualification"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/contact"
        android:text="Contact:"
        android:layout_below="@+id/expertise"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/qualification1"
        android:layout_alignBottom="@+id/qualification"
        android:layout_toRightOf="@+id/qualification"
        android:layout_marginLeft="50dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/expertise1"
        android:layout_alignBottom="@+id/expertise"
        android:layout_alignLeft="@+id/qualification1"
        android:layout_alignStart="@+id/qualification1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/contact1"
        android:layout_alignBottom="@+id/contact"
        android:layout_alignLeft="@+id/qualification1"
        android:layout_alignStart="@+id/qualification1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Call"
        android:id="@+id/button"
        android:layout_marginBottom="68dp"
        android:onClick="onClickCall"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/imageView"
        android:layout_alignEnd="@+id/imageView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SMS"
        android:id="@+id/button2"
        android:layout_alignTop="@+id/button"
        android:layout_alignLeft="@+id/name"
        android:layout_alignStart="@+id/name" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

尝试在

中删除此行android:onClick="onClickCall"
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Call"
    android:id="@+id/button"
    android:layout_marginBottom="68dp"
    android:onClick="onClickCall"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/imageView"
    android:layout_alignEnd="@+id/imageView" />

答案 1 :(得分:0)

尝试这样的事情......

Intent intent = new Intent(Intent.ACTION_CALL);

    intent.setData(Uri.parse("tel:" + number));

    try {

        startActivity(intent);

    } catch (Exception e) {
        Toast.makeText(MyActivity.this, "Error", Toast.LENGTH_SHORT).show();
    }

答案 2 :(得分:0)

解决方案1: 删除android:onClick =&#34; onClickCall&#34;从你的布局xml按钮

解决方案2:

要从xml使用onclick,你应该声明一个没有Listner

的方法
public void onClickCall(View v) {
    // does something 
}

所以你应该删除这个

 b.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View view) {

                        String phNum = "tel:" + con;
                        Intent myIntent = new Intent(Intent.ACTION_CALL, Uri.parse(phNum));
                        //myIntent.setData(Uri.parse(phNum));
                        if (ActivityCompat.checkSelfPermission(DetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        startActivity(myIntent);
                    }
                });

并执行此操作:

public void onClickCall(View v) {
    String phNum = "tel:" + con;
                        Intent myIntent = new Intent(Intent.ACTION_CALL, Uri.parse(phNum));
                        //myIntent.setData(Uri.parse(phNum));
                        if (ActivityCompat.checkSelfPermission(DetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        startActivity(myIntent);}