Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:43:22+00:00 2026-06-13T19:43:22+00:00

I would like to implement a broadcast receiver for my internet connection checking. If

  • 0

I would like to implement a broadcast receiver for my internet connection checking. If connection is not present just finish(); it. But i am still messing up the context . Please check on my below codes.

/**
 * This broadcast receiver is awoken after boot and registers the service that
 * checks for new photos from all the known contacts.
 */


public class ConnectionDetector extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {



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

     if(noConnectivity){

         ((Activity)context).finish();
         //Show Warning Message
         //Close Application the way i suggested
     }

    }


}

AndroidManifest

 <receiver      android:name=".ConnectionDetector"
                        android:label="NetworkConnection">
        <intent-filter>
            <action     android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </intent-filter>
        </receiver>

LogCat :

11-01 22:40:29.179: E/AndroidRuntime(29531): FATAL EXCEPTION: main
11-01 22:40:29.179: E/AndroidRuntime(29531): java.lang.RuntimeException: Unable to start receiver in.wptrafficanalyzer.actionbarsherlocknavtab.ConnectionDetector: java.lang.ClassCastException: android.app.ReceiverRestrictedContext
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.app.ActivityThread.access$2400(ActivityThread.java:117)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.os.Looper.loop(Looper.java:130)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.app.ActivityThread.main(ActivityThread.java:3691)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at java.lang.reflect.Method.invokeNative(Native Method)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at java.lang.reflect.Method.invoke(Method.java:507)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at dalvik.system.NativeStart.main(Native Method)
11-01 22:40:29.179: E/AndroidRuntime(29531): Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext
11-01 22:40:29.179: E/AndroidRuntime(29531):    at in.wptrafficanalyzer.actionbarsherlocknavtab.ConnectionDetector.onReceive(ConnectionDetector.java:29)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798)
11-01 22:40:29.179: E/AndroidRuntime(29531):    ... 10 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:43:23+00:00Added an answer on June 13, 2026 at 7:43 pm

    The context you get in BroadcastReciever is not an Activity. BroadcastReciever works outside of activity and isn’t tied to one, unless you specifically make it.

    Putting aside bad practice of just finishing activity on internet problem without notifying user you can do the following:

    public abstract class ConnectionAwareActivity extends Activity {
    
    protected final IntentFilter mIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); // A filter for a BR. We want to listen to internet changes
    protected final ConnectionDetector mConnectionDetector = new ConnectionDetector(); // Creating an instance of our BR for activity to use
    
    @Override
    protected void onResume() {
        super.onResume();
        try {
            registerReceiver(mConnectionDetector, mIntentFilter); // Activity gets shown, we register a BR and it starts to receive notifications about internet changes
        } catch (Exception exc) {
            // whoops
        }
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        try {
            unregisterReceiver(mConnectionDetector); // Try to unregister BR, since when activity is not visible to user, we don't want to perform any operations on internet change
        } catch (Exception exc) {
            // whoops
        }
    }
    
    // Your BR that is encapsulated in Activity and therefore has access to it's methods, since it has access to Activity instance
    protected class ConnectionDetector extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            if (noConnectivity) {
                finish();
            }
    
        }
    }
    

    }

    Use it as a superclass for your other activities (this one extends from Activity, change it to whatever), that must be terminated on connectivity errors.

    It’s very basic variant and somewhat wrong, since you better prefer composition over superclasses

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement some Drag-select functionality into a project of mine but
I would like to implement ABAP Unit tests in my ABAP programs, but my
I would like to implement tips in my web application, but i don't want
I would like to implement a data access object pattern in C++, but preferably
I would like to implement a TCP socket connection to an anonymous server{ chat.facebook.com:5222
I would like to implement what I know as a CVAR System, I'm not
I would like to implement a distinct thread for each route in apache camel.I
I would like to implement a very simple way to store a variable containing
I would like to implement a function with R that removes repeated characters in
I would like to implement a simple queueing service specific to a project. Where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.