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

时间:2018-03-03 07:01:58

标签: java android

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

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);

            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

检查您在清单中是否获得了良好的许可。

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

修改:找到了它无效的原因here

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

  

现在没有得到你的许可的重要原因是因为   您的项目的targetSdkVersion为23或更高,并且   您要求的许可是&#34;危险&#34;。在Android 6.0中,   这包括:

     
      
  • CALL_PHONE
  •   
  • [...]
  •   

这意味着,如果您的targetSdkVersion&gt; = 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);

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