gpt4 book ai didi

java - 如果未找到结果,则显示 Toast

转载 作者:行者123 更新时间:2023-12-01 09:15:28 24 4
gpt4 key购买 nike

快速说明我正在尝试做什么。我正在创建一个扫描寻找特定 Wi-Fi 连接的脚本。如果找到,则返回带有字符串的StartingActivity。

但是,如果它扫描了所有连接但没有找到正确的连接,我该如何让它显示 Toast。因为现在,它只是坐在那里,什么也不做。我必须向用户解释它什么也没发现。

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();
}
}
}

更新:

我最好的猜测是设置一个计时器,如果它在那里停留 5 秒,则显示一个 toast(这意味着它没有改变 Activity ,所以没有发现任何结果)。但感觉这不是最好的办法。

最佳答案

如果 results.size 为零 (0),则在下一行之后显示您的 toast。

final int Amount = results.size();

这可能就足够了。 [我不确定你是否可以从广播接收器中扔 toast 。我几乎没有和他们一起工作过。]

干杯!

关于java - 如果未找到结果,则显示 Toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40573573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com