我想捕获 Android 服务中的网络连接类型更改,并在事件触发时运行此服务的代码。如何在没有单独的类的情况下在服务类中做到这一点?我有这个代码,但它不起作用。
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
playlist="NETWORK TYPE CHANGED";
}
};
public void playSong(Context c, String url) {
try
{
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);