在开发中经常用到的一些工具方法,慢慢积累,不用到网络上找大半天了~~~
package com.caojing.about;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Rect;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
public class Utils {
/**
* 取得当前时间,并且格式化成yyyy-MM-dd HH:mm:ss
* @return String
*/
public static String getFormatTime(){
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
/**
* 取得Manifest.xml配置文件中的android:versionCode
* @param context 上下文
* @return int 版本号
* @throws NameNotFoundException 找不到versionCode异常
*/
public static int getVersionCode(Context context) throws NameNotFoundException{
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
return pi.versionCode;
}
/**
* 取得Manifest.xml配置文件中的android:versionName
* @param context 上下文
* @return String 版本名
* @throws NameNotFoundException 找不到versionName异常
*/
public static String getVersionName(Context context) throws NameNotFoundException{
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
return pi.versionName;
}
/**
* 取得状态栏的高度
* @param Activity 当前Activity
* @return int 状态栏高度
*/
public static int getStatusBarHeight(Activity activity){
Rect rect = new Rect();
Window win = activity.getWindow();
win.getDecorView().getWindowVisibleDisplayFrame(rect);
return rect.top;
}
/**
* 设置Activity无标题全屏显示
* @param activity 当前Activity
*/
public static void setFullScreen(Activity activity){
activity.requestWindowFeature(Window.FEATURE_NO_TITLE); //设置无标题
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置全屏
}
/**
* 根据响应的组建View和Activity隐藏软键盘
* @param view 响应的View
* @param activity 当前Activity
*/
public static void hideInputMethod(View view,Activity activity)
{
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
/**
* 根据坐标值x,y打开默认的谷歌地图
* @param context 上下文
* @param x X坐标
* @param y Y坐标
*/
public static void openMap(Context context, float x, float y){
Uri uri = Uri.parse("geo:"+x+","+y);
//Uri uri = Uri.parse("geo:38.899533,-77.036476?q=street+address");//打开地图,并显示指定地址
Intent it = new Intent(Intent.ACTION_VIEW,uri);
context.startActivity(it);
}
/**
* 调用拨打电话功能,直接拨打
* @param context 上下文
* @param number 号码
*/
public static void doCall(Context context, String number){
Uri uri = Uri.parse("tel:" + number);
Intent it = new Intent(Intent.ACTION_CALL, uri);
context.startActivity(it);
}
/**
* 调用发送短信功能
* @param context 上下文
* @param number 号码
* @param content 内容
*/
public static void openSMS(Context context, String number, String content){
Uri uri = Uri.parse("smsto:" + number);
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", content);
context.startActivity(it);
}
/**
* 调用发送邮件功能
* @param context 上下文
* @param sendTo 收件人
* @param ccTo 抄送人
* @param subject 标题
* @param content 内容
*/
public static void openEmail(Context context, String[] sendTo,String[] ccTo,String subject,String content){
Intent it=new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, sendTo);
it.putExtra(Intent.EXTRA_CC, ccTo);
it.putExtra(Intent.EXTRA_TEXT, content);
it.putExtra(Intent.EXTRA_SUBJECT, subject);
it.setType("message/rfc822");
context.startActivity(Intent.createChooser(it, "Choose Email Client"));
}
/**
* 调用其他程序,注意,该程序必须存在
* @param context 上下文
* @param packageName 所要打开的程序包名
* @param packageAndMainActivity 所要打开的程序包名+主运行类名
*/
public static void openApplicationByPackageName(Context context, String packageName, String packageAndMainActivity){
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, packageAndMainActivity));
intent.setAction(Intent.ACTION_VIEW);
context.startActivity(intent);
}
//根据IMSI号码识别移动供应商
public static String getProvidersName(Context context) {
String ProvidersName = null;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String IMSI = tm.getDeviceId();//移动设备国际辨识码
// IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
ProvidersName = "中国移动";
} else if (IMSI.startsWith("46001")) {
ProvidersName = "中国联通";
} else if (IMSI.startsWith("46003")) {
ProvidersName = "中国电信";
}
return ProvidersName;
}
//获取电话号码
public static String getPhoneNumber(Context context){
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getLine1Number();
}
//获取手机型号
public static String getPhoneModel(){
return android.os.Build.MODEL;
}
//获取系统版本
public static String getPhoneVersion(){
return Build.VERSION.RELEASE;
}
//判断是否为null或者空
public static Boolean isEmptyOrNull(String str){
if(str != null) {
if (!str.equals("")){
return false;
}
}
return true;
}
/**
* 检测网络是否连接
*
* @return
*/
public boolean isNetConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo[] infos = cm.getAllNetworkInfo();
if (infos != null) {
for (NetworkInfo ni : infos) {
if (ni.isConnected()) {
return true;
}
}
}
}
return false;
}
/**
* 检测wifi是否连接
*
* @return
*/
public boolean isWifiConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null
&& networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
}
return false;
}
/**
* 检测3G是否连接
*
* @return
*/
public boolean is3gConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null
&& networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
}
return false;
}
/**
* 检测GPS是否打开
*
* @return
*/
public boolean isGpsEnabled(Context context) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
List<String> accessibleProviders = lm.getProviders(true);
for (String name : accessibleProviders) {
if ("gps".equals(name)) {
return true;
}
}
return false;
}
}
本文汇总了一系列在Android应用开发过程中常用的工具方法,包括时间格式化、版本信息获取、软键盘控制、地图调用等,旨在帮助开发者提高工作效率。
2882

被折叠的 条评论
为什么被折叠?



