一:对目录下文件的监听
定义
int flags = FileObserver.CREATE | FileObserver.MOVED_TO; mObserver = new FileObserver(REPORT_DIR.getPath(), flags) { public void onEvent(int event, String path) { mHandler.post(new Runnable() { public void run() { scanDirectory(); } }); } };
开始监听,一般写于onStart()
mObserver.startWatching();
mObserver.startWatching();
停止监听,一般写于onStop()
mObserver.stopWatching();
mObserver.stopWatching();
二:注册ContextMenuInfo为ContextMenu
public void registerForContextMenu (View view) Since: API Level 1 Registers a context menu to be shown for the given view (multiple views can show the context menu). This method will set the View.OnCreateContextMenuListener on the view to this activity, so onCreateContextMenu(ContextMenu, View, ContextMenuInfo) will be called when it is time to show the context menu. Parameters view The view that should show a context menu. See Also unregisterForContextMenu(View)
AdapterView.AdapterContextMenuInfo extends
ContextMenu.ContextMenuInfo
注册到Menu中,可以直接从MenuItem中获取Info相关信息进行操作
registerForContextMenu(getListView()); @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, SYSTEM_LOG_ID, 0, "System Log"); menu.add(0, CPU_ID, 0, "CPU Info"); menu.add(0, MEMORY_ID, 0, "Memory Info"); menu.add(0, PROCRANK_ID, 0, "Procrank"); } public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); if (info.position >= mFiles.size()) { return true; } int id = item.getItemId(); switch (id) { case SYSTEM_LOG_ID: // drop down case MEMORY_ID: // drop down case CPU_ID: // drop down case PROCRANK_ID: File file = mFiles.get(info.position); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "vnd.android/bugreport"); intent.putExtra("section", ID_MAP.get(id)); startActivity(intent); return true; default: return super.onContextItemSelected(item); } }