获得屏幕的宽高:
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解析时间方法:
|
12小时制时间显示:
|
两者区别:yyyy-MM-dd HH:mm:ss ; yyyy-MM-dd hh:mm:ss
如下:
字母 | 日期或时间元素 | 表示 | 示例 |
---|---|---|---|
G | Era 标志符 | Text | AD |
y | 年 | Year | 1996 ; 96 |
M | 年中的月份 | Month | July ; Jul ; 07 |
w | 年中的周数 | Number | 27 |
W | 月份中的周数 | Number | 2 |
D | 年中的天数 | Number | 189 |
d | 月份中的天数 | Number | 10 |
F | 月份中的星期 | Number | 2 |
E | 星期中的天数 | Text | Tuesday ; Tue |
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 Time ; PST ; GMT-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);
- /**
- * 检测网络的连接状态,如果网络不可用,提示用户配置网络.
- *
- * @return
- */
- private boolean checkNetworkStatus() {
- boolean netStatus = false;
- ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- connManager.getActiveNetworkInfo();
- if (connManager.getActiveNetworkInfo() != null) {
- netStatus = connManager.getActiveNetworkInfo().isAvailable();
- }
- if (!netStatus) {
- Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage("是否对网络进行设置?");
- b.setPositiveButton("是", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- Intent mIntent = new Intent("/");
- ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
- mIntent.setComponent(comp);
- mIntent.setAction("android.intent.action.VIEW");
- startActivityForResult(mIntent, 0); // 如果在设置完成后需要再次进行操作,可以重写操作代码,在这里不再重写
- }
- }).setNeutralButton("否", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- dialog.cancel();
- }
- }).show();
- }
- return netStatus;
- }