不工作打电话

时间:2017-11-22 13:41:57

标签: android

我正在尝试使用代码中的权限操作调用进行调用当我单击图像按钮它不起作用我的预期我错误的代码或某些行缺少此操作PLZ帮助我..

aboutme.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mydetail">
<ImageButton
        android:id="@+id/callme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_call"
        android:clickable="true"
        android:alpha="0.2"
        android:layout_below="@+id/skill"
        android:layout_alignStart="@+id/skill" />
</RelativeLayout>

androidmainfest.xml:

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

mainactivity.java:

private ImageButton callme;

public void addListenerOnButtonClick(){
        callme = (ImageButton) findViewById(R.id.callme);
        callme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel: 9841******"));
                if (ActivityCompat.checkSelfPermission(MainActivity.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(callIntent);

            }
        });
    }

3 个答案:

答案 0 :(得分:0)

您需要将 URI 传递给 Intent

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telNumber));
startActivity(intent);

不要忘记要求运行时权限&gt; = M

答案 1 :(得分:0)

试试这个

private void makeCall(String num) {
    int hasPerm = getPackageManager().checkPermission(Manifest.permission.CALL_PHONE, getPackageName());
    if (hasPerm == PackageManager.PERMISSION_GRANTED) {
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num));
        startActivity(intent);
    } else {
        //"Permission denied! ask for permission here :- need to change your phone permission in setting."
    }
}

答案 2 :(得分:0)

这样做

    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
    {
        final String[] NECESSARY_PERMISSIONS = new String[] {
                Manifest.permission.CALL_PHONE};

        if (ContextCompat.checkSelfPermission(DialerHomeActivity.this,
                Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {

            startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                    + yourNumber)));

        } else {
            ActivityCompat.requestPermissions(DialerHomeActivity.this,NECESSARY_PERMISSIONS, 123);


        }
    }
    else
    {
        startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                + yourNumber)));
    }

如果版本&gt; = M则首先提供运行时权限