我在IntentService类上实现ResultReceiver时遇到了困难,该类会扫描网络上的可用设备。 (到目前为止无视我的代码的可怕设计)我似乎甚至不能让我的resultReceiver传回一个测试字符串,日志会选择我发送的实例,但是我得到一个nullPointerException我尝试访问传入的字符串。
以下是我的Intent Service类的相关部分:
public class NetHelper extends IntentService{
private ConnectivityManager cm;
private WifiManager manager;
private WifiInfo connectionInfo;
private NetworkInfo activeNetwork;
public Context myContext;
public ResultReceiver receiver;
public Bundle b;
SharedPreferences netprefs;
SharedPreferences.Editor editor;
@Override
protected void onHandleIntent(@Nullable Intent intent) {
//if intent-int == 0: run netsniff
//if intent-int == 1: run getNetInfo
int OPR = intent.getIntExtra("OPR", 0); //0 is default value...
//result receiver for callback use
Bundle params = intent.getExtras();
receiver = params.getParcelable("receiverTag");
b = new Bundle();
//long running operation is netsniff
listDevices = new ArrayList<Devices>();
myContext = getApplicationContext();
//application based storing of values
netprefs = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
editor = netprefs.edit();
cm = (ConnectivityManager) myContext.getSystemService(Context.CONNECTIVITY_SERVICE);
activeNetwork = cm.getActiveNetworkInfo();
manager = (WifiManager) myContext.getSystemService(Context.WIFI_SERVICE);
connectionInfo = manager.getConnectionInfo();
if (OPR == 0) {
try {
netSniff();
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, e.toString());
}
}
}
我的函数中的片段发送了一个新的包:
public void netSniff() throws IOException {
Log.d(TAG, "begin sniffing network on network: "+ NET_IP);
Log.d(TAG, "Active Network: " + String.valueOf(activeNetwork));
Log.d(TAG, "IP_ADDR: " + String.valueOf(MYNET_IP));
b.putString("RESULT_RECEIVED", "TESTING23748W3458763298457Y34");
receiver.send(0, b);
myResultReceiver类:
public class myResultReceiver extends ResultReceiver{
private Receiver mReceiver;
public myResultReceiver(Handler handler) {
super(handler);
}
public interface Receiver {
public void onReceiveResult(int resultCode, Bundle resultData);
}
public void setReceiver(Receiver receiver) {
mReceiver = receiver;
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (mReceiver != null) {
mReceiver.onReceiveResult(resultCode, resultData);
}
}
最后,这是我对片段的要求:
public class devicesFragment extends Fragment implements myResultReceiver.Receiver {
public myResultReceiver mReceiver;
//discover network info, and get a list of devices
public void launchNetworkSniffer(int opr) {
Intent serviceIntent = new Intent(getActivity(), NetHelper.class);
//setup resultReceiver for service callbacks
mReceiver = new myResultReceiver(new Handler());
mReceiver.setReceiver(this);
serviceIntent.putExtra("receiverTag", mReceiver);
serviceIntent.putExtra("OPR", opr);
getActivity().startService(serviceIntent);
}
//receiver implemented methods
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
Log.d("DATA_RECEIVED******", "processing...");
String newdevjson = resultData.getString("RECEIVED_RESULT");
Log.d("DATA_RECEIVED", newdevjson);
Devices newd = new Gson().fromJson(newdevjson, Devices.class);
devlist.add(newd);
for (Devices i : devlist) {
Log.d("ACTIVE_DEV_COUNT", i.getIp());
}
}
我的片段onResultReceived()
中的第一个日志正在显示,但是当我为resultData.getString()
分配nullPointerReference时,它会崩溃。
我相信这个事实意味着我已经正确地实例化了一切,所以为什么根本没有传回任何东西?