1、修改window的亮度
Window window = this.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL; //将亮度调到最大
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE; //将亮度恢复到默认设置
window.setAttributes(lp);
2、获取系统时间信息
SystemClock.uptimeMillis() //自开机后,经过的时间,不包括深度睡眠的时间
SystemClock.elapsedRealtime() //自开机后,经过的时间,包括深度睡眠的时间
Thread.sleep(millis),Object.wait(millis),SystemClock.sleep(millis) 和Handler的postDelay()都是使用uptimeMills,在系统休眠之后它们的回调也会延期,直到系统被唤醒才继续计时
3、重启、关机
//重启
PowerManager.reboot("reboot_reason"),使用此方法重启机器时,其他程序无法接收到关机广播
//重启
Intent i = new Intent(Intent.ACTION_REBOOT); //这个方法重启可以接收到关机广播
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
context.sendBroadcast(i);
//关机
String action;
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1){
action = "android.intent.action.ACTION_REQUEST_SHUTDOWN";
}else{
//8.0系统
action = "com.android.internal.intent.action.REQUEST_SHUTDOWN";
}
Intent shutdown = new Intent(action);
shutdown.putExtra("android.intent.extra.KEY_CONFIRM", false);
shutdown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(shutdown);
4、ScrollView滑动速度的改变: 继承ScrollView,复写fling方法
/**
* 滑动事件
*/
@Override
public void fling(int velocityY) {
super.fling(velocityY / 4); //重点在"velocityY / 4",这里意思是滑动速度减慢到原来四分之一的速度
}
scrollView.fullScroll(ScrollView.FOCUS_DOWN);滚动到底部 // 过handler在新线程中更新
scrollView.fullScroll(ScrollView.FOCUS_UP);滚动到顶部
5、TextView实现长按复制文本功能
android:textIsSelectable = "true"
6、Android中监听“"电源键 ”、"长按 电源键"、"Home键"、"Home键长按"
这几个“按钮”的触发,都会产生一个Action ==Intent.ACTION_CLOSE_SYSTEM_DIALOGS的通知
区分他们三个的不同事件的参数,就是随着Intent带过来的reason字符串
按电源键:lock 注:如果在锁屏界面按电源键,则不会受到此广播
长按 电源键:(globalscreen)
Home键:(homekey)
Home键长按:(recentapps)
7、程序退出后重新启动,进程实际没有结束,Application的实例可能还是以前的。 待验证。。。
8、android的native crash : 闪退现象, 没有弹窗提示, 如何检测此crash
Build fingerprint: 'XXXXXXXXX'
pid: 1658, tid: 13086 >>> system_server <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 64696f7e
r0 00000000 r1 00000001 r2 ad12d1e8 r3 7373654d
r4 64696f72 r5 00000406 r6 00974130 r7 40d14008
r8 4b857b88 r9 4685adb4 10 00974130 fp 4b857ed8
........
9、查看函数调用关系
系统函数被无故调用时也可用此方法调查
使用android.util.Log类中的方法getStackTraceString
Log.d(TAG, Log.getStackTraceString(new Throwable()));
输出日志:
02-22 19:24:58.650 5306-5306/com.zsg.test D/MainActivity: java.lang.Throwable
at com.zsg.test.MainActivity.onCreate(MainActivity.java:88)
at android.app.Activity.performCreate(Activity.java:6849)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6120)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
10、写文件并且同步到闪存
fos = new FileOutputStream("/sdcard/test.txt");
fos.write(buf);
fos.flush(); //将数据保存到内核缓冲区,不能确保数据写到存储设备中,如断电时
FileDescriptor fd = fos.getFD();
fd.sync(); //将数据同步到达物理存储设备
11、本地socket
LocalServerSocket localServerSocket = new LocalServerSocket("zsg");
LocalSocket localAcceptSocket = localServerSocket.accept();
localAcceptSocket.setReceiveBufferSize(1024*10);
localAcceptSocket.setSendBufferSize(1024*10);
LocalSocket localSocket = new LocalSocket(LocalSocket.SOCKET_STREAM);
localSocket.connect(new LocalSocketAddress("zsg"));
12、文件锁FileLock
FileLock 用于进程间并发,控制不同程序对同一文件的读、写访问
FileInputStream fileInputStream = null;
FileChannel fileChannel = fileInputStream.getChannel(); //支持获取通道的类:RandomAccessFile、FileOutputStream、FileInputStream
FileLock fileLock = fileChannel.lock(); //获取独占锁
//FileLock fileTryLock = fileChannel.tryLock();
//do something...
fileLock.release();
13、匿名共享内存(Ashmem)
MemoryFile file = new MemoryFile("test", 1024*5);
网友评论