当Android应用程序中的互联网连接不可用时显示警报

编程入门 行业动态 更新时间:2024-10-27 18:32:39
本文介绍了当Android应用程序中的互联网连接不可用时显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的应用程序中,数据来自互联网,我正在尝试创建一个函数来检查互联网连接是否可用,如果不可用,它会发出警报消息,表明没有可用的互联网连接.我正在使用以下代码.但它不起作用.

In my application data comes from internet and I am trying to create a function that checks if a internet connection is available or not and if it isn't, it gives an alert messege that no internet connection available. i am using following code. but its not working.

public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main1); if (isOnline()) { // my code } else { Hotgames4meActivity1.this.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); try { AlertDialog alertDialog = new AlertDialog.Builder(Hotgames4meActivity1.this).create(); alertDialog.setTitle("Info"); alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try again"); //alertDialog.setIcon(R.drawable.alerticon); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); alertDialog.show(); } catch(Exception e) { //Log.d(Constants.TAG, "Show Dialog: "+e.getMessage()); } } }

推荐答案

public void onCreate(Bundle obj) { super.onCreate(obj) setContextView(layout); if (isOnline()) { //do whatever you want to do } else { try { AlertDialog alertDialog = new AlertDialog.Builder(con).create(); alertDialog.setTitle("Info"); alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try again"); alertDialog.setIcon(android.R.drawable.ic_dialog_alert); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); alertDialog.show(); } catch (Exception e) { Log.d(Constants.TAG, "Show Dialog: " + e.getMessage()); } } }

更多推荐

当Android应用程序中的互联网连接不可用时显示警报

本文发布于:2023-11-12 19:27:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582323.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:互联网   警报   应用程序   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!