发布于 2016-04-02 04:05:00 | 204 次阅读 | 评论: 0 | 来源: 网友投递

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

Android移动端操作系统

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


这篇文章主要介绍了Android获取手机号码和运营商信息的方法,以实例形式完整讲述了获取手机号码和运营商信息的技巧,代码中包含完整的注释说明,需要的朋友可以参考下

本文实例讲述了Android获取手机号码和运营商信息的方法。分享给大家供大家参考。具体实现方法如下:

package com.pei.activity;  

  

import android.app.Activity;  

import android.os.Bundle;  

import android.view.View;  

import android.view.View.OnClickListener;  

import android.widget.Button;  

import android.widget.TextView;  

  

/** 

 * class name:AndroidUtilActivity<BR> 

 * class description:show get sim card info activity<BR> 

 * PS:注意权限 <BR> 

 * Date:2012-3-12<BR> 

 * @version 1.00 

 * @author CODYY)peijiangping 

 */  

public class AndroidUtilActivity extends Activity {  

    private Button button_getSIMInfo;  

    private TextView number;  

    private TextView privoid;  

  

    @Override  

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.main);  

        button_getSIMInfo = (Button) this.findViewById(R.id.getSIMInfo);  

        number = (TextView) this.findViewById(R.id.textView1);  

        privoid = (TextView) this.findViewById(R.id.textView2);  

        button_getSIMInfo.setOnClickListener(new ButtonListener());  

    }  

  

    class ButtonListener implements OnClickListener {  

  

        @Override  

        public void onClick(View v) {  

            if (v == button_getSIMInfo) {  

                SIMCardInfo siminfo = new SIMCardInfo(AndroidUtilActivity.this);  

                System.out.println(siminfo.getProvidersName());  

                System.out.println(siminfo.getNativePhoneNumber());  

                number.setText(siminfo.getNativePhoneNumber());  

                privoid.setText(siminfo.getProvidersName());  

            }  

        }  

  

    }  

}

package com.pei.activity;  

  

import android.content.Context;  

import android.telephony.TelephonyManager;  

  

/** 

 * class name:SIMCardInfo<BR> 

 * class description:读取Sim卡信息<BR> 

 * PS: 必须在加入各种权限 <BR> 

 * Date:2012-3-12<BR> 

 *  

 * @version 1.00 

 * @author CODYY)peijiangping 

 */  

public class SIMCardInfo {  

    /** 

     * TelephonyManager提供设备上获取通讯服务信息的入口。 应用程序可以使用这个类方法确定的电信服务商和国家 以及某些类型的用户访问信息。 

     * 应用程序也可以注册一个监听器到电话收状态的变化。不需要直接实例化这个类 

     * 使用Context.getSystemService(Context.TELEPHONY_SERVICE)来获取这个类的实例。 

     */  

    private TelephonyManager telephonyManager;  

    /** 

     * 国际移动用户识别码 

     */  

    private String IMSI;  

  

    public SIMCardInfo(Context context) {  

        telephonyManager = (TelephonyManager) context  

                .getSystemService(Context.TELEPHONY_SERVICE);  

    }  

  

    /** 

     * Role:获取当前设置的电话号码 

     * <BR>Date:2012-3-12 

     * <BR>@author CODYY)peijiangping 

     */  

    public String getNativePhoneNumber() {  

        String NativePhoneNumber=null;  

        NativePhoneNumber=telephonyManager.getLine1Number();  

        return NativePhoneNumber;  

    }  

  

    /** 

     * Role:Telecom service providers获取手机服务商信息 <BR> 

     * 需要加入权限<uses-permission 

     * android:name="android.permission.READ_PHONE_STATE"/> <BR> 

     * Date:2012-3-12 <BR> 

     *  

     * @author CODYY)peijiangping 

     */  

    public String getProvidersName() {  

        String ProvidersName = null;  

        // 返回唯一的用户ID;就是这张卡的编号神马的  

        IMSI = telephonyManager.getSubscriberId();  

        // IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。  

        System.out.println(IMSI);  

        if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {  

            ProvidersName = "中国移动";  

        } else if (IMSI.startsWith("46001")) {  

            ProvidersName = "中国联通";  

        } else if (IMSI.startsWith("46003")) {  

            ProvidersName = "中国电信";  

        }  

        return ProvidersName;  

    }  

}

<?xml version="1.0" encoding="utf-8"?>  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:layout_width="fill_parent"  

    android:layout_height="fill_parent"  

    android:orientation="vertical" android:gravity="center">  

  

    <TextView  

        android:id="@+id/textView1"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="TextView" />  

  

    <TextView  

        android:id="@+id/textView2"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="TextView" />  

  

    <Button  

        android:id="@+id/getSIMInfo"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="获取手机号" />

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

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