如何从后台服务拨打电话?

时间:2017-12-21 15:05:39

标签: android service background call

我是新来的android请帮助。
我尝试使用这段代码......

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

没有工作:它突然停止了 我提到了必要的权限

1 个答案:

答案 0 :(得分:0)

请尝试以下代码。

        new Handler().post(new Runnable() {
            @Override
            public void run() {
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);
context.startActivity(intent);
            }
        });

您将从服务onStart()方法获取上下文。 Context的InPlace可以使用getApplicationContext()