发布于 2017-09-14 20:18:57 | 15 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android编程实现简单流量管理功能的方法,结合实例形式分析了Android实现流量监控所涉及的功能模块与布局技巧,需要的朋友可以参考下

本文实例讲述了Android编程实现简单流量管理功能的方法。分享给大家供大家参考,具体如下:


package cn.itcast.mobilesafe.ui;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.net.TrafficStats;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.itcast.mobilesafe.R;
import cn.itcast.mobilesafe.util.TextForMater;
public class TrafficManagerActivity extends Activity {
  private TextView _3gTotal;
  private TextView wifiTotal;
  private ListView content;
  private String mobileTraffic;
  private String wifiTraffic;
  private PackageManager pm;
  private TrafficAdapter adapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    pm = getPackageManager();
    setContentView(R.layout.traffic_manager);
    _3gTotal = (TextView) this.findViewById(R.id._3gTotal);
    wifiTotal = (TextView) this.findViewById(R.id.wifiTotal);
    content = (ListView) this.findViewById(R.id.content);
    setTotalData();
    adapter = new TrafficAdapter();
    content.addHeaderView(View.inflate(this, R.layout.traffic_title, null));
    content.setAdapter(adapter);
  }
  private void setTotalData() {
    long mobileRx = TrafficStats.getMobileRxBytes();
    long mobileTx = TrafficStats.getMobileTxBytes();
    long totalRx = TrafficStats.getTotalRxBytes();
    long totalTx = TrafficStats.getTotalTxBytes();
    long wifiRx = totalRx - mobileRx;
    long wifiTx = totalTx - mobileTx;
    mobileTraffic = TextForMater.getDataSize(mobileRx + mobileTx);
    _3gTotal.setText(mobileTraffic);
    wifiTraffic = TextForMater.getDataSize(wifiTx + wifiRx);
    wifiTotal.setText(wifiTraffic);
  }
  private class TrafficAdapter extends BaseAdapter{
    List<ResolveInfo> resolveInfos ;
    public TrafficAdapter() {
      super();
      Intent intent = new Intent();
      intent.setAction("android.intent.action.MAIN");
      intent.addCategory("android.intent.category.LAUNCHER");
      resolveInfos = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    }
    @Override
    public int getCount() {
      return resolveInfos.size();
    }
    @Override
    public Object getItem(int position) {
      return position;
    }
    @Override
    public long getItemId(int position) {
      return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view ;
      if(null == convertView){
        view = View.inflate(getApplicationContext(), R.layout.traffic_item, null);
      }else{
        view = convertView;
      }
      ViewHolder holder = new ViewHolder();
      holder.iv_traffic_icon = (ImageView) view.findViewById(R.id.iv_traffic_icon);
      holder.tv_traffic_name = (TextView) view.findViewById(R.id.tv_traffic_name);
      holder.tv_traffic_tx = (TextView) view.findViewById(R.id.tv_traffic_tx);
      holder.tv_traffic_rx = (TextView) view.findViewById(R.id.tv_traffic_rx);
      ResolveInfo info = resolveInfos.get(position);
      String appName = info.loadLabel(pm).toString();
      holder.tv_traffic_name.setText(appName);
      Drawable icon = info.loadIcon(pm);
      holder.iv_traffic_icon.setImageDrawable(icon);
      int uid = info.activityInfo.applicationInfo.uid;
      holder.tv_traffic_rx.setText(TextForMater.getDataSize(TrafficStats.getUidRxBytes(uid)));
      holder.tv_traffic_tx.setText(TextForMater.getDataSize(TrafficStats.getUidTxBytes(uid)));
      return view;
    }
  }
  static class ViewHolder{
    ImageView iv_traffic_icon;
    TextView tv_traffic_name;
    TextView tv_traffic_tx;
    TextView tv_traffic_rx;
  }
}

traffic_manager.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <TableRow
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" >
      <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="2G/3G总流量" />
      <TextView
        android:id="@+id/_3gTotal"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    </TableRow>
    <TableRow
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" >
      <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Wifi总流量" />
      <TextView
        android:id="@+id/wifiTotal"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    </TableRow>
  </TableLayout>
  <SlidingDrawer
    android:id="@+id/ll_sd_traffic"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:content="@+id/content"
    android:handle="@+id/handle"
    android:orientation="vertical" >
    <ImageView
      android:id="@id/handle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/notification" />
    <ListView
      android:id="@id/content"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
    </ListView>
  </SlidingDrawer>
</LinearLayout>

traffic_manager_item.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center_vertical"
  android:orientation="horizontal" >
  <ImageView
    android:id="@+id/iv_traffic_icon"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />
  <TextView
    android:id="@+id/tv_traffic_name"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="名称" />
  <TextView
    android:id="@+id/tv_traffic_tx"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="上传" />
  <TextView
    android:id="@+id/tv_traffic_rx"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="下载" />
</LinearLayout>

traffic_title.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center_vertical"
  android:orientation="horizontal" >
  <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="图标" />
  <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="名称" />
  <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="上传" />
  <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="下载" />
</LinearLayout>

希望本文所述对大家Android程序设计有所帮助。



相关阅读 :
Android编程实现简单流量管理功能实例
Android编程实现简单的UDP Client实例
android编程实现对话框的封装实例
Android编程实现调用系统分享功能示例
Android编程实现google消息通知功能示例
Android编程实现两点触控功能示例
Android编程实现动画自动播放功能
Android编程实现VideoView循环播放功能的方法
Android编程实现自定义输入法功能示例【输入密码时防止第三方窃取】
Android编程实现换肤功能实例
Android实现发送短信功能实例详解
Android中使用Service实现后台发送邮件功能实例
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务