1

I have a broadcast receiver which receives for internet connectivity..and as soon as it doesn't find any connection it opens up my splash activity saying "NO INTERNET CONNECTION"....till now everything is ok but when user put the application into background using device home button and then off the internet connection the splash activity comes to foreground while the app was running in background. I don't want this to happen the splash activity should open but in the background only.

  @Override
  public void onReceive(Context context, Intent intent) {


    //  SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);

    boolean isNetworkDown = intent.getBooleanExtra(
        ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);    

    if (isNetworkDown) {
      Log.d(TAG, "onReceive: NOT connected, stopping UpdaterService");
      Intent myintent=new Intent(context,NoConnectivity.class);
      myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(myintent);

    } else 

    {

      Log.d(TAG, "onReceive: connected, starting UpdaterService");
      NoConnectivity.h.sendEmptyMessage(0);      


    }
  }
Kara
  • 6,115
  • 16
  • 50
  • 57
Ishant
  • 53
  • 9

2 Answers2

1

startActivity will automatically bring the activity to the foreground on top of whicever activity you are viewing. User can go back to the previous one using back button. That's the usual way things work.

However, you can use moveTaskToBack(true) to send your activity to background.

Here is the function description.

EDIT

Check out this question and use the solution to see if your activity is in the background. If yes, then use the method I advised above to send the new activity to the background.

Community
  • 1
  • 1
Erol
  • 6,478
  • 5
  • 41
  • 55
  • i think you didn't understand my problem..the issue is to keep the splash activity to background if the app is in background...so while the user is working on some other thing and if internet connectivity is gone then suddenly the splash activity should not come to foreground and disturbing the user – Ishant Jul 11 '12 at 06:20
  • If your activity's only job is to display a message about internet connectivity, then a Toast message would work better for you. It will only appear for a few seconds and then disappear, no matter the acitivity is in the foreground or the background. – Erol Jul 11 '12 at 06:22
  • i need that only....that's the issue man....TOAST is not a solution..it would be helpful if you could suggest me regarding disabling and again enabling the BroadCast Receiver..thanks... – Ishant Jul 11 '12 at 06:23
  • Can you elaborate more on why Toast is not a solution if what you want is to only notify the user that the connection is cut? – Erol Jul 11 '12 at 06:24
0
if (isNetworkDown) {
    Log.d(TAG, "onReceive: NOT connected, stopping UpdaterService");

    // Check the foreground package
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(1);
    ComponentName topActivity = runningTasks.get(0).topActivity;
    String foregroundPackageName = topActivity.getPackageName();
    if(foregroundPackageName.equals(context.getPackageName()) {
        Intent myintent = new Intent(context,NoConnectivity.class);
        myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        context.startActivity(myintent);
    }
}
Andrea Motto
  • 1,540
  • 21
  • 38