2

我有这样的方法:

public static boolean checkConnection(){

    if(getConnection()!=null){
        return true;
    }else

        return false;
}

我试图在整个应用程序中听取这种方法的结果。直到我的应用程序还活着。仅在我的应用程序中

我应该如何创建自己的操作,以便BroadcastReceiver我可以收听此方法并在它返回 false 时显示一个对话框,并在它开始返回 true 时自动隐藏对话框?

如何以及最好的方法是什么?

public class BroadCastActivity extends Activity implements OnClickListener{

ConnectionReceverLocal mReceverLocal;
Button mSwithcOn,mSwitchOff;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_broad_cast);

    /*Intent intent = new Intent();
    intent.setAction("com.broadcast.myconnectionbroadcast");
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);*/
    mReceverLocal = new ConnectionReceverLocal();
    mSwitchOff = (Button)findViewById(R.id.switchoff);
    mSwithcOn = (Button)findViewById(R.id.switchon);
    mSwitchOff.setOnClickListener(this);
    mSwithcOn.setOnClickListener(this);

}

private void sendMessage() {

    new AsyncTask<Void, Void, Boolean>() {

        @Override
        protected Boolean doInBackground(Void... params) {
            // TODO Auto-generated method stub
            Log.d("Connection result Checking Asynctasks", ""+ConnectionProvider.checkConnection());
            return ConnectionProvider.checkConnection();
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            Intent intent = new Intent("com.broadcast.myconnectionbroadcast");
            // Add data
            intent.putExtra("message", result);
            LocalBroadcastManager.getInstance(BroadCastActivity.this).sendBroadcast(intent);
        }




    }.execute();
} 


@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceverLocal);
}




@Override
protected void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(mReceverLocal,
            new IntentFilter("com.alignminds.broadcast.myconnectionbroadcast"));
    sendMessage();
}




public class ConnectionReceverLocal extends BroadcastReceiver {

    public ConnectionReceverLocal() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        AlertDialog.Builder mDBuilder = new AlertDialog.Builder(BroadCastActivity.this);

        Boolean intentAction = intent.getBooleanExtra("message", false);
        if(intentAction==false)
        {
            Log.d("Connection is false in broadcast recever", "Connection is false");
            if(mDBuilder!=null)
            {
                mDBuilder.setMessage("No Network");
                mDBuilder.create().show();  
            }

        }else {
            Log.d("Connection is true in broadcast recever", "Connection is true");
            if(mDBuilder!=null)
            {
                mDBuilder.setMessage("Network Ok");
                mDBuilder.create().show();  
            }
        }

    }
}




@Override
public void onClick(View v) {

    if(v==mSwithcOn)
    {
        WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(true);
    }
    if(v==mSwitchOff)
    {

        WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(false);
    }
}

}

4

1 回答 1

4
  1. 像这样发送带有您想要的任何操作的广播

    Intent intent = new Intent(); 
    intent.setAction("my_fancy_action");
    intent.putExtra(EVENT_MESSAGE, message);
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    
  2. 子类 BroadcastReceiver 类来监听广播

    @Override
    public void onReceive(Context context, Intent intent) {
      String intentAction = intent.getAction();
      // Do something
    }
    
  3. 注册您的接收器以收听广播

    IntentFilter filter = new IntentFilter();
    filter.addAction("my_fancy_action");
    LocalBroadcastManager.getInstance(context).registerReceiver(myReceiver, filter);
    
于 2013-07-12T10:06:01.260 回答