平时写安卓代码时,用到某个功能,其实也比较简单,但经常疏忽忘记怎样写,所以将开发过程中经常用到的安卓示例代码总结到下面,用的时候直接Copy过去就行。这样开发时,做个优先的搬运工。
/**
* 子线程更新UI示例
*/
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
// a potentially time consuming task
final 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>() {
@Override
public 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())
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case MSG_SHOW_NETWORKACCESS:
//do something
break;
}
}
};
mHandler.sendEmptyMessage(MSG_SHOW_NETWORKACCESS);
/**
* UI线程执行
*/
runOnUiThread(new Runnable() {
@Override
public 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() {
@Override
public 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日