android一些很有用的小工具

获得屏幕的宽高:

DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);
width = dm.widthPixels;

int height = dm.heightPixels;

判断SD存在的

Environment.getExternalStorageDirectory().toString()


httpclient下载图片的方法:

public String download(String path){
this.mPath = path;
try {
File file = getFromSDcard(mPath);
if (file.exists()) {
return file.getAbsolutePath();
}else{
file.createNewFile();
mHttpClient = new DefaultHttpClient();
final HttpGet getRequest = new HttpGet(mPath);
HttpResponse httpResponse = mHttpClient.execute(getRequest);
int statusCode = httpResponse.getStatusLine().getStatusCode();
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null&&statusCode==HttpStatus.SC_OK) {
InputStream inputStream = null;
long size = httpEntity.getContentLength();
inputStream = httpEntity.getContent();
saveToSDCard(mPath,inputStream, size);
return file.getAbsolutePath();
}else {
return null;
}
}
} catch (Exception e) {
 return null;
}finally{
if (mHttpClient != null) {
mHttpClient.getConnectionManager().shutdown();
}
}
       //return null;
}

public void saveToSDCard(String url, InputStream inputStream, long size) {
System.out.println("====xx saveToSDCard====> url="+url);
OutputStream outputStream = null;
try {
if (!Util.canSave(size)) {
return ;
}


File file = getFromSDcard(url);
if (!file.exists()) {
file.createNewFile();
}
outputStream = new FileOutputStream(file);
copyStream(inputStream, outputStream);


} catch (Exception e) {
return;
} finally {
if (outputStream != null) {
try {
//outputStream.close();
//inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

消除android换页的效果:

overridePendingTransition(0, 0);

SimpleDateFormat解析时间方法:

24小时制时间显示:
 

public class Datetime {

    public static void main(String args[]){
         java.util.Date current=new java.util.Date();
           java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
           String c=sdf.format(current);
           System.out.println(c);
    }
}

12小时制时间显示:


public class Datetime {

    public static void main(String args[]){
         java.util.Date current=new java.util.Date();
           java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
           String c=sdf.format(current);
           System.out.println(c);
    }
}

两者区别:yyyy-MM-dd HH:mm:ss ;  yyyy-MM-dd hh:mm:ss

如下:

字母 日期或时间元素 表示 示例
G Era 标志符 Text AD
y Year 199696
M 年中的月份 Month JulyJul07
w 年中的周数 Number 27
W 月份中的周数 Number 2
D 年中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text TuesdayTue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard TimePSTGMT-08:00
Z 时区 RFC 822 time zone -0800

获得应用的信息:

public static String getAppVersionName(Context context) {
String versionName = "";
try {
// Get the package info
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo("co.jp.bs.xxxx.activity", 0);(这个是第一个包的名称)
versionName = pi.versionName;
if (TextUtils.isEmpty(versionName)) {
return "";
}
} catch (Exception e) {
e.printStackTrace();
}
return versionName;
}

获取设备的UUid

public static String getUUid(Context context) {
SharedPreferences sp = context.getSharedPreferences(CONFIG, 0);
String uuid = sp.getString(UUID_KEY, "");
if ("".equals(uuid)) {
uuid = UUID.randomUUID().toString();
uuid = uuid.replaceAll("-", "") + "";
Editor ed = sp.edit();
ed.putString(UUID_KEY, uuid);
ed.commit();
}
return uuid;
}

得到SD卡的可利用大小:

public static long getAvailableExternalMemorySize() {
if (isSDCardExist()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
} else {
return ERROR;
}
}


  一、Activity和Task(栈)的关系:


  Task就像一个容器,而Activity就相当与填充这个容器的东西,第一个东西(Activity)则会处于最下面,最后添加的东西(Activity)则会在最低端。从Task中取出东西(Activity)则是从最顶端取出。

  二、界面跳转和服务的启动都会用到Intent,现在介绍Intent Flag是关于Activity的跳转
  Intent intent = new Intent(this,xxx.class);
  //如果activity在task存在,拿到最顶端,不会启动新的Activity
  intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
  //如果activity在task存在,将Activity之上的所有Activity结束掉
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  //默认的跳转类型,将Activity放到一个新的Task中
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  //如果Activity已经运行到了Task,再次跳转不会在运行这个Activity

  intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);



  1. /** 
  2.  * 检测网络的连接状态,如果网络不可用,提示用户配置网络. 
  3.  *  
  4.  * @return 
  5.  */  
  6. private boolean checkNetworkStatus() {  
  7.     boolean netStatus = false;  
  8.     ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  9.   
  10.     connManager.getActiveNetworkInfo();  
  11.   
  12.     if (connManager.getActiveNetworkInfo() != null) {  
  13.         netStatus = connManager.getActiveNetworkInfo().isAvailable();  
  14.     }  
  15.       
  16.     if (!netStatus) {  
  17.         Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage("是否对网络进行设置?");  
  18.         b.setPositiveButton("是"new DialogInterface.OnClickListener() {  
  19.   
  20.             public void onClick(DialogInterface dialog, int whichButton) {  
  21.                 Intent mIntent = new Intent("/");  
  22.                 ComponentName comp = new ComponentName("com.android.settings""com.android.settings.WirelessSettings");  
  23.                 mIntent.setComponent(comp);  
  24.                 mIntent.setAction("android.intent.action.VIEW");  
  25.                 startActivityForResult(mIntent, 0); // 如果在设置完成后需要再次进行操作,可以重写操作代码,在这里不再重写  
  26.             }  
  27.         }).setNeutralButton("否"new DialogInterface.OnClickListener() {  
  28.   
  29.             public void onClick(DialogInterface dialog, int whichButton) {  
  30.                 dialog.cancel();  
  31.             }  
  32.         }).show();  
  33.     }  
  34.   
  35.     return netStatus;  
  36. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值