Broadcast的使用
1.发送方:
private final static String SCROLL_TO_POSITION = "scroll_to_position";//发送BroadcastIntent intent = new Intent();intent.putExtra("POSITION", position);intent.setAction(SCROLL_TO_POSITION);myContext.sendBroadcast(intent);
?2.接收方:
private final static String SCROLL_TO_POSITION = "scroll_to_position";private BroadcastReceiverForListView myBroadcastReceiverForListView;//注册监听@Overridepublic void onCreate(){super.onCreate();? IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(SCROLL_TO_POSITION);? myBroadcastReceiverForListView = new BroadcastReceiverForListView();? this.registerReceiver(myBroadcastReceiverForListView, intentFilter); }//取消监听@Overridepublic void onDestroy(){super.onDestroy();this.unregisterReceiver(myBroadcastReceiverForListView);}//定义一个内部类实现对SCROLL_TO_POSITION消息的接收和处理private class BroadcastReceiverForListView extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent){String action = intent.getAction();int position = intent.getIntExtra("POSITION", -1);if(action.equals(SCROLL_TO_POSITION) && position != -1){System.out.println("哥接收到SCROLL_TO_POSITION通知啦!");//lvOfCallLogs.scrollTo(position, position);lvOfCallLogs.setSelection(position);}} }