1.pinyinUtil(将汉字,英文,火星文转化成拼音,并提取首字母,需要用到pinyin4j第三方类库)
*package util;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class PinYinUtil {
public static String getPinYin(String username){
try {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
StringBuilder sb = new StringBuilder();
for(int i=0;i<username.length();i++){
String str = username.substring(i, i+1);
if(str.matches("[\u4E00-\u9FFF]")){
char c = str.charAt(0);
String[] py = PinyinHelper.toHanyuPinyinStringArray(c,format);
sb.append(py[0]);
}else if(str.matches("[a-zA-Z]")){
sb.append(str.toUpperCase());
}else{
sb.append("#");
}
}
return sb.toString();
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
throw new RuntimeException("不正确的拼音格式");
}
}
public static String getSortLetter(String string){
return getPinYin(string).substring(0,1);
}
}*
2.NetUtil(判断当前手机是否连接网络)
package util;
import java.util.List;
import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
public class Netutil {
/**
* 网络是否可用
*
* @param activity
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
/**
* Gps是否打开
*
* @param context
* @return
*/
public static boolean isGpsEnabled(Context context) {
LocationManager locationManager = ((LocationManager) context
.getSystemService(Context.LOCATION_SERVICE));
List<String> accessibleProviders = locationManager.getProviders(true);
return accessibleProviders != null && accessibleProviders.size() > 0;
}
/**
* wifi是否打开
*/
public static boolean isWifiEnabled(Context context) {
ConnectivityManager mgrConn = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mgrTel = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
}
/**
* 判断当前网络是否是wifi网络
* if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) {
*
* @param context
* @return boolean
*/
public static boolean isWifi(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
/**
* 判断当前网络是否3G网络
*
* @param context
* @return boolean
*/
public static boolean is3G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
}
3.SharePreference(偏好存储,用于存储一些用户对软件的设置信息,)
package util;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import app.MyApp;
public class SharePreferenceUtil {
public static Editor editor;
public SharedPreferences sp;
public SharePreferenceUtil(String filename){
sp = MyApp.context.getSharedPreferences(filename, Context.MODE_PRIVATE);
editor = sp.edit();
}
public SharePreferenceUtil(){
sp = PreferenceManager.getDefaultSharedPreferences(MyApp.context);
editor = sp.edit();
}
public boolean isAllowNotification(){
return sp.getBoolean("notification", true);
}
public boolean isAllowSound(){
return sp.getBoolean("sound", true);
}
public boolean isAllowVibrate(){
return sp.getBoolean("vibrate", true);
}
public void setAllowNotification(boolean flag){
editor.putBoolean("notification", flag);
editor.commit();
}
public void setAllowSound(boolean flag){
editor.putBoolean("sound", flag);
editor.commit();
}
public void setAllowVibrate(boolean flag){
editor.putBoolean("vibrate", flag);
editor.commit();
}
}
4.BaseAdapter(在写项目时经常要用到很多adapter,写一个父类baseadapter可以节省很多事)
package Adapter;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public abstract class MyBaseAdapter<E> extends BaseAdapter{
Context context;
List<E> lists;
LayoutInflater inflater;
public MyBaseAdapter(Context context, List<E> lists) {
super();
this.context = context;
this.lists = lists;
this.inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return lists.size();
}
@Override
public E getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getItemView(position, convertView, parent);
}
public abstract View getItemView(int position, View convertView, ViewGroup parent);
public void addAll(List<E> list,boolean isClear){
if(isClear){
lists.clear();
}
lists.addAll(list);
notifyDataSetChanged();
}
public void add(E e){
lists.add(e);
notifyDataSetChanged();
}
public void move(E e){
lists.remove(e);
notifyDataSetChanged();
}
}