您的位置:首页 > 移动开发 > Android开发

Android 手机设置中的关于手机界面

2016-06-18 16:32 267 查看
不管哪一款Android 设备都会在设置中显示关于手机一栏,基本上大同小异,手机名称啊,型号啊,版本号啊手机内存机身存储等等,以下是简单的记录下:

1.Activity:

package activity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.StatFs;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.Formatter;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.vstar3d.myapplication.R;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import adapter.Setting_AboutPhoneAdapter;

public class Setting_AboutPhoneActivity extends AppCompatActivity {
private TextView mImageView;
private ListView listView;
private Setting_AboutPhoneAdapter adapter;
private List<String> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
initView();
initDate();
getDate();
}

private void initDate() {
list = new ArrayList<String>();
adapter = new Setting_AboutPhoneAdapter(list, this);
}

private void initView() {
mImageView = (TextView) findViewById(R.id.image);
mImageView.setText("基于Adnroid"+android.os.Build.VERSION.RELEASE+"开发");
listView = (ListView) findViewById(R.id.listView);
}

public void getDate() {
String name = "手机名称\n" + android.os.Build.PRODUCT;
String model = "型号\n" + android.os.Build.MODEL;
String version = "Android版本\n" + android.os.Build.VERSION.RELEASE;
String cpu = "处理器\n" + android.os.Build.CPU_ABI2;
String phonememory = "手机内存\n" + "手机总内存: " + this.getTotalMemory() + ", " + "可用内存: " + this.getAvailMemory();
String sdmemory = "机身存储\n" + "SD卡总内存: " + this.getSDAllSize() + "GB" + ", " + "可用内存: " + this.getSDFreeSize() + "GB";
String display = "版本号\n" + android.os.Build.DISPLAY;
String result1 = "基带版本\n" + getBaseband_Ver();
String kernel = "内核版本\n" + getKernelVersion();
String inner = "内部版本\n" + getInner_Ver();

list.add(name);
list.add(model);
list.add(version);
list.add(cpu);
list.add(phonememory);
list.add(sdmemory);
list.add(result1);
list.add(kernel);
list.add(inner);
list.add(display);
listView.setAdapter(adapter);
}

/*手机可用内存*/
private String getAvailMemory() {

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return Formatter.formatFileSize(getBaseContext(), mi.availMem);

}
/*手机总内存*/
private String getTotalMemory() {
String str1 = "/proc/meminfo";
String str2;
String[] arrayOfString;
long initial_memory = 0;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
initial_memory = Long.parseLong(arrayOfString[1]) * 1024;
localBufferedReader.close();
} catch (IOException e) {
}
return Formatter.formatFileSize(getBaseContext(), initial_memory);
}

/*SD卡总内存*/
public float getSDAllSize() {
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
long blockSize = sf.getBlockSize();
long allBlocks = sf.getBlockCount();
return (allBlocks * blockSize) / 1024 / 1024 / 1024; //单位MB

}

/*SD卡可用内存*/
public float getSDFreeSize() {
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
long blockSize = sf.getBlockSize();
long freeBlocks = sf.getAvailableBlocks();
return (freeBlocks * blockSize) / 1024 / 1024 / 1024; //单位MB
}

/*基带版本*/
public static String getBaseband_Ver() {
String Version = "";
try {
Class cl = Class.forName("android.os.SystemProperties");
Object invoker = cl.newInstance();
Method m = cl.getMethod("get", new Class[]{String.class, String.class});
Object result = m.invoke(invoker, new Object[]{"gsm.version.baseband", "no message"});
Version = (String) result;
} catch (Exception e) {
e.printStackTrace();
}
return Version;
}

/*内部版本*/
public static String getInner_Ver() {
String ver = "";

if (android.os.Build.DISPLAY.contains(android.os.Build.VERSION.INCREMENTAL)) {
ver = android.os.Build.DISPLAY;
} else {
ver = android.os.Build.VERSION.INCREMENTAL;
}
return ver;

}

/*内核版本*/
public static String getKernelVersion() {
String kernelVersion = "";
InputStream inputStream = null;
try {
inputStream = new FileInputStream("/proc/version");
} catch (FileNotFoundException e) {
e.printStackTrace();
return kernelVersion;
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 8 * 1024);
String info = "";
String line = "";
try {
while ((line = bufferedReader.readLine()) != null) {
info += line;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

try {
if (info != "") {
final String keyword = "version ";
int index = info.indexOf(keyword);
line = info.substring(index + keyword.length());
index = line.indexOf(" ");
kernelVersion = line.substring(0, index);
}
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}

return kernelVersion;
}

}


2.Adapter:

package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.vstar3d.myapplication.R;
import java.util.List;

public class Setting_AboutPhoneAdapter extends BaseAdapter {
private List<String> itemDetailed;
private Context context;

public Setting_AboutPhoneAdapter(List<String> itemDetailed, Context context) {
this.itemDetailed = itemDetailed;
this.context = context;
}

@Override
public int getCount() {
return itemDetailed.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.item,null);
viewHolder = new ViewHolder();
viewHolder.text_detailed = (TextView) convertView.findViewById(R.id.text_detailed);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
String stringTitle = itemDetailed.get(position);
viewHolder.text_detailed.setText(stringTitle);
return convertView;
}
class ViewHolder{
TextView text_detailed;
}


3.布局文件:main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.vstar3d.myapplication.Main3Activity">

<TextView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:drawableTop="@mipmap/about"
android:onClick="onClick" />

<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#ff211323" />

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"></ListView>

</LinearLayout>


item:

<?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">

<TextView
android:padding="3dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:id="@+id/text_detailed"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


至于还有一些法律信息等等信息加进去就行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: