gpt4 book ai didi

android oreo 在服务中获取来电号码

转载 作者:行者123 更新时间:2023-12-05 04:55:15 25 4
gpt4 key购买 nike

我正在实现代码以在 android Oreo 中获取来电号码。为此,我在服务中使用广播接收器。这是我到目前为止完成的代码。

public class CallService extends Service {
private static final int ID_SERVICE = 101;
BroadcastReceiver brSms;

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
System.out.println("incomingNumber : "+incomingNumber);
Toast.makeText(context,""+incomingNumber,Toast.LENGTH_LONG).show();
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();



IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
brSms = new MyReceiver();
registerReceiver(brSms, filter);




// Create the Foreground Service
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? createNotificationChannel(notificationManager) : "";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(PRIORITY_MIN)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build();

startForeground(ID_SERVICE, notification);
}

@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(NotificationManager notificationManager){
String channelId = "my_service_channelid";
String channelName = "My Foreground Service";
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
channel.setImportance(NotificationManager.IMPORTANCE_NONE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManager.createNotificationChannel(channel);
return channelId;
}
@Override
public void onDestroy() {
unregisterReceiver(brSms);

}
}

代码无效。我尝试了堆栈溢出中给出的各种选项。我在 list 文件中添加了前台服务权限。如何在应用关闭或被用户杀死的情况下获取来电号码?

最佳答案

请尝试这是否适合您。

BroadcastReceiver.java

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state != null) {
if (state == TelephonyManager.EXTRA_STATE_RINGING) {
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (incomingNumber != null) {
Log.d("incomingNumber", incomingNumber);
// do what you want with the incomingNumber
}
}
}
}
}

AndroidManifest.xml

<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.READ_CALL_LOG" />
</intent-filter>
</receiver>

关于android oreo 在服务中获取来电号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65460394/

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