带有呼叫电子邮件和短信功能的弹出窗口

问题描述 投票:1回答:1

我是Android应用程序开发的初学者..我现在已经制作了一个应用程序,其中有一个弹出窗口显示一个人的姓名和详细信息..我已经添加了3个按钮,如CALL,短信,电子邮件..我去了呼叫活动,但它不起作用。没有错误仍有呼叫按钮没有拨打电话..我在新项目中试过的相同代码,它运行良好..但是当我在弹出窗口上执行此操作时,呼叫无效...请帮助我

public class popupinv extends AppCompatActivity {

    public Button b;
    public void init(){
    }

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

        b= (Button) findViewById(R.id.call);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent callIntent=new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:123456));
                startActivity(callIntent);

            }
        });
    }
}
java android
1个回答
0
投票

你似乎忘了关闭字符串的双引号。

callIntent.setData(Uri.parse("tel:123456"));

检查您在清单中是否拥有良好的权限。

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

编辑:找到它不起作用的原因here

因此,即使您在清单中添加权限,您仍可能具有SecurityException:权限拒绝。

现在没有得到你的许可的重要原因是因为你的项目有23或更高的targetSdkVersion,你要求的许可是“危险的”。在Android 6.0中,这包括:

  • CALL_PHONE
  • [...]

这意味着,如果您的targetSdkVersion> = 23,那么您必须在清单中设置权限并在运行时检查权限。

以下是在运行时检查权限的代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b;
        b = (Button) findViewById(R.id.button);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkCallPermission();
            }
        });
    }

    protected void call() {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:123456"));
        startActivity(callIntent);
    }

    final int PERMISSION_REQUEST_CALL = 1;
    protected void checkCallPermission() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

            // Permission is not granted
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.CALL_PHONE},
                        PERMISSION_REQUEST_CALL);

            }
        } else {
            // Permission has already been granted
            call();
        }
        return;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CALL: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted, yay!
                    call();
                } else {

                    // permission denied!
                }
                return;
            }
            // other 'case' lines to check for other
            // permissions this app might request.
        }
    }

有关它的更多信息,请查看手册:https://developer.android.com/training/permissions/requesting.html

解决方法:如果您不想在运行时检查权限,则可以使用较少危险的权限:

    Intent callIntent = new Intent(Intent.ACTION_DIAL);
    callIntent.setData(Uri.parse("tel:123456"));
    startActivity(callIntent);

使用此代码,应用程序将不会调用该号码,它将打开带有预填充号码的拨号器。

© www.soinside.com 2019 - 2024. All rights reserved.