安卓开发示例代码总结(持续更新中...)
生活随笔
收集整理的這篇文章主要介紹了
安卓开发示例代码总结(持续更新中...)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
平時寫安卓代碼時,用到某個功能,其實也比較簡單,但經常疏忽忘記怎樣寫,所以將開發過程中經常用到的安卓示例代碼總結到下面,用的時候直接Copy過去就行。這樣開發時,做個優先的搬運工。
/*** 子線程更新UI示例*/ public void onClick(View v) {new Thread(new Runnable() {public void run() {// a potentially time consuming taskfinal Bitmap bitmap =processBitMap("image.png");imageView.post(new Runnable() {public void run() {imageView.setImageBitmap(bitmap);}});}}).start(); }/*** 弱引用使用*/ public void setData(String key, Object object) { WeakReference value =new WeakReference<>(object); dataList.put(key, value); }
/*** 泛型示例*/ double a = FanTest1.MaxNum(2.3, 3.5, 5.4);public static <T extends Comparable<T>> T MaxNum(T x, T y, T z){T max = x;if(y.compareTo(max)>0){max = y;}return max;}
/*** Thread帶Looper示例*/ class LooperThread extends Thread {public Handler mHandler; public void run() {Looper.prepare();mHandler = new Handler(Looper.myLooper()) {public void handleMessage(Message msg) {// process incoming messages here}}; Looper.loop();}}
/*** 注解示例,編譯檢查用*/ public void setAlpha(@IntRange(from=0,to=255) int alpha) { ... } public void setAlpha(@FloatRange(from=0.0, to=1.0) float alpha) {...} @RequiresPermission(allOf = {Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.ACCESS_MEDIA_LOCATION}) public static final void copyImageFile(String dest, String source) {//... }
/*** 升序 */ Collections.sort(list, new Comparator<CommoditysBean>() {@Overridepublic int compare(CommoditysBean o1, CommoditysBean o2) {return MyUtils.strToInt(o1.getDifferenceQty()) - MyUtils.strToInt(o2.getDifferenceQty());}});
/*** 打開一個網頁*/ private void clickMall() { String requestURL = "http://applink.dossav.com/shop"; Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(requestURL)); startActivity(intent); }
/*** 判斷時間差*/ public static Date g_LastDate = new Date(System.currentTimeMillis()); long diff = new Date(System.currentTimeMillis()).getTime()- g_LastDate.getTime(); if (diff < 5000) { //5秒return; }
/*** Hadler發送消息示例*/ private static final int MSG_SHOW_NETWORKNOTACCESS = 200; private Handler mHandler = new Handler(Looper.getMainLooper()){@Overridepublic void handleMessage(Message msg){switch (msg.what){case MSG_SHOW_NETWORKACCESS://do somethingbreak; }}}; mHandler.sendEmptyMessage(MSG_SHOW_NETWORKACCESS);
/*** UI線程執行*/ runOnUiThread(new Runnable() {@Overridepublic void run() { //刷新UI}});
/*** 創建子線程*/ new Thread() {public void run() {System.out.println("Thread is running.");}}.start();
/*** 判斷WIFI連接的是2.4G還是5G WIFI*/ WifiManager wifiManager = (WifiManager)getApplicationContext(). getSystemService(Context.WIFI_SERVICE);//注意用getApplicationContext可以避免android內存泄漏. WifiInfo wifiInfo= wifiManager.getConnectionInfo(); int frequency = wifiInfo.getFrequency();//以5開關是5G WIFI, 2開頭是2.4G Log.i(TAG, "frequency = " + frequency );//備注:AndroidManifest.xml權限要加 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
/*** ExecutorService 示例* ExecutorService原代碼是個接口類*/ private ExecutorService mExecutor; mExecutor = Executors.newCachedThreadPool(); Runnable runnable = new Runnable() {@Overridepublic void run() {String threadName = Thread.currentThread().getName();Log.i("TAG", "runnable is running");Log.i("TAG", "threadName = " + threadName);}}; mExecutor.execute(runnable);//onDestroy里加下面代碼 if (null != mExecutor) {mExecutor.shutdown(); }
參考示例:
Android開發人員不得不收集的代碼(持續更新中)
https://blog.csdn.net/feelinghappy/article/details/105575062
作者簡介:https://shimo.im/docs/rp3OVwxle2fJn7Am/
上海徐匯
2022年4月3日
總結
以上是生活随笔為你收集整理的安卓开发示例代码总结(持续更新中...)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: poj 3748 位操作
- 下一篇: USACO2.11 The Castle