android 代码中实现获取log信息

本文介绍了一个用于展示系统通知记录的日志适配器类实现细节,包括如何读取和解析事件日志、填充视图等内容。
 class NotificationLogAdapter extends BaseAdapter {
        private ArrayList<EventLog.Event> mNotificationEvents;
        private final PackageManager mPM;
        private final LayoutInflater mInflater;


        public NotificationLogAdapter() {
            mPM = getPackageManager();
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mNotificationEvents = new ArrayList<EventLog.Event>();
            
            int[] tags = new int[] { EventLog.getTagCode("notification_enqueue") };
            try {
                EventLog.readEvents(tags, mNotificationEvents);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return;
            }
            android.util.Log.d("NotificationLogActivity", "loaded " + getCount() + " entries");
        }
        
        public int getCount() {
            return mNotificationEvents != null ? mNotificationEvents.size() : 0;
        }


        public Object getItem(int position) {
            return position;
        }


        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = mInflater.inflate(R.layout.row, parent, false);
            } else {
                view = convertView;
            }
            bindView(view, mNotificationEvents.get(position));
            return view;
        }
        


        private final void bindView(View view, EventLog.Event evt) {
            TextView title = (TextView)view.findViewById(R.id.title);
            TextView more = (TextView)view.findViewById(R.id.text);
            TextView time = (TextView)view.findViewById(R.id.time);
            ImageView icon = (ImageView)view.findViewById(R.id.icon);


            Object[] data = (Object[]) evt.getData();
            // EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag,
            // notification.toString());
            String pkg = (String) data[0];
            int id = (Integer) data[1];
            String tag = (String) data[2];
            String text = (String) data[3];


            ApplicationInfo appInfo;
            Drawable appIcon = null;
            try {
                appInfo = mPM.getApplicationInfo(pkg, 0);
                pkg =  mPM.getApplicationLabel(appInfo) + " (" + pkg + ")";
                appIcon = mPM.getApplicationIcon(appInfo);
            } catch (NameNotFoundException e) {
            }
            title.setText(pkg);
            more.setText(text);
            time.setText(new Date(evt.getTimeNanos()/1000000).toString());
            icon.setImageDrawable(appIcon);
        }

    }



最后要记得androidManifest.xml中添加 android:sharedUserId="android.uid.log"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值