2017-03-17 77 views
0

我正在构建一个应用程序来监视wifi更改。这是一个基于MainActivity和WiFiReceiver类的非常简单的应用程序。广播接收器中的Wifi状态更改发送到MainActivit

在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以显示某些内容或执行其他操作。我有兴趣在活动和广播接收器之间的沟通

任何想法?

回答

0

您应该使用BroadcastReceiver作为MainActivity内部的类。分别在onResume()和onPause()中注册和取消注册您的接收器。 这里是Sample code

0

我在活动中与广播接收机

可以使用LocalBroadcastManager用于此目的

在接收端之间的通讯insterested:

  • 首先注册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); 
+0

做我需要做的清单的东西吗? – Seb

+0

nop,只需使用我的代码。一切顺利(Y) – Darish