当我在网络关闭时启动我的应用时,应用停止,但没有消息:annotate is new SSIDAsync().execute();
。
有什么问题?
这是代码:
public static TextView ssid = null; // Global variable
new SSIDAsync().execute(); //onresume ()
public class SSIDAsync extends AsyncTask<Void, Void, String > {
@Override
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
String m_strName = new String();
int m_iNetworkType = activeNetwork.getType();
//WI-FI
if (m_iNetworkType == cm.TYPE_WIFI) {
m_strName = wifiManager.getConnectionInfo().getSSID();
}
//Ethernet
else if (m_iNetworkType == cm.TYPE_ETHERNET) {
m_strName = "connected ethernet";
}
// unableNetwork
else {
m_strName = " unable network ";
}
String newStrName = m_strName.replace('"',' ');
return newStrName;
}
@Override
protected void onPostExecute(String ssidresult){
super.onPostExecute(ssidresult);
if(ssidresult!=null){
ssid.setText(ssidresult);
}
}
}
答案 0 :(得分:0)
将您的doInBackground
更改为:
@Override
protected String doInBackground(Void... params) {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
String m_strName = new String();
int m_iNetworkType = activeNetwork.getType();
//WI-FI
if (m_iNetworkType == cm.TYPE_WIFI) {
m_strName = wifiManager.getConnectionInfo().getSSID();
}//Ethernet
else if (m_iNetworkType == cm.TYPE_ETHERNET) {
m_strName = "connected ethernet";
}//unableNetwork
else {
m_strName = null; // <--- Changed here
}
String newStrName = m_strName.replace('"',' ');
return newStrName;
}