/**
检测网络状态
*/
private boolean isNetWorkAvaiable(){
mConnMan = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
NetWorkInfo info = mConnMan.getActiveNetWork();
if(info==null){
return false;
}
reurn info.isContected();
}
/***
服务停掉自身
**/
stopSelf();
/**
轮询的方式(handler)
**/
private Handler handler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
if(isRunning){
Message msg = obtainMessage(LOOP);
sendMessagedelayed(msg,30*1000);
}
}
}
}
/***在androidmanifest.xml中注册service时,有一个android:process属性,如果这个属性以"."开头,则为此服务开启一个
全局的独立进程,如果以":"开头则为此服务开启一个为此应用私有的独立进程
*/
/**
取得版本的名字
**/
public static String getVerSionName(){
String versionName="";
try{
versionName = context.getPackageManager().getPackageInfo(context.getPackageName(),1
)
catch(PackageManager.NameNotFoundException e){
}
}
}
public static <T> T toObject(String result, Class<T> clazz)
{
if ((clazz == null) || (StringUtil.isEmpty(result))) {
return null;
}
try
{
Gson gson = new Gson();
return gson.fromJson(result, clazz);
} catch (Exception e) {
Log.e("JSON 转换异常!", e);
try {
return clazz.newInstance();
} catch (IllegalAccessException e1) {
Log.e("toObject IllegalAccessException 实例化异常", e1);
} catch (InstantiationException e1) {
Log.e("toObject IllegalAccessException 实例化异常", e1);
}
}
return null;
}
public void releae(){
if(this.request!=null){
this.request.abort();
}
}
/**
上传文件
/****
从对话框列表中移除dialog
*/
private static HashMap<String,Dialog> dialogs = new HashMap();
public static void dismissAlert(Context context){
Dialog dialog = (Dialog)dialogs.get(context.toString());
if(dialog != null&&dialog.isShowing()){
dialog.dismiss();
dialogs.remove(context.toString());
}
}
/***
根据uri下载安装软件
*/
private void setUp(String httpUrl){
Uri uri = Uri.parse(httpUrl);
Intent intent = new Intent(Intent.ACTION_VIEW,httpUrl);
startActivity();
}
**/
public String uploadFile(String upUrl, String filePath)
{
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
try {
URL url = new URL(upUrl);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" +
fileName + "\"" + end);
ds.writeBytes(end);
InputStream fStream = new FileInputStream(new File(filePath));
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
while ((length = fStream.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
fStream.close();
ds.flush();
InputStream is = con.getInputStream();
StringBuffer b = new StringBuffer();
int ch;
while ((ch = is.read()) != -1)
{
int ch;
b.append((char)ch);
}
ds.close();
return b.toString();
}
catch (Exception e) {
e.printStackTrace();
}return "";
}
/***
将从网络上获得的输入流进行解析
**/
public MenuEntity parse(InputStream stream){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Element root = docuemnt.getDocumentElement();
return traverse(root);
}
catch (Exception ex) {
ex.printStackTrace();
Log.e("解析xml异常,异常为:" + ex);
}
return null;
}
/****
下载文件
**/
private void download(String url ,File file){
FileOutputStream f = createOpustream(file);
try{
Inputstream in = download(url);
if (in == null)
return;
int fix = 0;
byte[] buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer)>0)){
f.write(buffer,0,len);
fix += len;
}
}
catch (MalformedURLException e) {
// e.printStackTrace();
Log.e(e.getMessage(), e);
deleteFile(file);
} catch (IOException e) {
// e.printStackTrace();
Log.e(e.getMessage(), e);
deleteFile(file);
} finally {
// Closes this stream.
StreamUtil.closeSilently(f);
if (conn != null) {
conn.disconnect();
}
}
}
private FileOutputStream createOpustream(File file){
try
{
FileOutputStream out = new FileOutputstream(file);
}catch(Exception e){
deleteFile(file);
}
}
private void deleteFile(File file){
if(file.exits()&&file!=null){
file.delete();
}
}
private Inputstream download(String url){
try{
URL u = new URL(url);
conn = (HttpURLConnection)u.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.connect();
return conn.getInputstream();
}catch (MalformedURLException e) {
// e.printStackTrace();
Log.e(e.getMessage(), e);
} catch (IOException e) {
// e.printStackTrace();
Log.e(e.getMessage(), e);
}
return null;
}
/***
判断网络状态
*/
public static boolean is3g2g(Context context){
ConnectivityManager connectvityManager = (ConnectivityManager)context
.getSystemService("connectivity");
NetWorkInfo info = connectivityManager.getActiveNetWorkInfo();
return (activeNetInfo!=null&&activeNetInfo.getType()==0);
}
/**
在onfling之中判断是否左滑
**/
private boolean isScrollingLeft(MotionEvent e1,MotionEvent e2){
if(e1 == null||e2 == null){
return false;
}
return e2.getX()>e1.getX();
}