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

第86章、系统服务之TELEPHONY_SERVICE(从零开始学Android)

2013-03-11 16:24 295 查看
  TelephonyManager类主要提供了一系列用于访问与手机通讯相关的状态和信息的get方法。其中包括手机SIM的状态和信息、电信网络的状态及手机用户的信息。在应用程序中可以使用这些get方法获取相关数据。

TelephonyManager类的对象可以通过Context.getSystemService(Context.TELEPHONY_SERVICE)方法来获得,需要注意的是有些通讯信息的获取对应用程序的权限有一定的限制,在开发的时候需要为其添加相应的权限。

一、设计界面

  1、布局文件

  打开res/layout/activity_main.xml文件。

  输入以下代码:

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

    <Button
        android:id="@+id/getphoneinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获得手机网络信息" />

</LinearLayout>


二、程序文件

  打开“src/com.genwoxue.contentprovider_b/MainActivity.java”文件。

  然后输入以下代码:

package com.genwoxue.telephony;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.telephony.TelephonyManager;
import android.content.Context;

public class MainActivity extends Activity {

	private Button btnGet=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btnGet=(Button)super.findViewById(R.id.getphoneinfo);
		btnGet.setOnClickListener(new OnClickListener(){
        	public void onClick(View v)
        	{  
        		StringBuilder info=new StringBuilder();
        		//获取TelephonyManager服务
        		TelephonyManager telphony=(TelephonyManager)MainActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
        		//获取移动服务商名称
        		info.append(telphony.getNetworkOperatorName()+"☆☆☆");
        		//获取设备号码
        		info.append(telphony.getDeviceId()+"☆☆☆");
        		
        		Toast.makeText(getApplicationContext(), info, Toast.LENGTH_LONG).show();
        	}
        });
	}
}


三、配置文件

  打开“AndroidManifest.xml”文件。

  然后输入以下代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.genwoxue.telephony"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="15" />
    
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.genwoxue.telephony.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


注意:需要在AndroidManifest.xml文件中添加权限:

   <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

四、运行结果

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐