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

android 小工具

2015-12-28 18:35 405 查看
当运行到真机,写sdcard 日志

package com.example.android;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

public class ADGD {

/**
* 启动分析
* @param context 上下文
* @param channel 渠道号
*/
public void dataAnalysis(Context context, String channel) {

String date = getCurrent();
do_analysis(channel, context);

}

private ADGD() {
};

private static ADGD adgd = new ADGD();

/**
* 单例实例化
*
* @return
*/
public static ADGD getInstance() {

return adgd;
}

private String fileName = "adgd_share";
private String fileName_key = "adgd_share_key";

public void initShare(Context context) {
share = context.getSharedPreferences(fileName,
Context.MODE_WORLD_WRITEABLE);
edit = share.edit();
}

private SharedPreferences share;
private SharedPreferences.Editor edit;
String tmp="";
String param="";
private void do_analysis(final String channel, final Context context) {
Toast.makeText(context, "do....."+ channel, 1).show();
tmp = "http://123.571.49.5:8087/playcool/m/?from=play";
param="&channel="
+ channel + "" + getPhoneInfo(context);
Toast.makeText(context, "1 "+tmp, 1).show();
new Thread(new Runnable() {

@Override
public void run() {
try {
// System.out.println("ADGD.dataAnalysis() to send....");
Log.i("adgd..", "ADGD.dataAnalysis() to send....");
long l=System.currentTimeMillis();

Log.i("adgd..", "ADGD.dataAnalysis() to cost...."+(System.currentTimeMillis()-l));
// tmp = sendGetHttp(tmp);
tmp=sendPost(tmp, param);
// Toast.makeText(context, "return "+tmp, 1).show();
System.out.println("ADGD.dataAnalysis() return" + tmp);

} catch (Exception e) {

e.printStackTrace();writeFileToSD(getErrorInfoFromException(e));
System.out.println("ADGD.dataAnalysis() is error");
}
}
}).start();
}

private String getPhoneInfo(Context context) {
try {
// 获得宽高

DisplayMetrics dm = new DisplayMetrics();
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels ;
int height = dm.heightPixels ;
// 获得imei
String imei = getPhoneIME(context);
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String mtype = android.os.Build.MODEL; // 手机型号
String brand = android.os.Build.BRAND+mtype;// 手机品牌
String sdk = android.os.Build.VERSION.SDK;// 手机品牌
String SDK_INT =""+ android.os.Build.VERSION.SDK_INT;// 手机品牌
String nettype = checkNetworkType(context);
return "&imei=" + imei + "&brand=" + brand + "&sdk=" + sdk
+ "&width=" + width + "&height=" + height + "&nettype="
+ nettype+"";
} catch (Exception e) {
e.printStackTrace();
writeFileToSD(e.getMessage());
return "";
}

}private static String getErrorInfoFromException(Exception e) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return "\r\n" + sw.toString() + "\r\n";
} catch (Exception e2) {
return "bad getErrorInfoFromException";
}
}

private static String getPhoneIME(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();

}
private void writeFileToSD(String str) {
String sdStatus = Environment.getExternalStorageState();
if(!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
Log.d("TestFile", "SD card is not avaiable/writeable right now.");
return;
}
try {
String pathName="/sdcard/";
String fileName="playcoo2l_log.txt";
File path = new File(pathName);
File file = new File(pathName + fileName);
if( !path.exists()) {
Log.d("TestFile", "Create the path:" + pathName);
path.mkdir();
}
if( !file.exists()) {
Log.d("TestFile", "Create the file:" + fileName);
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(file);
String s = str;
byte[] buf = s.getBytes();
stream.write(buf);
stream.close();

} catch(Exception e) {
Log.e("TestFile", "Error on writeFilToSD.");
e.printStackTrace();
}
}
/**
* 当前年
*
* @param amount
* @return
*/
private String getCurrent() {
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy");
return simpledateformat.format(new Date());

}

private String sendGetHttp(String url) throws Exception {
// System.out.println("sendGetHttp() "+a);
// url="http://baidu.com";
URL url2 = new URL(url);
// Log.i("http:send", "http send"+url);
HttpURLConnection httpUrl = (HttpURLConnection) url2.openConnection();
// 连接指定的网络资源
httpUrl.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpUrl.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
String lines;

while ((lines = reader.readLine()) != null) {
stringBuffer.append(lines);
}
return "" + stringBuffer.toString();

}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
private static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
/***
* 判断Network具体类型 other 其他 ,wifi ,mobile
*
* */
private static String checkNetworkType(Context mContext) {
try {
final ConnectivityManager connectivityManager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo mobNetInfoActivity = connectivityManager
.getActiveNetworkInfo();
if (mobNetInfoActivity == null || !mobNetInfoActivity.isAvailable()) {
// 注意一:
// NetworkInfo 为空或者不可以用的时候正常情况应该是当前没有可用网络,
// 但是有些电信机器,仍可以正常联网,
// 所以当成net网络处理依然尝试连接网络。
// (然后在socket中捕捉异常,进行二次判断与用户提示)。
// return TYPE_NET_WORK_DISABLED;
return "other";
} else {
// NetworkInfo不为null开始判断是网络类型
int netType = mobNetInfoActivity.getType();
if (netType == ConnectivityManager.TYPE_WIFI) {
// wifi net处理
return "wifi";
} else if (netType == ConnectivityManager.TYPE_MOBILE) {
return "mobile";
}
}

} catch (Exception ex) {
return "other";
}

return "other";

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