ANDROID显示当互联网/ GPS失去连接或没有连接对话框 [英] ANDROID Show a dialog when the Internet/GPS loses connectivity or is not connected
问题描述
我要显示一个闪屏/对话框当互联网或GPS被关闭或无法连接,直到连接良好用户再次无法使用的应用程序。
I want to show a Splash Screen / dialog when the Internet or GPS is down or not connected so the user can't use the app until the connection is good again.
我试着用一些广播接收器,但都出现了问题。其中之一:
I tried with some broadcast receivers, but all had problems. One of them:
public class ConnectivityChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
String status = NetworkUtil.getConnectivityStatusString(context);
Toast.makeText(context, status, Toast.LENGTH_LONG).show();
if(status.equals("Not connected to Internet")) {
Intent splashIntent = new Intent(context, SplashScreen.class);
splashIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(splashIntent);
//I also had a Toast for internet is connected
}
}
}
和使用util类:
public class NetworkUtil {
public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;
public static int getConnectivityStatus(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
return TYPE_WIFI;
if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
return TYPE_MOBILE;
}
return TYPE_NOT_CONNECTED;
}
public static String getConnectivityStatusString(Context context) {
int conn = NetworkUtil.getConnectivityStatus(context);
String status = null;
if (conn == NetworkUtil.TYPE_WIFI) {
status = "Wifi enabled";
} else if (conn == NetworkUtil.TYPE_MOBILE) {
status = "Mobile data enabled";
} else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
status = "Not connected to Internet";
}
return status;
}
}
问题:它不会立刻开始工作。我通常会在连续5敬酒的时候互联网是断开,一段时间后,如果我修改状态的连接敬酒停止展示或之后的一段漫长的等待出现。
Problems: it doesn't work instantly. I usually get 5 toasts in a row when the internet is disconnected and after some time if I modify the status the connection toast stop appearing or appear after a long wait of time.
另外,如何关闭开机画面活动时,互联网是备份?
Also, how to close the Splash Screen activity when the internet is back up?
推荐答案
您可以创建一个服务
类,并使用的TimerTask
与定时
,并给它一个时间来重新开始,这样,你就可以开始你的服务
从接收
您已经拥有并使其(接收
)听 BOOT_COMPLETED
与
you can create a Service
class and use TimerTask
with Timer
and give it a duration to start over, with this you can start your service
from the receiver
that you already have and make it (receiver
) listen to BOOT_COMPLETED
with
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
和指定权限&LT;使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/&GT;
和让你的接收器调用服务类 - (或者你可以在那里检查你的东西 - (考虑我提出的意见))在执行此你的的onReceive
and let your receiver call the service class -(or you can check your stuff there -(take into consideration the comments i made)) by implementing this in your onReceive
boolean isMyServiceRunning(Class<?> serviceClass, Context t) {
ActivityManager manager = (ActivityManager) t.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
,如果使用返回false启动服务
and if it returns false start service using
Intent i = new Intent(context, MyService.class);
context.startService(i);
然后在您的服务做到这一点。
then in your service do this
public class MyService extends Service {
Timer timer; TimerTask task;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
// put here your code for checking
String status = NetworkUtil.getConnectivityStatusString(context);
// i am having nesting problems so put this in a handler() ok
Toast.makeText(context, status, Toast.LENGTH_LONG).show();
if(status.equals("Not connected to Internet")) {
Intent splashIntent = new Intent(context, SplashScreen.class);
splashIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(splashIntent);
}
// handler() code should end here
};
timer.schedule(task, 0, 3000); // repeat every 3 seconds
return START_STICKY;
}
}
其越来越长,关闭闪屏,你可以通过你的活动的一个实例,并使其从服务本身关闭或绑定到服务,或使用本地广播经理
its getting long, to close your splash screen, you can pass an instance of your activity and make it close itself from the service, or bind to the service or use local broadcast manager
也是我的code结束标记可能是错误的,以便纠正它们。
also my code closing tags might be wrong so correct them..
这篇关于ANDROID显示当互联网/ GPS失去连接或没有连接对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!