gpt4 book ai didi

java - 如何为android 3.0以上版本定义广播接收器?

转载 作者:行者123 更新时间:2023-12-01 13:55:50 25 4
gpt4 key购买 nike

这是我的phonestate 类,我在其中检查手机状态更改

public class CustomPhoneStateListener extends PhoneStateListener 
{

private static final String TAG = "CustomPhoneStateListener";

Context context; //Context to make Toast if required
public CustomPhoneStateListener(Context context)
{
super();
this.context = context;
}
public void onCallStateChange(int state, String incomingNumber)
{

Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
Log.v(TAG, incomingNumber);

switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "RINGING");
Toast.makeText(context, "Incoming number :"+incomingNumber, Toast.LENGTH_LONG).show();
break;
} }

这是我的广播接收器类,其中与广播连接

    public class CustomBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "CustomBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();

telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);


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

我有一些在java文件中定义 Intent 的想法,bcz Android版本 3.0以上的需求 定义 Intent 。

Intent intent = new Intent();
intent.setAction("com.example.SendBroadcast");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);

我不知道如何以及在哪里定义这部分代码是否在 广播类或phonestate类。

最佳答案

显然!!将此代码调用到自定义 PhoneStateListener 类中,否则永远不会调用广播。

关于java - 如何为android 3.0以上版本定义广播接收器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19634329/

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