我想从Intent方法中提取第一个参数。我怎么能这样做?
示例,BroadcastCodes.LOGIN_BROADCAST只是字符串参数。
Utilities.sendLocalBroadcastActivity(new Intent(BroadcastCodes.LOGIN_BROADCAST, null,
ServerConnectionManager.this, LoginActivity.class));
在MainActivity类中将是:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressInfo = findViewById(R.id.splash_screen_progress_info);
Utilities.startService(this, StickyService.class);
localBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// How should I extract the first parameter to compare and call other function.
String broadcast = intent.getStringExtra("?");
logger.info("Received broadcast: " + broadcast);
if(?)
{
handleConnectBroadcast(broadcast);
}
}
};
connectionMessagesHandler = new ConnectionMessagesHandler();
}
答案 0 :(得分:1)
如果我理解,你想获取第一个参数,即BroadcastCodes.LOGIN_BROADCAST
这是动作参数,
使用intent.getAction();
希望它有所帮助!
答案 1 :(得分:0)
这是一个如何提取意图数据的例子:
Intent intent = getIntent();
String age= intent.getStringExtra("age");
String name = intent.getStringExtra("name");
在您的代码中,它将是:
public void onReceive(Context context, Intent intent) {
String broadcast = intent.getStringExtra("YOUR_EXTRA_NAME");
在您的评论案例中,您应该添加:
if (intent.getAction().equals(Constants.LOGIN_BROADCAST)){
//code
}
答案 2 :(得分:0)
Intent intent = new Intent(ABC.this,XYZ.class);
intent.putExtra("any_key_name","any_value");
startActivity(intent)