安卓开发笔记(一)

来源:互联网 发布:在线音乐软件 编辑:程序博客网 时间:2024/05/18 01:42

牛逼站点:http://www.templatesy.com/ 丰富酷炫的企业网站模板,
https://github.com/CraftsmenTech/AndBase 安卓快速开发框架,集成多种控件
https://github.com/Blankj/AndroidUtilCode/blob/master/README-CN.md 开发人员不得不收集的代码
https://github.com/zhousuqiang/TvRecyclerView 可以定制manager的TV列表
https://github.com/Dawish/BriskTVLauncher 很好的launcher
https://github.com/daniulive/SmarterStreaming 大牛直播方案
https://github.com/jeasonlzy/okhttp-OkGo 很好用的OKHTTP的封装
https://github.com/hongyangAndroid/AndroidAutoLayout 最佳适配方案,鸿洋制作
http://blog.csdn.net/self_study/article/details/51234377 讲安卓java设计模式
https://github.com/chrisjenx/Calligraphy 比较方便的设置textView字体第三方库
http://blog.csdn.net/self_study/article/details/47128773 安卓下存储相关的API
http://blog.csdn.net/zhcswlp0625/article/details/55211904 解决View不能直接setId
http://blog.csdn.net/w815878564/article/details/50941665 TV开发一些心得
https://github.com/LitePalFramework/LitePal 郭霖写的数据库框架
http://www.jianshu.com/p/82f8b58db53c retrofit基础介绍
http://www.jianshu.com/p/bd758f51742e retrofit+rxjava封装
https://github.com/square/leakcanary 内存泄露检查工具,配置很简单
https://my.oschina.net/sunlimiter/blog/344158 studio开发中的一些问题
http://mp.weixin.qq.com/s/xJtFkhEzKJGoOFQoSzqL5Q 鸿洋公众号收集的几个项目,可以系统的学习下
https://github.com/XXApple/AndroidLibs 开源效果的收集

https://github.com/dinuscxj/RecyclerItemDecoration 比较酷的recycleview带条目标题保持效果
http://gank.io/post/560e15be2dca930e00da1083 Rxjava的详细介绍
http://www.tianapi.com/#news 收集了各种API接口,自己练习用
http://www.cnblogs.com/tomsen/archive/2013/06/04/3115460.html CM10.1源码的下载和编
译(支持CyanogenMod官方列出的机型和三星S5660、三星S5830、LG-P509等低配机型)
http://blog.csdn.net/studyvcmfc/article/details/7338521 Http协议的详细介绍
http://p.codekk.com/ 安卓开源项目集合
http://www.androiddevtools.cn/ 安卓工具集合,不用翻墙
http://gityuan.com/2016/04/24/how-to-study-android/ 安卓学习路线
http://a.codekk.com/ 各种源码解析
https://github.com/GcsSloop/AndroidNote 安卓学习笔记整理,比较系统的介绍各个知识点,含金量比较高
http://blog.csdn.net/qq_30379689/article/details/52637226 安卓知识点集合
https://github.com/ynztlxdeai/Image2Video 图片转视频
http://www.qingcore.com/ 比较酷的一个博客
https://github.com/GcsSloop/AndroidNote/blob/master/Course/ReleaseLibraryByJitPack.md 发布开源项目
https://www.diycode.cc/ 自定义github里的徽章

1.

当Android Studio中编译java方面的代码,出现类似的警告:

有关详细信息, 请使用 -Xlint:deprecation 重新编译。
有关详细信息, 请使用 -Xlint:unchecked 重新编译。
时,去项目对应的build.gradle中添加配置:

allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << “-Xlint:unchecked” << “-Xlint:deprecation”
}
}
}
即可消除警告。

2.自定义对话框
private void showExitGameAlert(final Context context) {
final AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.show();
Window window = dlg.getWindow();
window.setContentView(R.layout.skgarden_toast_exit_ok);

     // 为确认按钮添加事件,执行退出应用操作     ImageButton ok = (ImageButton) window.findViewById(R.id.btn_exit);     ok.setOnClickListener(new View.OnClickListener() {      public void onClick(View v) {          dlg.dismiss();          finish();          }     });        }

3.异步任务
private void update(){
UpdateTextTask updateTextTask = new UpdateTextTask(this);
updateTextTask.execute();
}

class UpdateTextTask extends AsyncTask<Void,Integer,Integer>{      private Context context;      UpdateTextTask(Context context) {          this.context = context;      }      /**      * 运行在UI线程中,在调用doInBackground()之前执行      */      @Override      protected void onPreExecute() {          Toast.makeText(context,"开始执行",Toast.LENGTH_SHORT).show();      }      /**      * 后台运行的方法,可以运行非UI线程,可以执行耗时的方法      */      @Override      protected Integer doInBackground(Void... params) {          int i=0;          while(i<10){              i++;              publishProgress(i);              try {                  Thread.sleep(1000);              } catch (InterruptedException e) {              }          }          return null;      }      /**      * 运行在ui线程中,在doInBackground()执行完毕后执行      */      @Override      protected void onPostExecute(Integer integer) {          Toast.makeText(context,"执行完毕",Toast.LENGTH_SHORT).show();      }      /**      * 在publishProgress()被调用以后执行,publishProgress()用于更新进度      */      @Override      protected void onProgressUpdate(Integer... values) {          tv.setText(""+values[0]);      }  }  

4.网速显示实时
package com.a4kgarden.mynetspeed;

    import android.content.Context;    import android.net.TrafficStats;    import android.os.Handler;    import android.support.v7.app.AppCompatActivity;    import android.os.Bundle;    import android.widget.TextView;    import java.text.DecimalFormat;    import java.util.Timer;    import java.util.TimerTask;    public class MainActivity extends AppCompatActivity {        private long rxtxTotal = 0;        private long mobileRecvSum = 0;        private long mobileSendSum = 0;        private long wlanRecvSum = 0;        private long wlanSendSum = 0;        private DecimalFormat showFloatFormat = new DecimalFormat("0.00");        private TextView tvspeed;        private Timer timer;        private Handler handler = new Handler();        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);            tvspeed = (TextView) findViewById(R.id.tvspeed);            timer = new Timer();            timer.scheduleAtFixedRate(new RefreshTask(), 0L, (long) 2000d);        }        public void updateViewData(Context context) {            long tempSum = TrafficStats.getTotalRxBytes()                    + TrafficStats.getTotalTxBytes();            long rxtxLast = tempSum - rxtxTotal;            double totalSpeed = rxtxLast * 1000 / 2000d;            rxtxTotal = tempSum;            tvspeed.setText(showSpeed(totalSpeed));        }        private String showSpeed(double speed) {            String speedString;            if (speed >= 1048576d) {                speedString = showFloatFormat.format(speed / 1048576d) + "MB/s";            } else {                speedString = showFloatFormat.format(speed / 1024d) + "KB/s";            }            return speedString;        }        class RefreshTask extends TimerTask {            @Override            public void run() {                // 当前有悬浮窗显示,则更新内存数据。                rxtxTotal = TrafficStats.getTotalRxBytes()                        + TrafficStats.getTotalTxBytes();                handler.post(new Runnable() {                    @Override                    public void run() {                        updateViewData(getApplicationContext());                    }                });            }        }    }

5.public static int getHeight(View view) //获得某组件的高度
{
int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
view.measure(w, h);
return view.getMeasuredHeight();
}

6.高版本兼容http client在buildgradle里加这一行即可
useLibrary ‘org.apache.http.legacy’

7.保存日志文件到本地
File f = new File(Environment.getExternalStorageDirectory(),”logcat.txt”);
try {
FileOutputStream out = new FileOutputStream(f,true);
out.write(response.toString().getBytes(“UTF-8”));
} catch (Exception e) {
e.printStackTrace();
}

8.开发应用的时候会有一些有可能会变得值,例如webservice地址 应用的一些ID等等,之前一直都是直接在应用中改代码,不是忘点这忘点那,于是想到了可以用Properties配置文件,
我把网址等变量配置的配置文件中,这样之后再改的话就直接改配置文件就行了,就不用改代码了
下面给大家说说Properties的用法
public static String getPropertiesURL(Context c, String s) {
String url = null;
Properties properties = new Properties();
try {
properties.load(c.getAssets().open(“property.properties”));
url = properties.getProperty(s);
} catch (Exception e) {
e.printStackTrace();
}
return url;
}

这里我把配置文件放在了assets文件夹下 大家也可以放在raw下 还可以跟我们Java文件同级这样我们就可以读到Properties值了
配置文件的写法很简单:
例如 url=www.baidu.com就行注释用#号

9.String字符串在指定位置插入字符
StringBuilder sb = new StringBuilder (spuuid);
sb.insert(26, “den”);
sb.insert(18, “gar”);
sb.insert(9, “4k”);
return sb.toString();

10.每次启动新的Activity的时候,输入法总是弹出来,太烦人了。
主要原因就是页面上方有个EditTexit,每次都自动获取焦点。
注意要求是:每次启动新的Activity的时候,EditTexit不要获取到焦点或者获取到焦点也不让输入法弹出来,并不是阻止输入法使用。只要第一次启动的时候别弹出来就行了,如果主动点到EditTexit的时候输入法还是得正常的弹出来的
解决:
在OnCreate方法里面加下面这句代码
// 隐藏软键盘
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
11.EventBus事件用法
1.注册订阅者
EventBus.getDefault().register(this);
2.在发送事件的类里写自定义的内部类,做消息的封装操作
public class MessageEvent {
public final String percent; //自定义的消息字段
public final String length; //自定义的消息字段

        public MessageEvent(String percent,String length) {              this.percent = percent;              this.length = length;          }      }3.在发送事件的类里将封装了消息的对象发送出去    EventBus.getDefault().post(new MessageEvent((int)percent+"",(int)length+""));4.事件的接收:共4种方式,分别为:MainThread 主线程,BackgroundThread 后台线程,Async 后台线程,PostThread 发送线程(默认)       常用的用于更新UI的方式为:     public void onEventMainThread(MessageEvent event){        seekBar.setVisibility(View.VISIBLE);        seekBar.setMax(Integer.parseInt(event.length));        seekBar.setProgress(Integer.parseInt(event.percent));    }5.  添加processor按照Markus Junginger的说法(EventBus创作者),在3.0中,如果你想进一步提升你的app的性能,你需要添加:provided 'de.greenrobot:eventbus-annotation-processor:3.0.0-beta1'其在编译的时候为注册类构建了一个索引,而不是在运行时,这样的结果是其让EventBus 3.0的性能提升了一倍,相比2.4来说,其会是它的3到6倍。  

12.

<meta-data android:value="GOOGLEPLAY_V165" android:name="HAIWAN_CHANNEL"/>/**  * 获取Manifest里面配置的渠道版本  * <p>2014-11-14</p>  * @return  * @author RANDY.ZHANG  */  public String getHaiwanVersion() {      String channel = "";      try {          channel = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA).metaData.getString("HAIWAN_CHANNEL");      } catch (NameNotFoundException e) {          e.printStackTrace();      }      return channel;  }其他节点://在Activity应用<meta-data>元素。  ActivityInfo info = this.getPackageManager()          .getActivityInfo(getComponentName(),PackageManager.GET_META_DATA);  info.metaData.getString("meta_name");  //在application应用<meta-data>元素。  ApplicationInfo appInfo = this.getPackageManager()          .getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA);  appInfo.metaData.getString("meta_name");  //在service应用<meta-data>元素。  ComponentName cn = new ComponentName(this, MetaDataService.class);  ServiceInfo info = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);  info.metaData.getString("meta_name");  //在receiver应用<meta-data>元素。  ComponentName cn = new ComponentName(context, MetaDataReceiver.class);  ActivityInfo info = context.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);  info.metaData.getString("meta_name");

13.Activity全屏
getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
14.方法超限65536问题,解决方法
http://blog.csdn.net/t12x3456/article/details/40837287

0 0
原创粉丝点击