首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Broadcast的运用

2012-09-23 
Broadcast的使用1.发送方:private final static String SCROLL_TO_POSITION scroll_to_position//发

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);}}    }

热点排行