Broadcast receiver for checking internet connection in android app

后端 未结 21 3190
温柔的废话
温柔的废话 2020-11-21 22:28

I am developing an android broadcast receiver for checking internet connection.

The problem is that my broadcast receiver is being called two times. I want it to get

21条回答
  •  鱼传尺愫
    2020-11-21 22:37

    Try with this

    public class ConnectionBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    
    
        if () { 
            Toast.makeText(context, "connect to the internet", Toast.LENGTH_LONG).show();
    
            /*upload background upload service*/
            Intent serviceIntent = new Intent(context,);
            context.startService(serviceIntent);
    
    
        }else{
            Toast.makeText(context, "Connection failed", Toast.LENGTH_LONG).show();
    
        }
        }
    }
    

    As soon as internet connection trigger, this (BroadcastReciever) will be loaded

提交回复
热议问题