0

I have an activity class MediaPlayerDemo_Audio and one broadcast receiver class ConnectivityChangeReceiver which extends BroadcastReceiver.

I want to finish an activity MediaPlayerDemo_Audio when network connection lost.

Any idea? Thanks.

ConnectivityChangeReceiver.java

public class ConnectivityChangeReceiver extends BroadcastReceiver {

@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE );
    /* Check wi-fi network availability */
    NetworkInfo activeWifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isConnectedToWifi = activeWifiInfo != null && activeWifiInfo.isConnectedOrConnecting();
    /* Check mobile network availability */
    NetworkInfo activeMobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isConnectedToMobileData = activeMobileInfo != null && activeMobileInfo.isConnectedOrConnecting();

    if(isConnectedToWifi){
        //Toast.makeText(context, "Connected to wifi!", Toast.LENGTH_LONG).show();
    }else{
        if(isConnectedToMobileData){
            //Toast.makeText(context, "Connected to Mobile data!", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(context, "No internet connection!", Toast.LENGTH_LONG).show();
            /* Here I want to finish that activity */
        }
    }       
}}

Androidmanifest.xml Class ConnectivityChangeReceiver will be running in background and It will keep checking network connectivity, this class will be called from this receiver.

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

I referred this answer but It could not help. Because one class cannot extend multiple classes. I mean, MediaPlayerDemo_Audio class cannot extend Activity as well as BroadcastReceiver class and vice versa.

Community
  • 1
  • 1
Ram
  • 3,887
  • 4
  • 27
  • 49

2 Answers2

1

You just need to put your BroadcastReceiver Class inside your activity as inner Class. and register Receiver on onResume and unregister inside onPause.

Let's say

public class MediaPlayerDemo_Audio extends Activity
{

//  your activity code
.....
//  


// here is your broadcast receiver 
public class ConnectivityChangeReceiver extends BroadcastReceiver {

@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE );
    /* Check wi-fi network availability */
    NetworkInfo activeWifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isConnectedToWifi = activeWifiInfo != null && activeWifiInfo.isConnectedOrConnecting();
    /* Check mobile network availability */
    NetworkInfo activeMobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isConnectedToMobileData = activeMobileInfo != null && activeMobileInfo.isConnectedOrConnecting();

    if(isConnectedToWifi){
        //Toast.makeText(context, "Connected to wifi!", Toast.LENGTH_LONG).show();
    }else{
        if(isConnectedToMobileData){
            //Toast.makeText(context, "Connected to Mobile data!", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(context, "No internet connection!", Toast.LENGTH_LONG).show();
            /* Here I want to finish that activity */

//finish activity here 

        }
    }       
}}



}
Praveen Sharma
  • 4,326
  • 5
  • 25
  • 45
  • But praveen, he will face issues with this type. – Nigam Patro Dec 08 '15 at 11:40
  • its a quick solution for him. Which kind of issue you are talking about? – Praveen Sharma Dec 08 '15 at 11:41
  • Ok. Ok Sorry for mistaken. – Nigam Patro Dec 08 '15 at 11:42
  • @praveenSharma I tried this also, but no luck. Actually I am using Vitamio library for audio streaming in android. When I start the activity and disconnect the network, It's showing the toast message but I am also getting Android alert saying that "Unfortunately, Application_Name has stopped". – Ram Dec 08 '15 at 12:15
  • Also put your logcat stack here – Praveen Sharma Dec 08 '15 at 14:00
  • @parveen, Earlier I had a separate class (ConnectivityChangeReceiver) but as you suggested, I put it inside activity (Inline class). Now It's throwing exception...ClassNotFoundException ConnectivityChangeReceiver in package io.vov.vitamio.demo and that's it in stack trace. (Testing in Nexus 5 with Marshmallow). – Ram Dec 08 '15 at 14:20
0

As per the example, it doesn't need to extend the both the classes. Just create broadcast receiver inside your activity and one thing, you need to do is register the receiver in onResume() and unregister it on onPause().

Below is example:-

public class MyPreferenceActivity extends Activity {

//Your stuff inside the activity

@Override
protected void onResume() {
    super.onResume();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    registerReceiver(receiver, intentFilter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
}

ConnectivityChangeReceiver receiver = new ConnectivityChangeReceiver();

public class ConnectivityChangeReceiver extends BroadcastReceiver {

    @SuppressWarnings("deprecation")
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
/* Check wi-fi network availability */
        NetworkInfo activeWifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean isConnectedToWifi = activeWifiInfo != null && activeWifiInfo.isConnectedOrConnecting();
/* Check mobile network availability */
        NetworkInfo activeMobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean isConnectedToMobileData = activeMobileInfo != null && activeMobileInfo.isConnectedOrConnecting();

        if (isConnectedToWifi) {
            //Toast.makeText(context, "Connected to wifi!", Toast.LENGTH_LONG).show();
        } else {
            if (isConnectedToMobileData) {
                //Toast.makeText(context, "Connected to Mobile data!", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, "No internet connection!", Toast.LENGTH_LONG).show();
        /* Here I want to finish that activity */
            }
        }
    }
}
}
Nigam Patro
  • 2,760
  • 1
  • 18
  • 33
  • But if I do so, what you have suggested. In that case, It won't keep checking network connectivity in background. I have to finish current activity when network gets disconnected. – Ram Dec 08 '15 at 11:55
  • Can you just tell me the action for the broadcast receiver. – Nigam Patro Dec 08 '15 at 12:00
  • What do you mean by the "action" for the broadcast receiver? (I want to finish the current activity and get back to previous activity when connection lost. For this purpose I used Broadcastreceiver. Is that what you are asking?) Please check my question, I have already done that. – Ram Dec 08 '15 at 12:20
  • Inside manifest file, you should have declared this receiver. – Nigam Patro Dec 08 '15 at 12:21
  • Please check my question, I have already declared receiver in AndroidManifest.xml. – Ram Dec 08 '15 at 12:26
  • Thanks a lot for your effort, but the issue is still there, It's finishing the activity but It is showing Android OS alert (Saying: Unfortunately, App has stopped). as well as throwing an exception (Unable to instantiate the receiver...) – Ram Dec 08 '15 at 13:52
  • @RamSharan In which line its showing error, and what is the code, can you tell me? – Nigam Patro Dec 08 '15 at 13:54
  • @RamSharan But, its not crashing in my case. – Nigam Patro Dec 08 '15 at 13:57
  • Thank you everyone, I was trying this broadcastreceiver code in Vitamio Sample demo code which is still crashing but when I used this code in my actual project, problem is solved. In my project, when I call finish(), It redirects to HTML screen so it doesn't show up any unwanted android OS alert. My issue is gone because of your code (I am not using receiver in AndroidManifest.xml anymore, still works), so I am accepting your answer. Thanks. – Ram Dec 08 '15 at 15:20