这程序哪里是在监听网络变化

图片说明
图片说明
按书上所说,按Home键,然后启动和禁用网络,不出现toast
可是按返回键,回到桌面就出现toast,如下图:
图片说明

 package com.example.broadcasttest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {
    private boolean mReceiverTag=false;
    private IntentFilter intentFilter;
    private NetworkChangeReceiver networkChangeReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intentFilter=new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkChangeReceiver=new NetworkChangeReceiver();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if(!mReceiverTag)
        {
            mReceiverTag=true;
            registerReceiver(networkChangeReceiver,intentFilter);
        }
        else
        {
            mReceiverTag=false;
            unregisterReceiver(networkChangeReceiver);
        }
    }

    class NetworkChangeReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context,Intent intent)
        {
            Toast.makeText(context, "network changes", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

如果是这样的话,这程序哪里是在监听网络变化?

1个回答

网络发送变化时,系统会发送一个广播 (安卓系统执行的代码,我简单的写下sendBroadcast)

sendBroadcast(new Intent("android.net.conn.CONNECTIVITY_CHANGE"))

_因为你注册了监听器

  registerReceiver(networkChangeReceiver,intentFilter);

所以能收到通知,顺便说一下注册应该写到onCreate里面。
你可以看下发送广播和接收广播这方面的知识,就容易理解了

xiaoluoli88
RaleighLuo 回复YXTS122: @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(networkChangeReceiver); }
接近 3 年之前 回复
xiaoluoli88
RaleighLuo @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(networkChangeReceiver); }
接近 3 年之前 回复
YXTS122
YXTS122 回复RaleighLuo:把registerReceiver写在onCreate里,那onDestroy方法里的代码怎么安排?删掉么?还是???
接近 3 年之前 回复
YXTS122
YXTS122 回复RaleighLuo:把registerReceiver写在onCreate里,那onDestroy方法里的代码怎么安排?删掉么?还是???
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问