在 Mainactivity.java 中,我从 wifi 获取信号强度 5 次并附加到服务中。我通过 Intent 传递数组列表。我通过另一个文件中的 Intent 获取值。现在,我想将此值传递给 java 桌面服务器。我在 Intent 的 onStart 中调用 asynctask 方法。我没有收到任何错误。但是没有显示任何价值。
当我记录 cat 时,会显示该值。
你能告诉我哪里错了吗
主要活动.Java
public class MainActivity extends Activity {
TextView mTextView;
private WifiManager wifiManager;
int count =0; String data ="";
ArrayList<String> arr = new ArrayList<String>();
private String messsage;
private static final IntentFilter FILTER = new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text_id);
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
registerReceiver(scanReceiver, FILTER);
wifiManager.startScan();
}
@Override
public void onStart() {
super.onStart();
// Register the scan receiver
registerReceiver(scanReceiver, FILTER);
}
@Override
public void onStop() {
super.onStop();
// Unregister the receiver
unregisterReceiver(scanReceiver);
}
public void onClickRefresh(View v) {
count=0;
wifiManager.startScan();
}
BroadcastReceiver scanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(count<5){
final StringBuilder sb = new StringBuilder();
List<ScanResult> wifiScanList = wifiManager.getScanResults();
for (ScanResult result : wifiScanList) {
if (result.SSID.equals("Dal")) {
sb.append(""+result.level);
}
}
mTextView.setText("caled" +count + sb + " length is" + sb.length());
arr.add(sb.toString());
count++;
wifiManager.startScan();
}
else if (count==5)
{
Intent i= new Intent(context, javaServiceClass.class);
i.putExtra("stock_list", arr);
context.startService(i);
}
}
};
JavaServiceClass.java
public class javaServiceClass extends Service {
public void onStart(Intent intent, int startId) {
ArrayList<String> stock_list = (ArrayList<String>) intent.getExtras().get("stock_list");
System.out.println(stock_list.get(1) );
messsage = stock_list.get(0);
new Asynctask1().execute(messsage);
}
public class Asynctask1 extends AsyncTask<String, Void, Void> {
private PrintWriter printwriter;
protected Void doInBackground(String... messages) {
final String IP_ADDRESS = "134.190.162.165";
final int DEST_PORT = 4444;
if(messsage !=null){
//Socket client = null;
try {
Socket client = new Socket(IP_ADDRESS, DEST_PORT); // connect to server
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
else
{ System.out.println(messsage);}
return null;