Android 自带Apps 学习---BugReportActivity

本文介绍如何使用FileObserver监听目录下文件的变化,并通过registerForContextMenu方法为视图组件注册上下文菜单,以便实现更丰富的交互功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:对目录下文件的监听

定义

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值