Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1049881
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-12 16:52:31

代码如下:

  1. package com.pei.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.TextView;  
  9.   
  10. /** 
  11.  * class name:AndroidUtilActivity
     
  12.  * class description:show get sim card info activity
     
  13.  * PS:注意权限 
     
  14.  * Date:2012-3-12
     
  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. }  

  1. package com.pei.activity;  
  2.   
  3. import android.content.Context;  
  4. import android.telephony.TelephonyManager;  
  5.   
  6. /** 
  7.  * class name:SIMCardInfo
     
  8.  * class description:读取Sim卡信息
     
  9.  * PS: 必须在加入各种权限 
     
  10.  * Date:2012-3-12
     
  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.      * 
    Date:2012-3-12
     
  35.      * 
    @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获取手机服务商信息 
     
  45.      * 需要加入权限 
  46.      * android:name="android.permission.READ_PHONE_STATE"/> 
     
  47.      * Date:2012-3-12 
     
  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. }  


  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android=""  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" android:gravity="center">  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="TextView" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/textView2"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="TextView" />  
  18.   
  19.     <Button  
  20.         android:id="@+id/getSIMInfo"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="获取手机号码等信息" />  
  24.   
  25. LinearLayout>  

 

阅读(448) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~