android設備號是惟一的deviceID;java
TelephonyManager
這個管理類中包含不少屬性,
TelephonyManager tm= (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
listValues.add(tm.getDeviceId());//獲取設備編號
listValues.add(tm.getSimCountryIso());//獲取SIM卡國別
listValues.add(tm.getSimSerialNumber());//獲取SIM卡序列號
listValues.add(simState[tm.getSimState()]);//獲取SIM卡狀態
listValues.add((tm.getDeviceSoftwareVersion()!=null?tm.getDeviceSoftwareVersion():"未知")); //獲取軟件版本
listValues.add(tm.getNetworkOperator());//獲取網絡運營商代號
listValues.add(tm.getNetworkOperatorName());//獲取網絡運營商名稱
listValues.add(phoneType[tm.getPhoneType()]);//獲取手機制式
listValues.add(tm.getCellLocation().toString());//獲取設備當前位置
另外網絡的判斷,判斷是否鏈接上了網絡
class MyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// mobile 3G Data Network
NetworkInfo mobile = CM
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// wifi net
NetworkInfo wifi = CM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
try {
// 判斷網絡是否鏈接
// 若是3G網絡和wifi網絡都未鏈接,且不是處於正在鏈接狀態 則進入Network Setting界面 由用戶配置網絡鏈接
if (!mobile.isConnected() && !wifi.isConnected()) {
signals.setVisibility(View.GONE);
signals.setBackgroundResource(R.drawable.well);
tv_nettype.setVisibility(0);// 設置可見
tv_nettype.setText("未鏈接網絡,請鏈接可用網絡!");
tv_signal.setVisibility(View.VISIBLE);
tv_signal.setText("無信號");
tv_netname.setVisibility(View.VISIBLE);
tv_netname.setText("無名稱");
dispaly_ping.setVisibility(View.GONE);
hint.setVisibility(View.GONE);
// tv_setting.setText("當前未鏈接任何網絡是否重置?");
// tv_signal.setVisibility(View.GONE);
// tv_setting.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// startActivity(new Intent(
// Settings.ACTION_WIRELESS_SETTINGS));// 進入無線網絡配置界面
// }
// });
} else {
// 網絡已經鏈接
CM = (ConnectivityManager) ActivityAppList.this
.getSystemService(Context.CONNECTIVITY_SERVICE);
//得到wifi管理的服務
wifiManager = (WifiManager) ActivityAppList.this
.getSystemService(Context.WIFI_SERVICE);
//獲取網絡服務的
NetworkInfo networkInfo = CM.getActiveNetworkInfo();
//獲取網絡類型
int nettype = networkInfo.getType();
//若是是wifi
if (nettype == ConnectivityManager.TYPE_WIFI) {
// 當前使用的是
tv_nettype.setVisibility(0);
tv_nettype.setText("當前網絡鏈接類型:WIFI");
tv_netname.setVisibility(0);
signals.setVisibility(View.VISIBLE);
tv_signal.setVisibility(View.VISIBLE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int speed = wifiInfo.getLinkSpeed();// 獲取鏈接速度
int ipaddress = wifiInfo.getIpAddress(); // 獲取ip地址
int netid = wifiInfo.getNetworkId(); // 獲取網絡id
int netrssi = wifiInfo.getRssi();// 信號的強度
String macaddress = wifiInfo.getMacAddress();// 取得設備的mac地址
String netname = wifiInfo.getSSID(); // 獲取網絡名稱
getConfiguration();
System.out
.println("");
removeWifi("CMCC");
String wifiname=wifiInfo.getSSID();
int wifiId=0;
if ("CMCC".equals(wifiname)) {
wifiId=wifiInfo.getNetworkId();
System.out
.println(wifiId+"^^^^^^^");
wifiManager.removeNetwork(wifiId);
}else if("ChinaNet".equals(wifiname)){
wifiId=wifiInfo.getNetworkId();
System.out
.println(wifiId+"^^^^^^^");
wifiManager.removeNetwork(wifiId);
}else if("ChinaUnicom".equals(wifiname)){
wifiId=wifiInfo.getNetworkId();
System.out
.println(wifiId+"^^^^^^^");
wifiManager.removeNetwork(wifiId);
}else if("CMCC-EDU".equals(wifiname)){
wifiId=wifiInfo.getNetworkId();
System.out
.println(wifiId+"^^^^^^^");
}
System.out.println(netid+"net=====");
tv_netname.setText("網絡名稱:" + netname);
if (netrssi < 0 && netrssi > -50) {
signals.setBackgroundResource(R.drawable.good);
tv_signal.setText("網絡信號強度:" + "好");
} else if (netrssi < -50 && netrssi > -70) {
signals.setBackgroundResource(R.drawable.well);
tv_signal.setText("網絡信號強度:" + "通常");
} else if (netrssi < -70) {
tv_signal.setText("網絡信號強度:" + "較差");
signals.setBackgroundResource(R.drawable.bad);
}
} else {
tv_signal.setVisibility(View.VISIBLE);
signals.setVisibility(View.VISIBLE);
CM = (ConnectivityManager) ActivityAppList.this
.getSystemService(Context.CONNECTIVITY_SERVICE);
//網絡類型爲:數據鏈接
tv_nettype.setVisibility(View.VISIBLE);
tv_nettype.setText("當前網絡類型:數據鏈接");
tv_netname.setVisibility(View.GONE);
tel.listen(new PhoneStateMonitor(),
PhoneStateListener.LISTEN_SIGNAL_STRENGTHS
| PhoneStateListener.LISTEN_SERVICE_STATE);
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
獲取 手機型號 phoneName = Build.MANUFACTURER + "-" + Build.MODEL;
獲取SN號
package com.innofidei.createsn;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import com.innofidei.read.ReadSXTBChannel;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn;
private String path="mnt/sdcard/xxx.txt";
private TelephonyManager tm;
String sn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.btn1);
tm= (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
//sn=tm.getDeviceId();
sn = Secure.getString(MainActivity.this.getContentResolver(), Secure.ANDROID_ID);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//生成在SDcard sn號
File file=new File(path);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br;
FileWriter fw;
FileReader fr;
BufferedWriter bw;
StringBuffer sb = new StringBuffer();
try {
fw = new FileWriter(file);// 初始化輸出流
bw = new BufferedWriter(fw);// 初始化輸出字符流
bw.write(sn);// 寫文件
bw.flush();
bw.close();
fw.close();
fr = new FileReader(file);// 初始化輸入流
br = new BufferedReader(fr);// 初始化輸入字符流
sb.append(br.readLine().toString());// 按行讀文件
System.out.println("文件內容是:" + sb);
if (sb!=null) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(String.valueOf("獲取的SN號"))
.setMessage(String.valueOf("SN號已經獲取到「,sb)
.setPositiveButton("確 定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
}
}).show();
Toast.makeText(MainActivity.this, "SN號初始化完成"+sb.toString(), Toast.LENGTH_LONG).show();
}
br.close();
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
else {
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}android