安卓:请电话从服务电话

2023-09-06 11:36:29 作者:没事瞎逼逼

我尝试从一个服务里面打个电话。在code我用的是:

I try to make a phone call from inside a service. The code I use is:

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

和它的工作原理以及从主活动内,但不能从该服务。最后一行出现的错误。它是不可能使一个服务里面打个电话?

and it works well from inside the main activity, but not from the service. The error occurs on the last line. Is it not possible to make a call inside a service?

和我怎么能做出这样一件code运行中的主要活动?

And how could I make this piece of code run on the main activity?

推荐答案

试图为从服务中拨打电话:

try as to make a phone call from inside a service:

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
startActivity(intent);