Why is my BroadCast Receiver not working?

前端 未结 6 924
礼貌的吻别
礼貌的吻别 2021-01-21 16:03

I am learning about BroadCast Receivers. What I am trying to achieve with the following piece of code is, I would like to see a toast when I switch to airplane mode, where the a

6条回答
  •  情歌与酒
    2021-01-21 16:53

    You need to put the receiver in the bundles package:

    package my.bundles.id;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.widget.Toast;
    
    public class ConnectivityChangedReceiver extends BroadcastReceiver {
    
    @Override
    public void onReceive( Context context, Intent intent )
    {
        Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
    
    }
    }
    

    You should put it in a package thats the same as the bundleId and that will let the above work. The first dot in the name field means that the Class is a member of the bundles namespace. So, since it wasnt in a package, the dot made it look in the wrong place.

提交回复
热议问题