在 Android 启动画面上显示 Toast
我遇到了以下问题。如果检测到没有网络连接,我希望我的应用程序退出。我的应用程序以启动屏幕启动。如果设备上没有网络连接,是否可以显示启动屏幕,然后显示烤面包。然后终止应用程序
我的启动屏幕代码中有类似这样的内容:
在 onCreate()
ConnectivityManager connectivitymanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
connected = networkinfo != null && networkinfo.isAvailable()
&& networkinfo.isConnected();
Log.v("Network state : ", connected + "");
Thread splashThread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while (waited < 5000) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
// do nothing
} finally {
Looper.prepare();
if (connected == false) {
Toast.makeText(
splashscreenActivity.this,
"No Network Connection is available on device.",
Toast.LENGTH_LONG).show();
finish();
System.exit(0);
} else {
finish();
startActivity(new Intent(splashscreenActivity.this,
mainActivity.class));
}
Looper.loop();
}
}
};
splashThread.start();
1.请参阅我的代码并指导我如何显示该吐司。 2.或者建议我其他更好的方法来做到这一点
谢谢
编辑:
谢谢大家的回复:
我选择了Dharmendra通过闪屏活动显示吐司的方式:
有效的代码是:
if (connected == false) {
splashscreenActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(splashscreenActivity.this,
"No Internet Connection.", 3000).show();
}
});
finish();
} else {
//migrate to main activity from splashscreen
}
I am stuck with following problem.I want my application to exit if it detects no network connection.My application starts with splash screen.Is it possible to show splash screen followed by toast if no network connection is on device.and then terminate the application
I have something like this in my splash screen code :
Inside onCreate()
ConnectivityManager connectivitymanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
connected = networkinfo != null && networkinfo.isAvailable()
&& networkinfo.isConnected();
Log.v("Network state : ", connected + "");
Thread splashThread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while (waited < 5000) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
// do nothing
} finally {
Looper.prepare();
if (connected == false) {
Toast.makeText(
splashscreenActivity.this,
"No Network Connection is available on device.",
Toast.LENGTH_LONG).show();
finish();
System.exit(0);
} else {
finish();
startActivity(new Intent(splashscreenActivity.this,
mainActivity.class));
}
Looper.loop();
}
}
};
splashThread.start();
1.Please see my code and guide me how can i show up that toast.
2.Or suggest me some other better way to do this
Thanks
EDIT :
Thank you everybody for replying :
I opted Dharmendra's way of showing toast via splashscreen activity :
The code that worked for is :
if (connected == false) {
splashscreenActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(splashscreenActivity.this,
"No Internet Connection.", 3000).show();
}
});
finish();
} else {
//migrate to main activity from splashscreen
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以这样做,使用 Handler 。将以下代码放在 else 条件中
或者,我在我的应用程序中执行此操作:
其中 NetworkError 类显示另一个布局,其图像如“无网络、、、、”(或您想要的任何内容)想要展示而不是泼溅...)
You can do it like this, use Handler .Put the following code in the else condition
Or ,I am doing this in my application :
where NetworkError class shows another layout with the image like "No Network,,,,"(or whatever you want to show instead of splash...)
您正在从线程创建并显示 Toast,因此可能不会调用它
您必须编写此代码
You are creating and showing Toast from Thread so it may be not called
You have to write this code
只需添加一个 if else 块。(使用您的连接网络信息)
Just add a if else block.(using ur connection network info)
如果您不希望捕获任何异常,则可以使用finally 块。在您的代码中,将所有代码放在 else 循环中,如果时间经过 5000,则转到 else 语句并执行它。如果你要添加catch,你可以最后删除。
A finally block is used if you do not wish to catch any exceptions. In your code, place all your code in a else loop, If time of 5000 lapses then, go to else statement and execute it. If you are adding catch you can remove finally.