是否可以自定义我自己的broadCastreceiver?

时间:2015-01-22 11:44:06

标签: android broadcastreceiver

是否有可能创建自己的" cutomized" broadCasrReceiver

我正在连接到服务器,我想跟踪整个应用程序的连接性。换句话说,应用程序有很多片段,所有这些片段需要有关于我连接到的服务器的连接状态的反馈,因此,如果与服务器的连接因任何原因丢失,那么任何fragment应该得到通知。

怎么做?

Updat_1

MainActivity中的

BroadcastReceiver broadCastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        boolean isconnected = intent.getBooleanExtra("success", true);
        Log.d(TAG, "@broadCastReceiver(): isConnected: " + isconnected);
    }
};

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    registerReceiver(broadCastReceiver, new IntentFilter());
}
片段中的

,当有连接

Intent i = new Intent();
i.putExtra("success", true);
getActivity().sendBroadcast(i);

2 个答案:

答案 0 :(得分:0)

如果我理解正确,我认为以下内容应该可以胜任。

<强>发件人

Intent i = new Intent("com.business.connection");
if (client.connect == true){
    i.putExtra("success", true);
}
else{
    i.putExtra("success", false);
}
this.context.sendBroadcast(i);     

/* Note: 'context' depends on where you're calling the code. 
   E.g. in a fragment you could just say 
   getActivity().sendBroadcast() */

<强>接收机

(此代码适用于Activity,但您可以使用适当的生命周期方法轻松更改为Fragment。)

public class YourActivity extends Activity{
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // DO something with the message
            if (intent.getBooleanExtra("success", true){
                // Connection successful 
            } else {
                // Connection NOT successful
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();


        // Register the receiver
        registerReceiver(broadcastReceiver, new IntentFilter("com.business.connection"));
    }

    @Override
    protected void onPause() {
        super.onPause();

        // Unregister the login receiver
        unregisterReceiver(broadcastReceiver);
    }
}

答案 1 :(得分:0)

  

以下是检测互联网连接并通过broadcastintent进行通知的代码

public class BroadCastSampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.registerReceiver(this.mConnReceiver,
            new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
        String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
        boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

        NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

        if(currentNetworkInfo.isConnected()){
            Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
        }
    }
};

}

  

并且不要忘记添加权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />