最新Android开发实用代码片段(二),滴滴 hr面试

总结

找工作是个很辛苦的事情,而且一般周期都比较长,有时候既看个人技术,也看运气。第一次找工作,最后的结果虽然不尽如人意,不过收获远比offer大。接下来就是针对自己的不足,好好努力了。

最后为了节约大家的时间,我把我学习所用的资料和面试遇到的问题和答案都整理成了PDF文档

喜欢文章的话请关注、点赞、转发 谢谢!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

	bt = (Button) findViewById(R.id.bt1);

	getViewSize(bt);

	getViewSize2(bt);

	getViewSize3(bt);

	DisplayMetrics dmDisplayMetrics=new DisplayMetrics();

	getWindowManager().getDefaultDisplay().getMetrics(dmDisplayMetrics);

	Log.e("屏幕宽度---->", dmDisplayMetrics.widthPixels+"");



}



public void getViewSize(View v) {

	int w = View.MeasureSpec.makeMeasureSpec(0,

			View.MeasureSpec.UNSPECIFIED);

	int h = View.MeasureSpec.makeMeasureSpec(0,

			View.MeasureSpec.UNSPECIFIED);

	v.measure(w, h);

	int height = v.getMeasuredHeight();

	int width = v.getMeasuredWidth();

	Log.e("方法一---->", width + "  " + height);

}



public void getViewSize2(final View v) {

	ViewTreeObserver vto = v.getViewTreeObserver();

	vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

		public boolean onPreDraw() {

			int height = v.getMeasuredHeight();

			int width = v.getMeasuredWidth();

			Log.e("方法二---->", width + "  " + height);

			return true;

		}

	});

}



public void getViewSize3(final View v) {

	ViewTreeObserver vto2 = v.getViewTreeObserver();

	vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

		@Override

		public void onGlobalLayout() {

			v.getViewTreeObserver().removeGlobalOnLayoutListener(this);

			Log.e("方法三---->", v.getWidth() + "  " + v.getHeight());

		}

	});

}

}


界面就一个宽度为match\_parent,高度为50dp的bt.测试结果如下:



![](https://img-blog.csdn.net/20160606224844679?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)



同时,当我退出的时候,第二种方法还会进行测试2次。



我推荐使用第三种方法  

  



**2.按2次返回键退出程序**



    连续快速按2次返回键退出程序



    private long mkeyTime;

public boolean onKeyDown(int keyCode, KeyEvent event) {

	// TODO Auto-generated method stub

	if (keyCode == KeyEvent.KEYCODE_BACK) {

		if ((System.currentTimeMillis() - mkeyTime) > 2000) {

			mkeyTime = System.currentTimeMillis();

			Toast.makeText(this, "再按一次退出程序",Toast.LENGTH_LONG).show();

		} else {

			finish();

		}

		return true;

	}

	return super.onKeyDown(keyCode, event);

}



**3.txt文件转String和String写入到txt文件中**



(1)txt转String



public static String txt2String(File file){

    String result = "";

    try{

        BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件

        String s = null;

        while((s = br.readLine())!=null){//使用readLine方法,一次读一行

            result = result + "\n" +s;

        }

        br.close();

    }catch(Exception e){

        e.printStackTrace();

    }

    return result;

}



 (2)String写入到txt



/**

 * String写入文件

 * @param fileName

 * @param content

 * @return

 */

public static boolean writeToTxt(String fileName, String content)

{

    try

    {

        File newFile = new File(fileName);

        if (newFile.exists())

        {

            newFile.delete();

        }

        int iLen = Util.getLengthString(content);

        OutputStreamWriter write = null;

        BufferedWriter out = null;

        if (!TextUtils.isEmpty(fileName))

        {

            try

            {

                // new FileOutputStream(fileName, true) 第二个参数表示追加写入

                write = new OutputStreamWriter(new FileOutputStream(

                        fileName),Charset.forName("gbk"));//一定要使用gbk格式

                out = new BufferedWriter(write, iLen);

            }

            catch (Exception e)

            {

            }

        }

        out.write(content);

        out.flush();

        out.close();

        return true;

    }

    catch (Exception e)

    {

        return false;

    }

}

  



**4.drawable转Bitmap**



private Bitmap drawableToBitmap(Drawable drawable) {

    int dw = drawable.getIntrinsicWidth();

    int dh = drawable.getIntrinsicHeight();

    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

            : Bitmap.Config.RGB_565;

    Bitmap bitmap=Bitmap.createBitmap(dw, dh, config);

    Canvas canvas = new Canvas(bitmap);

    drawable.setBounds(0, 0, dw, dh);

    drawable.draw(canvas);

    return bitmap;

}

  



**5.调用系统拨号**



   有2种方式



第一种:没有拨号界面,直接拨号



Uri uri=Uri.parse(“tel:”+“13012345678”);

Intent intent=new Intent();

intent.setAction(Intent.ACTION_CALL);// ACTION_CALL直接拨出

intent.setData(uri);

startActivity(intent);




第二种:拨号界面了,号码也输入好了,但是要手动点击拨号



Uri uri=Uri.parse(“tel:”+“13012345678”);

Intent intent=new Intent();

intent.setAction(Intent.ACTION_DIAL);

intent.setData(uri);

startActivity(intent);




当然,不要忘了要加上权限






**6.调用系统短信界面**



第一种,设定发送的号码,和内容,界面没有联系人,群组组等按钮



            Uri smsToUri = Uri.parse("smsto:13012345678");// 联系人地址

            Intent mIntent = new Intent(android.content.Intent.ACTION_SENDTO,

                    smsToUri);

            mIntent.putExtra("sms_body", "短信内容test123");// 短信内容

            startActivity(mIntent);

效果如下:



**![](https://img-blog.csdn.net/20160708114721248?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)  

  

**



第二种,设定发送短信内容,不设置发送的号码,界面有联系人,群组等按钮



Uri smsUri = Uri.parse(“smsto:”);

Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);

intent.putExtra(“sms_body”, “短信内容”);

intent.setType(“vnd.android-dir/mms-sms”);

startActivity(intent);


  

**7.调用系统浏览器浏览网页**



Uri uri = Uri.parse(“http://www.baidu.com”);

Intent it = new Intent(Intent.ACTION_VIEW,uri);

startActivity(it);


  



**8.FileUtil工具类**



  主要包括:在指定的位置创建指定的文件;在指定的位置创建文件夹;删除指定的文件;删除指定的文件夹;复制文件/文件夹;移动指定的文件(夹)到目标文件(夹)。



/**

  • 文件操作工具类

*/

public class FileUtil {

/**

 * 在指定的位置创建指定的文件

 *

 * @param filePath 完整的文件路径

 * @param mkdir 是否创建相关的文件夹

 * @throws Exception

 */

public static void mkFile(String filePath, boolean mkdir) throws Exception {

    File file = new File(filePath);

    file.getParentFile().mkdirs();

    file.createNewFile();

    file = null;

}



/**

 * 在指定的位置创建文件夹

 *

 * @param dirPath 文件夹路径

 * @return 若创建成功,则返回True;反之,则返回False

 */

public static boolean mkDir(String dirPath) {

    return new File(dirPath).mkdirs();

}



/**

 * 删除指定的文件

 *

 * @param filePath 文件路径

 *

 * @return 若删除成功,则返回True;反之,则返回False

 *

 */

public static boolean delFile(String filePath) {

    return new File(filePath).delete();

}



/**

 * 删除指定的文件夹

 *

 * @param dirPath 文件夹路径

 * @param delFile 文件夹中是否包含文件

 * @return 若删除成功,则返回True;反之,则返回False

 *

 */

public static boolean delDir(String dirPath, boolean delFile) {

    if (delFile) {

        File file = new File(dirPath);

        if (file.isFile()) {

            return file.delete();

        } else if (file.isDirectory()) {

            if (file.listFiles().length == 0) {

                return file.delete();

            } else {

                int zfiles = file.listFiles().length;

                File[] delfile = file.listFiles();

                for (int i = 0; i < zfiles; i++) {

                    if (delfile[i].isDirectory()) {

                        delDir(delfile[i].getAbsolutePath(), true);

                    }

                    delfile[i].delete();

                }

                return file.delete();

            }

        } else {

            return false;

        }

    } else {

        return new File(dirPath).delete();

    }

}



/**

 * 复制文件/文件夹 若要进行文件夹复制,请勿将目标文件夹置于源文件夹中

 * @param source 源文件(夹)

 * @param target 目标文件(夹)

 * @param isFolder 若进行文件夹复制,则为True;反之为False

 * @throws Exception

 */

public static void copy(String source, String target, boolean isFolder)

        throws Exception {

    if (isFolder) {

        (new File(target)).mkdirs();

        File a = new File(source);

        String[] file = a.list();

        File temp = null;

        for (int i = 0; i < file.length; i++) {

            if (source.endsWith(File.separator)) {

                temp = new File(source + file[i]);

            } else {

                temp = new File(source + File.separator + file[i]);

            }

            if (temp.isFile()) {

                FileInputStream input = new FileInputStream(temp);

                FileOutputStream output = new FileOutputStream(target + "/" + (temp.getName()).toString());

                byte[] b = new byte[1024];

                int len;

                while ((len = input.read(b)) != -1) {

                    output.write(b, 0, len);

                }

                output.flush();

                output.close();

                input.close();

            }

            if (temp.isDirectory()) {

                copy(source + "/" + file[i], target + "/" + file[i], true);

            }

        }

    } else {

        int byteread = 0;

        File oldfile = new File(source);

        if (oldfile.exists()) {

            InputStream inStream = new FileInputStream(source);

            File file = new File(target);

            file.getParentFile().mkdirs();

            file.createNewFile();

            FileOutputStream fs = new FileOutputStream(file);

            byte[] buffer = new byte[1024];

            while ((byteread = inStream.read(buffer)) != -1) {

                fs.write(buffer, 0, byteread);

            }

            inStream.close();

            fs.close();

        }

    }

}



/**

 * 移动指定的文件(夹)到目标文件(夹)

 * @param source 源文件(夹)

 * @param target 目标文件(夹)

 * @param isFolder 若为文件夹,则为True;反之为False

 * @return

 * @throws Exception

 */

public static boolean move(String source, String target, boolean isFolder)

        throws Exception {

    copy(source, target, isFolder);

    if (isFolder) {

        return delDir(source, true);

    } else {

        return delFile(source);

    }

}

}




**9.获取sdk版本号**



public static int getAndroidSDKVersion(){

    int version;

    version = Integer.valueOf(android.os.Build.VERSION.SDK);

    return version;

}

  

**10.存储bitmap到指定路径**  

/**

 * bitmap存储

 * @param mBitmap

 * @param strPath

 */

public static void saveMyBitmap(Bitmap mBitmap, String strPath,boolean isRecycle)

{

    if(mBitmap!=null&&!mBitmap.isRecycled()&&!TextUtils.isEmpty(strPath))

    {

        File f = new File( strPath );

        if (f.exists())

        {

            f.delete();

        }

        FileOutputStream fOut = null;

        try {

            fOut = new FileOutputStream(f);

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        }

        mBitmap.compress(Bitmap.CompressFormat.JPEG, 60, fOut);

        try {

            fOut.flush();

        } catch (IOException e) {

            e.printStackTrace();

        }catch (NullPointerException e)

        {

            e.getMessage();

        }

最后

下面是辛苦给大家整理的学习路线

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

        }

        FileOutputStream fOut = null;

        try {

            fOut = new FileOutputStream(f);

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        }

        mBitmap.compress(Bitmap.CompressFormat.JPEG, 60, fOut);

        try {

            fOut.flush();

        } catch (IOException e) {

            e.printStackTrace();

        }catch (NullPointerException e)

        {

            e.getMessage();

        }

最后

下面是辛苦给大家整理的学习路线

[外链图片转存中…(img-ClqorVtp-1715368099962)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 24
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值