Java 将值从broadcastreceiver类传递到wifi direct中的主活动
我想将WiFiDirectBroadcastReceiver中字符串变量result中的值发送到WiFiDirectActivity,并尝试按中所示进行操作 但是,当我运行应用程序时,它一直在搜索同龄人。当我评论这些添加的行(WiFiDirectBroadcastReceiver的最后3行)时,它工作正常 添加了WiFiDirectBroadcastReceiver中的onReceive函数和WiFiDirectActivity中的onResume()代码 如何将该值传递给WiFiDirectActivity?我错过了什么 WiFiDirectBroadcastReceiver.javaJava 将值从broadcastreceiver类传递到wifi direct中的主活动,java,android,eclipse,wifi-direct,wifip2p,Java,Android,Eclipse,Wifi Direct,Wifip2p,我想将WiFiDirectBroadcastReceiver中字符串变量result中的值发送到WiFiDirectActivity,并尝试按中所示进行操作 但是,当我运行应用程序时,它一直在搜索同龄人。当我评论这些添加的行(WiFiDirectBroadcastReceiver的最后3行)时,它工作正常 添加了WiFiDirectBroadcastReceiver中的onReceive函数和WiFiDirectActivity中的onResume()代码 如何将该值传递给WiFiDirectA
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//if (MyIntentService.ACTION_MyIntentService.equals(action))
// {
//result = intent.getStringExtra(MyIntentService.EXTRA_KEY_OUT);
result = "hello";
//}
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// UI update to indicate wifi p2p status.
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
// Wifi Direct mode is enabled
activity.setIsWifiP2pEnabled(true);
} else {
activity.setIsWifiP2pEnabled(false);
activity.resetData();
}
Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// request available peers from the wifi p2p manager. This is an
// asynchronous call and the calling activity is notified with a
// callback on PeerListListener.onPeersAvailable()
if (manager != null) {
manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager()
.findFragmentById(R.id.frag_list));
}
Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
if (manager == null) {
return;
}
NetworkInfo networkInfo = (NetworkInfo) intent
.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
// we are connected with the other device, request connection
// info to find group owner IP
DeviceDetailFragment fragment = (DeviceDetailFragment) activity
.getFragmentManager().findFragmentById(R.id.frag_detail);
manager.requestConnectionInfo(channel, fragment);
//fragment.peerCountInfo=result;
//fragment.peerCt = peerCountFromDlist;
//fragment.peerNm = peerNameFromDlist;
} else {
// It's a disconnect
activity.resetData();
}
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
.findFragmentById(R.id.frag_list);
fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
//peerCountFromDlist = fragment.peerTot;
//peerNameFromDlist = fragment.deviceid;
}
intent.putExtra("message",result);
intent.setClass(context, WiFiDirectActivity.class);
context.startActivity(intent);
}
WiFiDirectActivity.java
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//if (MyIntentService.ACTION_MyIntentService.equals(action))
// {
//result = intent.getStringExtra(MyIntentService.EXTRA_KEY_OUT);
result = "hello";
//}
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// UI update to indicate wifi p2p status.
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
// Wifi Direct mode is enabled
activity.setIsWifiP2pEnabled(true);
} else {
activity.setIsWifiP2pEnabled(false);
activity.resetData();
}
Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// request available peers from the wifi p2p manager. This is an
// asynchronous call and the calling activity is notified with a
// callback on PeerListListener.onPeersAvailable()
if (manager != null) {
manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager()
.findFragmentById(R.id.frag_list));
}
Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
if (manager == null) {
return;
}
NetworkInfo networkInfo = (NetworkInfo) intent
.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
// we are connected with the other device, request connection
// info to find group owner IP
DeviceDetailFragment fragment = (DeviceDetailFragment) activity
.getFragmentManager().findFragmentById(R.id.frag_detail);
manager.requestConnectionInfo(channel, fragment);
//fragment.peerCountInfo=result;
//fragment.peerCt = peerCountFromDlist;
//fragment.peerNm = peerNameFromDlist;
} else {
// It's a disconnect
activity.resetData();
}
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
.findFragmentById(R.id.frag_list);
fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
//peerCountFromDlist = fragment.peerTot;
//peerNameFromDlist = fragment.deviceid;
}
intent.putExtra("message",result);
intent.setClass(context, WiFiDirectActivity.class);
context.startActivity(intent);
}
多亏了Mike M,我找到了从BroadcastReceiver向主要活动发送一些数据的简单方法。(同样的方法setIsWifiP2pEnabled(boolean-isWifiP2pEnabled)方法有效) 发送
“消息”
并将其检索到str
变量
在main活动中创建公共方法:
public void setResult(String result){
str = result;
}
然后在广播接收器中调用它:
activity.setResult(“消息”)代码>你们试过共享参考资料吗?嘿,伙计们,谢谢你们的回答!事实上,我尝试了我熟悉的东西,因为我对android有点陌生@Itzik什么是SharedReferences?@Mike我现在可以理解检索部分了,对不起,但是你把信息放在“开始活动的意图”中是什么意思?@Mike哦,谢谢,我现在明白了!它就在那个演示代码中(setIsWifiP2pEnabled也是这样做的),我没有看到它。我觉得现在问这个问题很愚蠢lol对不起你的时间,非常感谢你指出这一点!如果你愿意的话,可以加上它作为答案,然后我会把它标记为答案:)@Mike ohh很好,你不知怎么回答了,谢谢你!我补充了答案。再次感谢。