public class MainActivity extends Activity {
private String LOG_PATH_ka = "/sdcard/PHONEINFO/";
public static final String SDPATH = Environment
.getExternalStorageDirectory().getAbsolutePath();
public static final String LOG_PATH = SDPATH + "/Android/data/getinfo/";
public static final String LOG_NAME = "info.txt";
Button getInfo;
TextView moble,yunying,mac,imei,imsi,xinghao,type;
String info="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getView();
getInfo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SIMCardInfo siminfo = new SIMCardInfo(MainActivity.this);
moble.setText("电话号码:"+siminfo.getNativePhoneNumber());
yunying.setText("运营商:"+siminfo.getProvidersName());
mac.setText("MAC:"+getMacAddress());
imei.setText("IMEI:"+getInfo()[0]);
imsi.setText("IMSI:"+getInfo()[1]);
xinghao.setText("型号:"+getInfo()[2]);
type.setText("联网方式:"+getWebType());
info+="\n";
info+="PhoneNum:"+siminfo.getNativePhoneNumber()+"\n";
info+="Operators:"+siminfo.getProvidersName()+"\n";
info+="MAC:"+getMacAddress()+"\n";
info+="IMEI:"+getInfo()[0]+"\n";
info+="IMSI:"+getInfo()[1]+"\n";
info+="Type:"+getInfo()[2]+"\n";
info+="Networking:"+getWebType()+"\n";
Log.e("info", info);
//获取手机信息
String phoneInfo = "Product: " + android.os.Build.PRODUCT+"\n";
phoneInfo += "CPU_ABI: " + android.os.Build.CPU_ABI+"\n";
phoneInfo += "TAGS: " + android.os.Build.TAGS+"\n";
phoneInfo += "VERSION_CODES.BASE: " + android.os.Build.VERSION_CODES.BASE+"\n";
phoneInfo += "MODEL: " + android.os.Build.MODEL+"\n";
phoneInfo += "SDK: " + android.os.Build.VERSION.SDK+"\n";
phoneInfo += "VERSION.RELEASE: " + android.os.Build.VERSION.RELEASE+"\n";
phoneInfo += "DEVICE: " + android.os.Build.DEVICE+"\n";
phoneInfo += "DISPLAY: " + android.os.Build.DISPLAY+"\n";
phoneInfo += "BRAND: " + android.os.Build.BRAND+"\n";
phoneInfo += "BOARD: " + android.os.Build.BOARD+"\n";
phoneInfo += "FINGERPRINT: " + android.os.Build.FINGERPRINT+"\n";
phoneInfo += "ID: " + android.os.Build.ID+"\n";
phoneInfo += "MANUFACTURER: " + android.os.Build.MANUFACTURER+"\n";
phoneInfo += "USER: " + android.os.Build.USER+"\n";
info+=phoneInfo;
new Thread(){
@Override
public void run(){
try
{
String urlPath="http://126.575.466.php";
//if(RequestService.postRequest(urlPath, info))
if(post(urlPath, info))
{
Toast.makeText(MainActivity.this, "提交成功!", Toast.LENGTH_LONG).show();
Log.e("info", "成功");
}
}
catch (Exception e)
{
Log.e("info", "失败");
Toast.makeText(MainActivity.this,"提交失败!", Toast.LENGTH_LONG).show();
Log.e("ERRORS", e.toString());
e.printStackTrace();
}
}
}.start();
saveFile(info, LOG_PATH_ka+LOG_NAME);
}
});
}
@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;
}
/**
* 获取view
*/
public void getView(){
getInfo=(Button) this.findViewById(R.id.getSIMInfo);
moble=(TextView) this.findViewById(R.id.moble);
yunying=(TextView) this.findViewById(R.id.yunying);
mac=(TextView) findViewById(R.id.mac);
imei=(TextView) findViewById(R.id.imei);
imsi=(TextView) findViewById(R.id.iesi);
xinghao=(TextView) findViewById(R.id.xinghao);
type=(TextView) findViewById(R.id.type);
}
/**
* 获取mac地址ַ
* @return
*/
private String getMacAddress(){
String result = "";
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
result = wifiInfo.getMacAddress();
Log.i("mac:", "macAdd:" + result);
return result;
}
/**
* 获取电话号码,imei,imsi等
* @return
*/
private String[] getInfo() {
TelephonyManager mTm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
String imei = mTm.getDeviceId();
String imsi = mTm.getSubscriberId();
String mtype = android.os.Build.MODEL; //
String numer = mTm.getLine1Number(); //获取手机号
String[] strs={imei,imsi,mtype};
return strs;
}
//��ȡcpu��Ϣ
private String[] getCpuInfo() {
String str1 = "/proc/cpuinfo";
String str2 = "";
String[] cpuInfo = {"", ""}; //1-cpu�ͺ� //2-cpuƵ��
String[] arrayOfString;
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
for (int i = 2; i < arrayOfString.length; i++) {
cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
}
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
cpuInfo[1] += arrayOfString[2];
localBufferedReader.close();
} catch (IOException e) {
}
Log.i("cpu:", "cpuinfo:" + cpuInfo[0] + " " + cpuInfo[1]);
return cpuInfo;
}
/**
* 获取网络连接方式
* @return string
*/
private String getWebType(){
ConnectivityManager cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
String i=info.getSubtypeName();
String typeName = info.getTypeName(); //cmwap/cmnet/wifi/uniwap/uninet
return typeName+i;
}
/**
* /执行post方法
* @param urlPath 接口地址
* @param str 上传的字符串
* @return true or false
* @throws IOException
* @throws JSONException
*/
public boolean post(String urlPath,String str) throws IOException, JSONException{
String map=str;
StringBuilder builder=new StringBuilder();
byte b[]=map.getBytes();
Log.e("info", "info长度:"+b.length);
URL url=new URL(urlPath);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setReadTimeout(5*1000);
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length",String.valueOf(b.length));
OutputStream outStream=con.getOutputStream();
outStream.write(b);
outStream.flush();
outStream.close();
if(con.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* /把日志保存到本地为txt格式
* @param str 保存的内容
* @param path保存路径
*/
public void saveFile(String str,String path) {
File file=new File(path);
if(!file.exists()){
File dir=new File(file.getParent());
dir.mkdir();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
FileOutputStream outStream=new FileOutputStream(file);
outStream.write(str.getBytes());
outStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
android 获取手机信息,并且将结果post到服务器和保存到本地
最新推荐文章于 2021-05-26 04:48:26 发布