簡體   English   中英

廣播接收器中的Wifi狀態更改將更改發送到MainActivit

[英]Wifi state change in broadcast receiver send change to MainActivit

我正在構建一個用於監視wifi變化的應用程序。 這是一個基於MainActivity和WiFiReceiver類的非常簡單的應用程序。

MainActivity如下:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).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.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

這只是提供一個UI。

另外,我有WifiReceiver類,它擴展了廣播接收器。

public class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();
        if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            Log.d("WifiReceiver", "Have Wifi Connection");
            sendEnteringHomeRequest();
        }
        else {
            Log.d("WifiReceiver", "Don't have Wifi Connection");
            sendLeavingHomeRequest();
        }

    }

這是監視wifi的基本廣播接收器。

我想使用sendLeavingHomeRequest和sendEnteringHomeRequest發送消息到MainActivity以便顯示某些內容或執行其他操作。 我對活動和廣播接收器之間的通信很感興趣

任何想法 ?

您應該將BroadcastReceiver用作MainActivity中的內部類。 在onResume()和onPause()中分別注冊和注銷接收者。 這是示例代碼

我對活動和廣播接收器之間的通信很感興趣

您可以為此使用LocalBroadcastManager

在接收端:

  • 首先注冊LocalBroadcast接收器
  • 然后在onReceive中處理傳入的意圖數據。

      @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver, new IntentFilter("Your_IntentFilter_string")); } public BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent != null ) { String str= intent.getStringExtra("key"); //Get all your data from intent and do what you want } } } }; 

在發送端:

   Intent intent = new Intent("Your_IntentFilter_string");
   intent.putExtra("key", "My Data");
   //Put your all data using put extra 

  LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM