gpt4 book ai didi

java - 在 Android 中调用电话

转载 作者:行者123 更新时间:2023-12-01 22:34:57 25 4
gpt4 key购买 nike

我想知道如何在 Android 中不使用 Intents 调用电话。

我知道可以使用 Intent 来完成,但我搜索了 developer.android.com并且没有任何关于此主题的内容。

假设我想为 Android 编写一个新的拨号器。我该怎么做?

提前致谢

最佳答案

调用电话,

private void performDial(String numberString) {
if (!numberString.equals("")) {
Uri number = Uri.parse("tel:" + numberString);
Intent dial = new Intent(Intent.ACTION_CALL, number);
startActivity(dial);
}

}

将此权限添加到 list 中。

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

引用this .

要显示自定义拨号器屏幕,请编写拨出电话的接收者

public class OutgoingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
//this will receive the outgoing call

}
}

将这些添加到 list

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

<receiver android:name=.OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>

要在拨号器应用程序启动时在其顶部显示 View , 在广播接收器中使用此代码

 private WindowManager wm;
private static LinearLayout ly1;
private WindowManager.LayoutParams params1;

// onReceive function of the Broadcast Receiver
public void onReceive(Context arg0, Intent arg1) {
String state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE);

// Adds a view on top of the dialer app when it launches.
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
params1 = new WindowManager.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);

params1.height = 75;
params1.width = 512;
params1.x = 265;
params1.y = 400;
params1.format = PixelFormat.TRANSLUCENT;

ly1 = new LinearLayout(context);
ly1.setBackgroundColor(Color.BLACK);
ly1.setOrientation(LinearLayout.VERTICAL);

wm.addView(ly1, params1);
}

// To remove the view once the dialer app is closed.
if(arg1.getAction().equals("android.intent.action.PHONE_STATE")){
String state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if(ly1!=null)
{
wm.removeView(ly1);
ly1 = null;
}
}
}
}

注意:上面的示例生成了一个具有黑色背景布局的 View ,其尺寸如上所示。您可以自由地在此 View 中添加任何布局。例如,要在 View 中包含布局,您可以修改上面的代码以包含以下代码。

 ly1 = new LinearLayout(getApplicationContext());
ly1.setOrientation(LinearLayout.HORIZONTAL);


View hiddenInfo = getLayoutInflater().inflate(R.layout.layout1, ly1, false);
ly1.addView(hiddenInfo);

wm.addView(ly1, params1);

在您的 list 中,您需要包含以下权限。

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<action android:name="android.intent.action.PHONE_STATE" /

注意:您还可以从 BroadcastReceiver 调用 Activity 而不是膨胀窗口。为此目的使用以下代码。

    new Handler().postDelayed(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
Intent intent = new Intent(context, CustomDialerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}, 2000);

我不确定您的自定义 GUI 是否始终位于默认 GUI 之上,因为系统广播接收器和您的接收器都试图在屏幕顶部显示其 GUI。我们不确定首先调用哪个,但是让 GUI 出现在屏幕顶部的一项棘手工作是,当电话响铃 1-2 秒后调用您的 Activity 时。我使用了 handler 为此。

关于java - 在 Android 中调用电话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26993055/

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