[英]Show Toast if no results were found
Quickly about what I am trying to do. 快速了解我要做什么。 I am creating a script that scans looks for certain Wi-Fi connection. 我正在创建一个扫描脚本,以查找某些Wi-Fi连接。 If it finds it, returns to StartingActivity with String. 如果找到它,则返回带有字符串的StartingActivity。
But how do I make it display Toast if it scanned all connections and didn't found the right one. 但是,如果它扫描了所有连接但没有找到正确的连接,我如何使它显示Toast。 Because right now, it just sits there and does nothing. 因为现在,它只是坐在那里,什么也不做。 And I have to explain user that it found nothing. 并且我必须向用户解释说它什么也没发现。
Button btnHit;
TextView txtJson;
String urlfinal;
String fssid;
Intent intent;
private static final String TAG = "My Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_connection);
btnHit = (Button) findViewById(R.id.request);
txtJson = (TextView) findViewById(R.id.results);
if (Build.VERSION.SDK_INT > 22) {
final String CoarseLocation = Manifest.permission.ACCESS_COARSE_LOCATION;
final String AccessWifi = Manifest.permission.ACCESS_WIFI_STATE;
final String ChangeWifi = Manifest.permission.CHANGE_WIFI_STATE;
if (checkSelfPermission(CoarseLocation) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 123);
}
if (checkSelfPermission(AccessWifi) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_WIFI_STATE}, 123);
}
if (checkSelfPermission(ChangeWifi) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE}, 123);
}
}
LocationManager lman = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
boolean network_enabled = false;
try
{
network_enabled = lman.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {}
if (!network_enabled)
{
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
}
btnHit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final WifiManager mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
mWifiManager.setWifiEnabled(true);
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
List<ScanResult> results = mWifiManager.getScanResults();
final int Amount = results.size();
int num = 0;
while (num < Amount)
{
Log.v(TAG, "SSID = " + results.get(num).SSID);
num = num+1;
}
int dis = 0;
String res = "Results:\n\n\n";
while (dis < Amount)
{
res = res + results.get(dis).SSID + "\n\n";
new JsonTask().execute(results.get(dis).SSID);
dis = dis+1;
}
}
}, filter);
mWifiManager.startScan();
}
});
}
private class JsonTask extends AsyncTask<String, String, String> {
ProgressDialog pd;
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(FindConnection.this);
pd.setMessage("Please wait...");
pd.setCancelable(false);
pd.show();
}
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
fssid = params[0];
urlfinal = "http://myurl?ssid=" + fssid;
URL url = new URL(urlfinal);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line+"\n");
if (!line.equals("null"))
{
String NetworkSSID = fssid;
String NetworkPass = line;
WifiConfiguration config = new WifiConfiguration();
config.SSID = "\"" + NetworkSSID + "\"";
config.preSharedKey = "\"" + NetworkPass + "\"";
WifiManager wifiman = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
wifiman.addNetwork(config);
List<WifiConfiguration> list = wifiman.getConfiguredNetworks();
for ( WifiConfiguration i : list )
{
if (i.SSID != null && i.SSID.equals("\"" + NetworkSSID + "\""))
{
wifiman.disconnect();
wifiman.enableNetwork(i.networkId, true);
wifiman.reconnect();
break;
}
Intent intent1 = new Intent(FindConnection.this, StartingPage.class);
intent1.putExtra("ssid_name", fssid);
startActivity(intent1);
}
}
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pd.isShowing()){
pd.dismiss();
}
}
}
UPDATE: 更新:
My best guess how to do that is set a timer and if it sits there for 5 seconds then display a toast (that means it hasn't changed activity, so found no results). 我最好的猜测是如何设置一个计时器,如果它在那儿坐了5秒钟,然后显示一个吐司(这意味着它没有改变活动,因此没有发现结果)。 But it feel that it is not the best way to do it. 但它感觉这不是最好的方法。
Show your toast after the following line, if results.size is zero (0). 如果result.size为零(0),请在下一行显示您的吐司。
final int Amount = results.size();
That is probably suffice. 这可能就足够了。 [I am not sure if you can throw toast from a broadcast receiver. [我不确定您是否可以从广播接收器吐司。 I have barely worked with them.] 我几乎没有与他们合作过。]
Cheers! 干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.