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

摘要:
TelephonyManager类主要提供一系列获取方法,用于访问与移动通信相关的状态和信息。它包括手机SIM卡的状态和信息、电信网络的状态和手机用户的信息。TelephonyManager类的对象可以通过Context.getSystemService方法获得。需要注意的是,某些通信信息的访问对应用程序的权限有一定的限制,需要在开发过程中添加相应的权限。˃˃LinearLayoutxmlns:android=“http://schemas.android.com/apk/res/android“android:layout_width=”match_parent“android:layout_height=”match _parent”android:orientation=“vertical”˃II.打开“src/com”。恩沃克斯。contentprovider_b/MainActivity。java”文件。然后输入以下代码:[java]viewplatecopyprint?

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

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

一、设计界面

  1、布局文件

  打开res/layout/activity_main.xml文件。
  输入以下代码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/getphoneinfo"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="获得手机网络信息" />  
  13.   
  14. </LinearLayout>  
<?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: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获得手机网络信息" />

</LinearLayout>


二、程序文件

  打开“src/com.genwoxue.contentprovider_b/MainActivity.java”文件。
  然后输入以下代码:

  1. package com.genwoxue.telephony;  
  2.   
  3.   
  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.EditText;  
  9. import android.widget.Toast;  
  10. import android.app.Activity;  
  11. import android.telephony.TelephonyManager;  
  12. import android.content.Context;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     private Button btnGet=null;  
  17.       
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.           
  23.         btnGet=(Button)super.findViewById(R.id.getphoneinfo);  
  24.         btnGet.setOnClickListener(new OnClickListener(){  
  25.             public void onClick(View v)  
  26.             {    
  27.                 StringBuilder info=new StringBuilder();  
  28.                 //获取TelephonyManager服务   
  29.                 TelephonyManager telphony=(TelephonyManager)MainActivity.this.getSystemService(Context.TELEPHONY_SERVICE);  
  30.                 //获取移动服务商名称   
  31.                 info.append(telphony.getNetworkOperatorName()+"☆☆☆");  
  32.                 //获取设备号码   
  33.                 info.append(telphony.getDeviceId()+"☆☆☆");  
  34.                   
  35.                 Toast.makeText(getApplicationContext(), info, Toast.LENGTH_LONG).show();  
  36.             }  
  37.         });  
  38.     }  
  39. }  
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”文件。

  然后输入以下代码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.genwoxue.telephony"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="11"  
  9.         android:targetSdkVersion="15" />  
  10.       
  11.     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>  
  12.   
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.genwoxue.telephony.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  
<?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"/>

四、运行结果

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

免责声明:文章转载自《第86章、系统服务之TELEPHONY_SERVICE(从零开始学Android)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇spring+redis 集群下的操作mongo分页查询,同时返回分页记录和记录数下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

xamarin跨平台iOS、Android 与Windows

http://xamarin.csdn.net/ Xamarin是一个行动App开发平台,提供跨平台开发能力,开发人员透过Xamarin开发工具与程序语言,即可开发出iOS、Android 与Windows 等平台的原生(Native) App 应用程序,不须个别使用各平台的开发工具与程序语言,不只是「write-once, run everywhere」...

Android开发模拟器启动失败的解决方法

1. 错误提示信息  错误大概意思:连接到adb(android debug bridge)出现了一个严重的错误,你必须重启adb和Eclipse工具,请确保在位置……adb工具被执行了。 adb:是sdk提供的工具,在android-sdk-windowsplatform-tools目录下,通过adb可以连接到android手机或模拟器。像91手机...

xcode KVC:Key Value Coding 键值编码

赋值 // 能修改私有成员变量 - (void)setValue:(id)value forKey:(NSString *)key; - (void)setValue:(id)value forKeyPath:(NSString *)keyPath; - (void)setValuesForKeysWithDictionary:(NSDictionary...

[原]零基础学习SDL开发之在Android使用SDL2.0渲染PNG图片

在上一篇文章我们知道了如何在android使用SDL2.0来渲染显示一张bmp图,但是如果是一张png或者一张jpg的图,那么还能显示成功么?答案是否定的 我们需要移植SDL_image库来支持除bmp之外的图片格式。 一、移植SDL_image库: 使用如下命令,从SDLMercurial获取SDL_image的源码: hg clone https://...

使用Google浏览器做真机页面调试

步骤1: 从Windows,Mac或Linux计算机远程调试Android设备上的实时内容。本教程将教您如何: 设置您的Android设备进行远程调试,并从开发机器中发现它。从您的开发机器检查和调试Android设备上的实时内容。将来自Android设备的内容屏幕截图到开发机器上的DevTools实例。 要求 Chrome 32或更高版本安装在您的开发机...

view 引用 xml 布局

引用:http://www.cnblogs.com/topcoderliu/archive/2011/05/07/2039862.html  在开发中,我们经常使用到ListView这个控件。Android的API也提供了许多创建ListView适配器的快捷方式。例如ArrayAdapter、SimpleAdapter和SimpleCursorAdapte...