常用的方法、知识(一)

来源:互联网 发布:ubuntu 目录分类 编辑:程序博客网 时间:2024/05/21 17:06

1、获取当前时间(包括年、月、日、时、分、秒):

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日  HH:mm:ss");Date curDate = new Date(System.currentTimeMillis());String str = formatter.format(curDate);

2、获取本地版本号:

public int getlocalVersion(){int localversion = 0;try {        PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);//如果是要获取versionName,就直接把versionCode换成versionNamelocalversion = info.versionCode;    } catch (PackageManager.NameNotFoundException e) {        e.printStackTrace();    }return localversion;}

3、判断是否有网络连接:

//基类public static boolean isNetworkAvailable(Context context) {    ConnectivityManager connectivity = (ConnectivityManager) context            .getSystemService(Context.CONNECTIVITY_SERVICE);if (connectivity != null) {        NetworkInfo info = connectivity.getActiveNetworkInfo();if (info != null && info.isConnected()) {// 当前网络是连接的if (info.getState() == NetworkInfo.State.CONNECTED) {// 当前所连接的网络可用return true;            }        }    }return false;}
//调用private void isnettest() {boolean isnet = BaseActivity.isNetworkAvailable(Home.this);if (isnet == false) {        Toast.makeText(Home.this, "请检查你的网络连接", Toast.LENGTH_SHORT).show();    } else {        Thread weatherThread = new Thread(new WeatherThread());        weatherThread.start();    }}

4、截屏、调用系统分享图片、文字

ScreenShot screenShot = new ScreenShot();Bmp = screenShot.GetandSaveCurrentImage(Report.this);String str = getSDCardPath()+"/PrintScreenDemo/ScreenImage/Screen_0.png";Intent shareIntent = new Intent(Intent.ACTION_SEND);File file = new File(str);shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));shareIntent.setType("image/png");startActivity(Intent.createChooser(shareIntent, "title"));private String getSDCardPath(){    File sdcardDir = null;    //判断SDCard是否存在    boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);    if(sdcardExist){        sdcardDir = Environment.getExternalStorageDirectory();    }    return sdcardDir.toString();}

5、正则判断,是否为手机登录:

//方法public static boolean isMobileNO(String mobiles) {/*移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188联通:130、131、132、152、155、156、185、186电信:133、153、180、189、(1349卫通)总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9*/String telRegex = "[1][358]\\d{9}";//"[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。if (TextUtils.isEmpty(mobiles)) return false;else return mobiles.matches(telRegex);}
//调用if(!isMobileNO(username)){Toast.makeText(SetUser.this, "请用手机号码注册", Toast.LENGTH_LONG).show();}

6、切换Activity的时候,加载动画:

case R.id.login_base_rl_back:    startActivity( new Intent(this, Forget.class));    finish();    overridePendingTransition(R.anim.push_right_in,            R.anim.push_right_out);break;

给整个APP修改切换动画,在style里面写,然后在清单里面修改主题:

<!-- Base application theme. --><style name="AppTheme1" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. -->    <!-- 设置没有标题 --><item name="android:windowNoTitle">true</item><!-- 设置activity切换动画 --><item name="android:windowAnimationStyle">@style/activityAnimation</item></style><!-- animation 样式 --><style name="activityAnimation" parent="@android:style/Animation">    <item name="android:activityOpenEnterAnimation">@anim/slide_right_in</item>    <item name="android:activityOpenExitAnimation">@anim/slide_left_out</item>    <item name="android:activityCloseEnterAnimation">@anim/slide_left_in</item>    <item name="android:activityCloseExitAnimation">@anim/slide_right_out</item></style>

7、定时任务,半个小时请求一次服务器,返回数据:

handler.postDelayed(runnable, TIME); //每隔半小时执行Runnable runnable = new Runnable() {@Overridepublic void run() {// handler自带方法实现定时器try {handler.postDelayed(this, TIME);            Thread weatherThread = new Thread(new WeatherThread());            weatherThread.start();        } catch (Exception e) {            e.printStackTrace();        }    }};
0 0