gpt4 book ai didi

java - 带有调用电子邮件和短信功能的弹出窗口

转载 作者:太空宇宙 更新时间:2023-11-04 10:40:08 25 4
gpt4 key购买 nike

我是android应用程序开发的初学者..我现在制作了一个应用程序,并且其中有一个弹出窗口,其中显示了人员姓名和详细信息..我在其中添加了3个按钮,如CALL,SMS,EMAIL ..通话活动,但无法正常工作。没有错误,仍然有呼叫按钮没有拨打电话..我在一个新项目中尝试过的相同代码,在那儿工作正常..但是当我在该弹出窗口中执行此操作时,呼叫无法正常工作...请帮助我

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

}
});
}
}

最佳答案

看来您忘记了双引号来关闭字符串。

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


支票在清单中具有良好的权限。

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


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

因此,即使在清单中添加权限,您仍然可能会遇到SecurityException:权限被拒绝。


  如今没有得到您的许可的主要原因是
  您的项目的 targetSdkVersion为23或更高,并且
  您所请求的许可是“危险的”。在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);


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

关于java - 带有调用电子邮件和短信功能的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49081616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com