gpt4 book ai didi

java - 创建接听电话通知

转载 作者:行者123 更新时间:2023-12-01 14:48:24 25 4
gpt4 key购买 nike

我正在创建一个应用程序,并且我的代码的某一部分需要在接到电话时创建通知。问题是我无法实现通知,因为我必须传递调用我的号码。以下是我的代码 fragment 。

我的接收者:

@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
CallIntercepterListener callIntercepterListener = new CallIntercepterListener();

telephonyManager.listen(callIntercepterListener, PhoneStateListener.LISTEN_CALL_STATE);

Bundle bundle = intent.getExtras();
String phoneNumber = bundle.getString("incoming_number");
Log.d(TAG, "phoneNumber: " + phoneNumber);
}

和我的听众:

@Override
public void onCallStateChanged(int state, String incomingNumber) {
Log.v(TAG, "Event Call: " + incomingNumber);

switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// Create a notification with de incomingNumber
break;
}
}

最佳答案

你需要监听器做什么?每当电话状态发生变化时,您的接收者就会被调用。

here is a nice article about how to use the BroadcastReceiver.

摘自这篇文章:

package de.vogella.android.receiver.phone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyPhoneReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.w("DEBUG", phoneNumber);
}
}
}
}

关于java - 创建接听电话通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15147786/

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