[EDITED]
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.
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>
and specify a permission <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
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;
}
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
also my code closing tags might be wrong so correct them..
STEP 1: Open AndroidManifest.xml and add the broadcast receiver.
<receiver
android:name=".Util.InternetConnectorReceiver"
android:enabled="true">
<intent-filter>
<!-- Intent filters for broadcast receiver -->
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>`
STEP 2: create activity with dialog theme:
<activity
android:name=".Activity.ActivityDialogInternet"
android:theme="@style/AppTheme.Dark.Dialog"></activity>
STEP 3: Make a BroadcastReceiver Class named InternetConnectorReceiver
public class InternetConnectorReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED) ||
intent.getAction().equalsIgnoreCase("android.net.conn.CONNECTIVITY_CHANGE")) {
Intent checkIntent = new Intent(context, ConnectivityCheck.class);
context.startService(checkIntent);
}
}}
STEP 4: Make another service class named ConnectivityCheck :
public class ConnectivityCheck extends Service {
@Override
public void onCreate() {
super.onCreate();
if (!checkConnection()) {
Toast.makeText(context, "off", Toast.LENGTH_LONG).show();
Intent dialogIntent = new Intent(this, ActivityDialogInternet.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}
stopSelf();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private boolean checkConnection() {
Log.i("wudfuyf", "checking started!!!!!!!!!!!!!!!!!!");
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
}}
STEP 5: create a activity called ActivityDialogInternet
public class ActivityDialogInternet extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_internet);
}}
STEP 6: When internet connection is turned off ActivityDialogInternet called and show the dialog:
Try below solution
STEP 1:
Make a Java Class
named NetworkStatus
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetworkStatus
{
Context context;
public NetworkStatus(Context context)
{
this.context = context;
}
public boolean isNetworkOnline()
{
boolean status = false;
try
{
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getNetworkInfo(0);
if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED)
{
status = true;
}
else
{
netInfo = cm.getNetworkInfo(1);
if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED)
{
status = true;
}
}
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return status;
}
}
STEP 2:
Make another Class
named AlertDialogManager
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.provider.Settings;
import com.lmkt.wfm.R;
import com.lmkt.wfm.activities.ActivityMainScreen;
public class AlertDialogManager
{
Context context;
public AlertDialogManager(Context context)
{
this.context = context;
}
public void showAlertDialog(String title, final String message, final Boolean status)
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle(title);
alertDialog.setMessage(message);
if(status != null)
{
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
}
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
if(message.contains("You do not have Internet Connection"))
{
((Activity) context).finish();
}
dialog.dismiss();
}
});
alertDialog.show();
}
}
STEP 3: Usage from your Splash Activity:
AlertDialogManager alert;
NetworkStatus ns;
ns = new NetworkStatus(context);
if(!(ns.isNetworkOnline()))
{
alert = new AlertDialogManager(context);
alert.showAlertDialog("Internet Connection!", "You do not have Internet Connection. "
+ "Please connect to the "
+ "Internet to sync Data From Server...", false);
}