android 常用的

// 获取编辑框焦点
editText.setFocusable(true);
//打开软键盘
InputMethodManager imm = (InputMethodManager) ctx
  .getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

//关闭软键盘
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
posted @ 2012-09-08 19:43 程序学习笔记 阅读(10) 评论(0) 编辑
   /**
     * 测试网络是否已连接
     * 
     * @param retryCount
     *            重试次数,如果为0,则不重试
     * @param retryDelayTimes
     *            重试时间间隔(单位:秒)
     * @return true表示已连接,否则为false
     */
    public boolean testNetConnected(int retryCount, int retryDelayTimes) {
        boolean flag = isNetConnected();// 网络是否已连接
        try {
            for (int i = 0; !flag && i < retryCount; i++) {
                Thread.sleep(retryDelayTimes * 1000);
                flag = isNetConnected();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return flag;
    }
    
    /**
     * 判断网络是否已连接
     * TODO: 这里的代码需要整理,实际上是判断手机是否正常连接网络。WIFI或者GPRS。
     * @return true表示已连接,否则为false
     */
    private boolean isNetConnected() {
        boolean flag = false;
        ConnectivityManager cm = (ConnectivityManager) LawCaseEditorActivity.this
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (null != cm) {
            NetworkInfo nif = cm.getActiveNetworkInfo();
            if (null != nif && nif.isConnected()) {
                if (nif.getState() == NetworkInfo.State.CONNECTED) {
                    flag = true;
                }
            }
        }
        return flag;
    }
posted @ 2012-09-08 19:01 程序学习笔记 阅读(13) 评论(0) 编辑
调用系统拍照
// 如果点击的是拍照按钮
case R.id.btn_map_camera: {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAMERA_REQUESTCODE);
        }
            break;
// 调用系统的方法,进入拍照界面

    /**
     * 回调和拍照功能
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            switch (requestCode) {
            case CAMERA_REQUESTCODE:
                if (resultCode == Activity.RESULT_OK && data != null) {
                    Bitmap image = (Bitmap) data.getExtras().get("data");
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    image.compress(CompressFormat.JPEG, 100, bos);
                    byte[] imagebytes = bos.toByteArray();// 照片信息

        // 这里开始的第二部分,获取图片的路径:
        String[] proj = { MediaStore.Images.Media.DATA };
        // 好像是android多媒体数据库的封装接口,具体的看Android文档
        Cursor cursor = managedQuery(originalUri, proj, null, null, null);
        // 按我个人理解 这个是获得用户选择的图片的索引值
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        // 将光标移至开头 ,这个很重要,不小心很容易引起越界
        cursor.moveToFirst();
        // 最后根据索引值获取图片路径
        String path = cursor.getString(column_index);
        File f = new File(path);
        if(f.exists()){
        f.delete();

                    Bundle bundle = new Bundle();
                    bundle.putByteArray(CameraActivity.IMAGE_BUFFER, imagebytes);
                    Intent intent = new Intent();
                    intent.putExtras(bundle);
                    intent.setClass(MapActivity.this, CameraActivity.class);
                    startActivity(intent);
                }
                break;
            default:
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
posted @ 2012-09-08 18:41 程序学习笔记 阅读(14) 评论(0) 编辑
   /**
     * 判断SD卡的剩余容量
     * 
     */
    public long isAvaiableSpace(int sizeMb) {
        long availableSpare = 0;
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            String sdcard = Environment.getExternalStorageDirectory().getPath();
            StatFs statFs = new StatFs(sdcard);
            long blockSize = statFs.getBlockSize();
            long blocks = statFs.getAvailableBlocks();
            availableSpare = (blocks * blockSize) / (1024 * 1024);
            Log.d("剩余空间", "availableSpare = " + availableSpare);
        }
        return availableSpare;
    }
posted @ 2012-09-08 18:29 程序学习笔记 阅读(12) 评论(0) 编辑
    /**
     *  获取时间差xx小时xx分钟前
     *@param newTime 新时间 2012-6-12 13:57:43
     *@param oldTime 老时间 2012-6-12 10:52:48
     *@return    描述
     *@author hongj
     */
    private String getTimeGap(String newTime,String oldTime){
        String hDes = "";
        String mDes = "";
        String[] newtime = newTime.split(":");
        int newH = Integer.parseInt(newtime[0]);
        int newM = Integer.parseInt(newtime[1]);
        
        String[] oldtime = oldTime.split(":");
        int oldH = Integer.parseInt(oldtime[0]);
        int oldM = Integer.parseInt(oldtime[1]);
        
        int h = newH - oldH;
        int m = newM - oldM;
        int i = 0;
        int k = 0;
        if (0 < h) {
            if(0 < m){
                hDes = h+"小时";
                mDes = m+"分钟";
            }else if(0 > m){
                i = 60 - oldM + newM;
                mDes = i + "分钟";
                if(1 < h){
                    k = h - 1;
                    hDes = k+"小时";
                }
            }else if(0 == m){
                hDes = h+"小时";
            }
        }else if(0 < m){
            mDes = m+"分钟";
        }
        return hDes+mDes+"前";
    }
posted @ 2012-09-08 18:24 程序学习笔记 阅读(14) 评论(0) 编辑
<shape>  
    <!-- 实心 -->  
    <solid android:color="#ff9d77"/>  
    <!-- 渐变 -->  
    <gradient  
        android:startColor="#ff8c00"  
        android:endColor="#FFFFFF"  
        android:angle="270" />  
    <!-- 描边 -->  
    <stroke  
        android:width="2dp"  
        android:color="#dcdcdc" />  
    <!-- 圆角 -->  
    <corners  
        android:radius="2dp" />  
    <padding  
        android:left="10dp"  
        android:top="10dp"  
        android:right="10dp"  
        android:bottom="10dp" />  
</shape>  


solid:实心,就是填充的意思
android:color指定填充的颜色

gradient:渐变
android:startColor和android:endColor分别为起始和结束颜色,ndroid:angle是渐变角度,必须为45的整数倍。
另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。

stroke:描边
android:width="2dp" 描边的宽度,android:color 描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp"

android:dashGap="3dp"
其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。

corners:圆角
android:radius为角的弧度,值越大角越圆。
我们还可以把四个角设定成不同的角度,方法为:

<corners  
  
        android:topRightRadius="20dp"    右上角  
        android:bottomLeftRadius="20dp"    右下角  
        android:topLeftRadius="1dp"    左上角  
        android:bottomRightRadius="0dp"    左下角  
/>  


这里有个地方需要注意,bottomLeftRadius是右下角,而不是左下角,这个有点郁闷,不过不影响使用,记得别搞错了就行。
还有网上看到有人说设置成0dp无效,不过我在测试中发现是可以的,我用的是2.2,可能修复了这个问题吧,如果无效的话那就只能设成1dp了。

padding:间隔
这个就不用多说了,XML布局文件中经常用到。


大体的就是这样,以下是一个使用的具体示例:用在Selector中作为Button的背景,分别定义了按钮的一般状态、获得焦点状态和按下时的状态,具体代码如下:

main.xml:  
<Button  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="TestShapeButton"  
    android:background="@drawable/button_selector"  
    />  
>  


button_selector.xml:

<?xml version="1.0" encoding="utf-8"?>  
<selector  
    xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_pressed="true" >  
        <shape>  
            <!-- 渐变 -->  
            <gradient  
                android:startColor="#ff8c00"  
                android:endColor="#FFFFFF"  
                android:type="radial"  
                android:gradientRadius="50" />  
            <!-- 描边 -->  
            <stroke  
                android:width="2dp"  
                android:color="#dcdcdc"  
                android:dashWidth="5dp"  
                android:dashGap="3dp" />  
            <!-- 圆角 -->  
            <corners  
                android:radius="2dp" />  
            <padding  
                android:left="10dp"  
                android:top="10dp"  
                android:right="10dp"  
                android:bottom="10dp" />  
        </shape>  
    </item>  
    <item android:state_focused="true" >  
        <shape>  
            <gradient  
                android:startColor="#ffc2b7"  
                android:endColor="#ffc2b7"  
                android:angle="270" />  
            <stroke  
                android:width="2dp"  
                android:color="#dcdcdc" />  
            <corners  
                android:radius="2dp" />  
            <padding  
                android:left="10dp"  
                android:top="10dp"  
                android:right="10dp"  
                android:bottom="10dp" />  
        </shape>  
    </item>  
    <item>        
        <shape>  
            <solid android:color="#ff9d77"/>  
            <stroke  
                android:width="2dp"  
                android:color="#fad3cf" />  
            <corners  
                android:topRightRadius="5dp"  
                android:bottomLeftRadius="5dp"  
                android:topLeftRadius="0dp"  
                android:bottomRightRadius="0dp"  
            />  
            <padding  
                android:left="10dp"  
                android:top="10dp"  
                android:right="10dp"  
                android:bottom="10dp" />  
        </shape>  
    </item>  
</selector>
posted @ 2012-09-08 18:19 程序学习笔记 阅读(21) 评论(0) 编辑
   /**
     * double 类型取后面N位小数 N自定义.
     * @param nodesTemp
     * @return
     */
    public static String getNumDouble(double dou, int num) {
        String retValue = null;
        DecimalFormat df = new DecimalFormat();
        df.setMinimumFractionDigits(0);
        df.setMaximumFractionDigits(num);
        retValue = df.format(dou);
        retValue = retValue.replaceAll(",", "");
        return retValue;
    }
posted @ 2012-09-08 18:16 程序学习笔记 阅读(12) 评论(0) 编辑
  /**
     *  把"yyyy-MM-dd HH:mm:ss"格式日期转换成毫秒
     *@param strDate
     *@return 转换后毫秒的值
     *@author hongj
     */
    public long paseDateTomillise(String strDate){
        String year = null;
        String month = null;
        String day = "";
        String hms = "";
        if(strDate.contains(" ") && !strDate.endsWith(" ")){
            String[] s = strDate.split(" ");
            hms = s[1];
        }
        String[] getYear = strDate.split("-");
        year = getYear[0].substring(2, 4);
        month = getYear[1];
        if("1".equalsIgnoreCase(month) || "01".equalsIgnoreCase(month)){
            month = "JAN";
        }else if("2".equalsIgnoreCase(month) || "02".equalsIgnoreCase(month)){
            month = "FEB";
        }else if("3".equalsIgnoreCase(month) || "03".equalsIgnoreCase(month)){
            month = "MAR";
        }else if("4".equalsIgnoreCase(month) || "04".equalsIgnoreCase(month)){
            month = "APR";
        }else if("5".equalsIgnoreCase(month) || "05".equalsIgnoreCase(month)){
            month = "MAY";
        }else if("6".equalsIgnoreCase(month) || "06".equalsIgnoreCase(month)){
            month = "JUN";
        }else if("7".equalsIgnoreCase(month) || "07".equalsIgnoreCase(month)){
            month = "JUL";
        }else if("8".equalsIgnoreCase(month) || "08".equalsIgnoreCase(month)){
            month = "AUG";
        }else if("9".equalsIgnoreCase(month) || "09".equalsIgnoreCase(month)){
            month = "SEPT";
        }else if("10".equalsIgnoreCase(month)){
            month = "OCT";
        }else if("11".equalsIgnoreCase(month)){
            month = "NOV";
        }else if("12".equalsIgnoreCase(month)){
            month = "DEC";
        }
        if(getYear[2].contains(" ")){
            day = getYear[2].split(" ")[0];
        }else{
            day = getYear[2];
        }
        String datees = day+"-"+month+"-"+year+" "+hms;
        Date datee = new Date(datees);
        return datee.getTime();
    }
posted @ 2012-09-08 18:15 程序学习笔记 阅读(21) 评论(0) 编辑
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
    public static void main(String[] args) {
        System.out.println(StringUtils.replaceBlank("just do it!"));
    }
    /*-----------------------------------

    笨方法:String s = "你要去除的字符串";

            1.去除空格:s = s.replace('\\s','');

            2.去除回车:s = s.replace('\n','');

    这样也可以把空格和回车去掉,其他也可以照这样做。

    注:\n 回车(\u000a) 
    \t 水平制表符(\u0009) 
    \s 空格(\u0008) 
    \r 换行(\u000d)*/
}
posted @ 2012-09-08 18:14 程序学习笔记 阅读(14) 评论(0) 编辑
针对定位服务,android的API里提供了LocationManager这么一个类

通过getLastKnownLocation(String provider)以及requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)方法可以获取到当前位置

此类提供两种定位方式:GPS定位和网络定位(基站+WIFI)

GPS定位的provider是LocationManager.GPS_PROVIDER,   网络定位则是LocationManager.NETWORK_PROVIDER

不过这两个接口都受限于系统设置,如下图:

如果上面两个开关都关了,自然就无法获取到GPS经纬度了(如何摆脱这种限制通过其他方式获取经纬度在下一章再介绍)

(假定开关都打开的前提)

 

GPS定位的获取方式

通过getLastKnownLocation(String provider)传对应参数,此时得到的Location并非当前的GPS位置信息,而是上一次获取到的位置信息

而requestLocationUpdates才是真正去请求位置信息的更新,可以理解为调用该方法后,会安装指定的规则去收集GPS信息,例如你请求locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,30 * 1000, 0, myListenGPS);

则每隔三十秒钟会收集一次GPS信息,如果收集到的话,会保存到系统存储中(保存至系统存储属个人理解)并通知myListenGPS监听器位置信息改变同时调用监听器onLocationChanged(Location location),那么每次我们调用getLastKnownLocation事实上就是从这个系统存储中获取这个信息,所以如果我们不调用此方法,getLastKnownLocation获取到的就

可能是N天以前的位置信息了。

 

网络定位的获取方式

其实同GPS差不多,保存到系统存储中的位置自然是跟GPS分开的。

 

二者区别

GPS定位准确,但在室内几乎无法定位而导致无法收集信息,即有定位盲区;网络定位偏差较大,但无盲区,只要有网络一般都可以收集的到;

另外经过本人手机实测,如果requestLocationUpdates的时候设置最少更新时间1S,最小更新距离为0的时候,GPS定位在有信号的情况下的确是每秒都刷新位置,

但网络定位大概是四五十秒更新一次(可能跟手机信号有关),如果把它设置为60秒的话,基本上两种定位方式都在六七十秒后更新位置信息

所以这个时间还是有些偏差的,这点大家务必要注意

部分童鞋可能对网络定位(基站+WIFI)不是很理解,简单来说就是你当前接入WIFI就使用WIFI定位,当前接入2G或3G网就是基站定位

其实针对基站和WIFI有单独的定位方式,只不过系统帮我们封装了这么个智能的方法,好是好就是受限于设置开关

为了达到实时定位的功能,现在广为流传的是GPS+基站定位方式,该基站定位即单独的定位方式,不受开关限制,所以在各个应用中用得比较多

如何基站定位且听下回分解


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值