gpt4 book ai didi

java - 如何在BroadcastReceiver和main方法之间传递信息

转载 作者:行者123 更新时间:2023-12-01 10:11:35 24 4
gpt4 key购买 nike

在 SMS 应用程序中,我想将 String 值从 BroadcastReceiver 传递到 main 方法。

public class LucesAlarma extends AppCompatActivity {

IntentFilter intentFilterLA;

private BroadcastReceiver intentRecieverLA = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{

final String MENSAJE = intent.getExtras().getString("mensaje");
String NUMERODELMENSAJE = intent.getExtras().getString("numero");
}
};

@Override
protected void onCreate(Bundle savedInstanceState)

{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_luces_alarma);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
intentFilterLA = new IntentFilter();
intentFilterLA.addAction("SMS_RECEIVED_ACTION");
//I want to use the String Mensaje from the BroadcastReceiver here
}

@Override
protected void onResume()
{
registerReceiver(intentRecieverLA, intentFilterLA);
super.onResume();
}

@Override
protected void onPause()
{
unregisterReceiver(intentRecieverLA);
super.onPause();
}

}

这是短信接收器代码

public class ReceptorSMS extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
SmsMessage[]messages=null;
String str = "";
String num = "";
String men = "";
if(bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
assert pdus != null;
messages = new SmsMessage[pdus.length];
for (int i=0 ; i<messages.length;i++)
{
messages[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
num = messages[i].getDisplayOriginatingAddress();
str += "Mensaje de" +messages[i].getOriginatingAddress();
str += ":";
str += messages[i].getMessageBody();
str += "\n";
men = messages[i].getMessageBody();

}
// Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
broadcastIntent.putExtra("mensaje", men);
broadcastIntent.putExtra("numero", num);
context.sendBroadcast(broadcastIntent);
}


}
}

我想知道如何将该字符串放入主要方法中。

最佳答案

我假设您希望在收到任何短信时开始 Activity ,而不是这样做

在您的ReceptorSMS 类中

public void onReceive(Context context, Intent intent)
{.
.
.
Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
Intent broadcastIntent = new Intent();
broadcastIntent .setClassName("<YOUR PACKAGE NAME>", "<YOUR PACKAGE NAME>.LucesAlarma");
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
broadcastIntent.putExtra("mensaje", men);
broadcastIntent.putExtra("numero", num);
context.startActivity(broadcastIntent);
}

之后在onCreate

Intent intent = getIntent();
String sms= intent.getStringExtra("sms");
String men = intent.getStringExtra("mensaje");
String num = intent.getStringExtra("numero");

关于java - 如何在BroadcastReceiver和main方法之间传递信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36096403/

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