题目
[主观题]
【简答题】编程实现功能:删去一维数组中所有相同的数,使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。 例如,若一维数组中的数据是:
答案
手机中每个应用程序消耗了多少手机流量,那么这些功能Android中是如何实现的呢?按照惯例,先来谈谈原理级别的东西。Android系统中封装了一套流量数据API,这些API可以很好的管理Android系统流量使用情况。可以基于这些Android API来实现管理手机流量的功能。这些API很好的封装在了android.net包下的TrafficStats中,主要的方法有:
TrafficStats.getMobileRxBytes();//2g/3g接收的流量
TrafficStats.getMobileRxPackets();//2g/3g接收的包信息
TrafficStats.getMobileTxBytes();//2g/3g上传的流量
TrafficStats.getMobileTxPackets();//2g/3g上传的包信息
TrafficStats.getTotalRxBytes();//手机总共接收的流量
TrafficStats.getTotalTxBytes();//手机总共上传的流量
TrafficStats.getUidRxBytes(uid); //得到某个应用程程序接收的流量
TrafficStats.getUidTxBytes(uid); //得到某个应用程程序接收的流量
基于这些方法实现一个Android手机流量管理的程序示例。
Android编程实现简单流量管理功能:
packagecn.itcast.mobilesafe.ui;
importjava.util.List;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.content.pm.PackageManager;
importandroid.content.pm.ResolveInfo;
importandroid.graphics.drawable.Drawable;
importandroid.net.TrafficStats;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.BaseAdapter;
importandroid.widget.ImageView;
importandroid.widget.ListView;
importandroid.widget.TextView;
importcn.itcast.mobilesafe.R;
importcn.itcast.mobilesafe.util.TextForMater;
public classTrafficManagerActivity 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(BundlesavedInstanceState) {
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 extendsBaseAdapter{
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, ViewconvertView, 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
<?xmlversion="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
<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns: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
<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns: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>
更多“【简答题】编程实现功能:删去一维数组中所有相同的数,使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。 例如,若一维数组中的数据是:”相关的问题
第1题
用于删除数组中所有重复的值,返回一个由唯一值组成的数组的函数为
A.in_array()
B.Array_unique()
C.array_keys()
D.array_values()
点击查看答案
第2题
用于删除数组中所有重复的值,返回一个由唯一值组成的数组的函数为
A.in_array()
B.Array_unique()
C.array_keys()
D.array_values()
点击查看答案
第3题
编写一个函数,可以统计一维数组或二维数组元素之和。在主程序中,定义一个一维整型数组,调用函数计算元素之和;再定义一个二维整型数组,调用函数计算元素之和。
点击查看答案
第4题
编程题: 定义一个含N个整数的一维数组。实现:该数组中的前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。宏定义N,其值为小于30的任意值。键盘输入一维数组的N个整数和正整数m。(程序中只能定义和使用一个数组,m<N)
点击查看答案
第5题
编程题: 定义一个含N个整数的一维数组。实现:该数组中的前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。宏定义N,其值为小于30的任意值。键盘输入一维数组的N个整数和正整数m。(程序中只能定义和使用一个数组,m<N)
点击查看答案
第6题
中国剩余定理(孙子定理)是求解同余数组的方法之一,《孙子算经》中的问题:今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问物几何?其答案可以是_________。
点击查看答案
第7题
实现模拟窗体菜单功能,实现对一维二维数组基本操作进行测试,具体菜单功能如下: 当输入11、12、13,分别执行一维数组数据录入;执行一维数组的每个元素增加10,并提示操作成功;显示一维数组的所有元素; 当输入21、22、23,分别执行二维数组数据录入;执行二维数组的每个元素增加5,并提示操作成功;显示二维数组的所有元素; 当输入quit,则退出
点击查看答案
第8题
中国剩余定理(孙子定理)是求解同余数组的方法之一,《孙子算经》中的问题:今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问物几何?其答案可以是_________。
点击查看答案
第9题
二维数组可以看成一个一维数组,这个一维数组中存放的是各个一维数组的首地址
点击查看答案