我将广播接收器定义为内部类。我不想从接收器到外部类获取对象,如下所示:
class External {
public object ;
class Inner {
object = something ;
getObject () { return object; }
}//end Inner class
method (){
Inner inn = new Inner();
inn.getObject(); //here i would like to get the object from inner class
}
}
但它为我做了工作,我得到一个空的对象,这是我的真实代码:
public class Wifi extends Activity implements OnClickListener{
/** Called when the activity is first created. */
WifiManager wifi;
Button enab;
String resultsString ;
String[] myStringArray;
public class Receiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){
List<ScanResult> results = wifi.getScanResults();
resultsString = results.toString() ;
//Log.i("resultsString",resultsString);
List<WifiConfiguration> resultsConf = wifi.getConfiguredNetworks();
}
}
/***** I would like to get resultsString *******/
public String getResult(){
return resultsString ;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi);
ConnectivityManager cxMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
Receiver receiver = new Receiver();
/***** I would like to get resultsString in this method but I'm getting a null **/
registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
registerReceiver(receiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.i("receiver.getResult()",receiver.getResult()+"");
}
}
这是我的logCat
03-27 12:28:51.947: I/receiver.getResult()(24524): null
谢谢。我有人会帮助我:)。
答案 0 :(得分:2)
试试这个..
我猜您在下面的行中遇到错误
Log.i("receiver.getResult()",receiver.getResult());
可能接收器 null ,以便您可以像下面一样检查添加""+
Log.i("receiver.getResult()",""+receiver.getResult());
修改强>
IntentFilter intentFilter =new IntentFilter(Receiver.ACTION_TEXT_CAPITALIZED);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
/* create new broadcast receiver*/
Receiver receiver = new Receiver();
registerReceiver(receiver, intentFilter);
在Receiver类中添加
public final static String ACTION_TEXT_CAPITALIZED= "com.android.guide.exampleintentservice.intent.action.ACTION_TEXT_CAPITALIZED";
您可以发送以下信息
Intent resultBroadCastIntent =new Intent();
resultBroadCastIntent.setAction(Receiver.ACTION_TEXT_CAPITALIZED);
resultBroadCastIntent.setAction(ConnectivityManager.CONNECTIVITY_ACTION);
resultBroadCastIntent.setAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
resultBroadCastIntent.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(resultBroadCastIntent);