android获取手机信息

1.获取手机安装的应用信息(排除系统自带):

Java代码:

 1 private String getAllApp() {  
 2             String result = "";  
 3             List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);  
 4             for (PackageInfo i : packages) {  
 5                 if ((i.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {  
 6                     result += i.applicationInfo.loadLabel(getPackageManager()).toString() + ",";  
 7                 }  
 8             }  
 9             return result.substring(0, result.length() - 1);  
10     }  
消除行号

以字符串的形式返回应用的名字app1,app2,app3,......,appn中间的以","分开。当然还可以得到包名,icon图标,等等 。

 

2.获取手机可用内存和总内存:

Java代码:

 1     private String[] getTotalMemory() {  
 2            String[] result = {"",""};  //1-total 2-avail  
 3            ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();    
 4            mActivityManager.getMemoryInfo(mi);    
 5            long mTotalMem = 0;  
 6            long mAvailMem = mi.availMem;  
 7            String str1 = "/proc/meminfo";  
 8            String str2;  
 9            String[] arrayOfString;  
10            try {  
11                FileReader localFileReader = new FileReader(str1);  
12                BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);  
13                str2 = localBufferedReader.readLine();  
14                arrayOfString = str2.split("\\s+");  
15                mTotalMem = Integer.valueOf(arrayOfString[1]).intValue() * 1024;  
16                localBufferedReader.close();  
17            } catch (IOException e) {  
18                e.printStackTrace();  
19            }  
20            result[0] = Formatter.formatFileSize(this, mTotalMem);  
21            result[1] = Formatter.formatFileSize(this, mAvailMem);  
22            Log.i(TAG, "meminfo total:" + result[0] + " used:" + result[1]);  
23            return result;  
24        }  

 手机的内存信息主要在/proc/meminfo文件中,其中第一行就是总内存,而剩余内存可通过 ActivityManager.MemoryInfo得到

 

3.手机CPU信息

Java代码:

 1     private String[] getCpuInfo() {  
 2             String str1 = "/proc/cpuinfo";  
 3             String str2 = "";  
 4             String[] cpuInfo = {"", ""};  //1-cpu型号  //2-cpu频率  
 5             String[] arrayOfString;  
 6             try {  
 7                 FileReader fr = new FileReader(str1);  
 8                 BufferedReader localBufferedReader = new BufferedReader(fr, 8192);  
 9                 str2 = localBufferedReader.readLine();  
10                 arrayOfString = str2.split("\\s+");  
11                 for (int i = 2; i < arrayOfString.length; i++) {  
12                     cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";  
13                 }  
14                 str2 = localBufferedReader.readLine();  
15                 arrayOfString = str2.split("\\s+");  
16                 cpuInfo[1] += arrayOfString[2];  
17                 localBufferedReader.close();  
18             } catch (IOException e) {  
19             }  
20             Log.i(TAG, "cpuinfo:" + cpuInfo[0] + " " + cpuInfo[1]);  
21             return cpuInfo;  
22         }  
消除行号

和内存信息同理,cpu信息可通过读取/proc/cpuinfo文件来得到,其中第一行为cpu型号,第二行为cpu频率。

 

4.获取手机MAC地址:

Java代码:

1     private String getMacAddress(){  
2             String result = "";  
3             WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
4             WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
5             result = wifiInfo.getMacAddress();  
6             Log.i(TAG, "macAdd:" + result);  
7             return result;  
8     }  

MAC地址比较容易,直接通过WifiManager得到。

 

4.IMEI号,IESI号,手机型号等:

Java代码:

1     private void getInfo() {  
2             TelephonyManager mTm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);  
3             String imei = mTm.getDeviceId();  
4             String imsi = mTm.getSubscriberId();  
5             String mtype = android.os.Build.MODEL; // 手机型号  
6             String numer = mTm.getLine1Number(); // 手机号码,有的可得,有的不可得  
7         }  
消除行号

 

5.获取手机屏幕高度:

Java代码:

 1     private void getWeithAndHeight(){  
 2            //这种方式在service中无法使用,  
 3            DisplayMetrics dm = new DisplayMetrics();  
 4            getWindowManager().getDefaultDisplay().getMetrics(dm);  
 5            String width = dm.widthPixels;              //
 6            String height = dm.heightPixels;           // 7       
 8            //在service中也能得到高和宽  
 9            WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);  
10            width = mWindowManager.getDefaultDisplay().getWidth();  
11            height = mWindowManager.getDefaultDisplay().getHeight();  
12        }  








不是每个人都能成为高手,但是不努力,就算有再高的天分,也白痴一个!
 
 
 
 
 
 
 
 
 
 
 
 

Android手机中获取手机号码和运营商信息

 
 
package com.pei.activity;
02   
03 import android.app.Activity;
04 import android.os.Bundle;
05 import android.view.View;
06 import android.view.View.OnClickListener;
07 import android.widget.Button;
08 import android.widget.TextView;
09   
10 /**
11  * class name:AndroidUtilActivity<BR>
12  * class description:show get sim card info activity<BR>
13  * PS:注意权限 <BR>
14  * Date:2012-3-12<BR>
15  * @version 1.00
16  * @author CODYY)peijiangping
17  */
18 public class AndroidUtilActivity extends Activity {
19     private Button button_getSIMInfo;
20     private TextView number;
21     private TextView privoid;
22   
23     @Override
24     public void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.main);
27         button_getSIMInfo = (Button) this.findViewById(R.id.getSIMInfo);
28         number = (TextView) this.findViewById(R.id.textView1);
29         privoid = (TextView) this.findViewById(R.id.textView2);
30         button_getSIMInfo.setOnClickListener(new ButtonListener());
31     }
32   
33     class ButtonListener implements OnClickListener {
34   
35         @Override
36         public void onClick(View v) {
37             if (v == button_getSIMInfo) {
38                 SIMCardInfo siminfo = new SIMCardInfo(AndroidUtilActivity.this);
39                 System.out.println(siminfo.getProvidersName());
40                 System.out.println(siminfo.getNativePhoneNumber());
41                 number.setText(siminfo.getNativePhoneNumber());
42                 privoid.setText(siminfo.getProvidersName());
43             }
44         }
45   
46     }
47 }
查看源码
打印 ?
01 package com.pei.activity;
02   
03 import android.content.Context;
04 import android.telephony.TelephonyManager;
05   
06 /**
07  * class name:SIMCardInfo<BR>
08  * class description:读取Sim卡信息<BR>
09  * PS: 必须在加入各种权限 <BR>
10  * Date:2012-3-12<BR>
11  
12  * @version 1.00
13  * @author CODYY)peijiangping
14  */
15 public class SIMCardInfo {
16     /**
17      * TelephonyManager提供设备上获取通讯服务信息的入口。 应用程序可以使用这个类方法确定的电信服务商和国家 以及某些类型的用户访问信息。
18      * 应用程序也可以注册一个监听器到电话收状态的变化。不需要直接实例化这个类
19      * 使用Context.getSystemService(Context.TELEPHONY_SERVICE)来获取这个类的实例。
20      */
21     private TelephonyManager telephonyManager;
22     /**
23      * 国际移动用户识别码
24      */
25     private String IMSI;
26   
27     public SIMCardInfo(Context context) {
28         telephonyManager = (TelephonyManager) context
29                 .getSystemService(Context.TELEPHONY_SERVICE);
30     }
31   
32     /**
33      * Role:获取当前设置的电话号码
34      * <BR>Date:2012-3-12
35      * <BR>@author CODYY)peijiangping
36      */
37     public String getNativePhoneNumber() {
38         String NativePhoneNumber=null;
39         NativePhoneNumber=telephonyManager.getLine1Number();
40         return NativePhoneNumber;
41     }
42   
43     /**
44      * Role:Telecom service providers获取手机服务商信息 <BR>
45      * 需要加入权限<uses-permission
46      * android:name="android.permission.READ_PHONE_STATE"/> <BR>
47      * Date:2012-3-12 <BR>
48      
49      * @author CODYY)peijiangping
50      */
51     public String getProvidersName() {
52         String ProvidersName = null;
53         // 返回唯一的用户ID;就是这张卡的编号神马的
54         IMSI = telephonyManager.getSubscriberId();
55         // IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
56         System.out.println(IMSI);
57         if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
58             ProvidersName = "中国移动";
59         } else if (IMSI.startsWith("46001")) {
60             ProvidersName = "中国联通";
61         } else if (IMSI.startsWith("46003")) {
62             ProvidersName = "中国电信";
63         }
64         return ProvidersName;
65     }
66 }

你可能感兴趣的:(android,String,service,手机,import,button)