gpt4 book ai didi

android - 如何在不使用 onCreate() 的情况下将数据从 BroadcastReceiver 传递到 Activity

转载 作者:太空宇宙 更新时间:2023-11-03 11:47:38 28 4
gpt4 key购买 nike

关于将数据从 BroadcastReceiver 传递到 Activity,我遇到了一个严重的问题。让我仔细看看我的问题。我有一个用于接收来电的类 PhoneStateReceiver extends BroadcastReceiver

public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){

}
}
catch (Exception e){
e.printStackTrace();
}
}
}

来电将被发送到一个名为ReceiverActivityActivityReceiverActivity 接收来电并通过套接字连接将其发送到服务器。套接字连接在 onCreate 函数中初始化。我用谷歌搜索并找到了将数据从 BroadcastReceiver 传递到 Activity 的服务器方式。常见的方式是通过putExtra函数发送数据,调用startActivity。但是,该方法会再次调用onCreate,然后连接套接字,重新绘制UI。因此,这对我的情况没有帮助。

在我的目标中,如果手机收到来电,它将把来电发送到ReceiverActivityReceiverActivity 接收消息并调用发送函数。哪种方法最好?谢谢

将数据从 BroadcastReceiver 传递到 ReceiverActivity 的常用方法如下所示

PhoneStateReceiver 类中:

Intent intent_phonenum = new Intent(context, ReceiverActivity.class);
intent_phonenum.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent_phonenum.putExtra("phone_num", incomingNumber);
context.startActivity(intent_phonenum);

ReceiverActivity 类中:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connect_socket();
Intent intent = getIntent();
phone_num = intent.getStringExtra("phone_num");
send(phone_num);
}

最佳答案

简单,无需任何第三方库。

确保 BroadcastReceiver 必须在 OnPause() 上注册和注销。

你必须做两件事

在您的 Activity 中注册一个接收器,如下所示。

public class MainActivity extends Activity {

Context context;
BroadcastReceiver updateUIReciver;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;

IntentFilter filter = new IntentFilter();
filter.addAction("service.to.activity.transfer");
updateUIReciver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//UI update here
if (intent != null)
Toast.makeText(context, intent.getStringExtra("number").toString(), Toast.LENGTH_LONG).show();
}
};
registerReceiver(updateUIReciver, filter);
}
}

现在为您服务

public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Intent local = new Intent();
local.setAction("service.to.activity.transfer");
local.putExtra("number", incomingNumber);
context.sendBroadcast(local);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

关于android - 如何在不使用 onCreate() 的情况下将数据从 BroadcastReceiver 传递到 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954261/

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