Android的各种零碎知识点(持续补充)

Posted 南飞的孤雁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android的各种零碎知识点(持续补充)相关的知识,希望对你有一定的参考价值。

1,将EditText的光标放在尾部:

editText.setSelection(editText.getText().length());

2,使用SparseArray<E>替换HashMap<Integer,E>可以提升查找效率。

3,通过logcat日志可以查看Activity的display time,日志样式如下:

I/ActivityManager: Displayed com.huawei.android.ui.PhoneFinderActivity: +2s152ms (total +22s66ms)

日志打印位置在com.android.server.am.ActivityRecord.reportLaunchTimeLocked(),具体代码如下:

private void reportLaunchTimeLocked(final long curTime) 
    final ActivityStack stack = task.stack;
    final long thisTime = curTime - displayStartTime;
    final long totalTime = stack.mLaunchStartTime != 0
            ? (curTime - stack.mLaunchStartTime) : thisTime;
    if (ActivityManagerService.SHOW_ACTIVITY_START_TIME) 
        Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
        EventLog.writeEvent(EventLogTags.AM_ACTIVITY_LAUNCH_TIME,
                userId, System.identityHashCode(this), shortComponentName,
                thisTime, totalTime);
        StringBuilder sb = service.mStringBuilder;
        sb.setLength(0);
        sb.append("Displayed ");
        sb.append(shortComponentName);
        sb.append(": ");
        TimeUtils.formatDuration(thisTime, sb);
        if (thisTime != totalTime) 
            sb.append(" (total ");
            TimeUtils.formatDuration(totalTime, sb);
            sb.append(")");
        
        Log.i(ActivityManagerService.TAG, sb.toString());
    
    mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
    if (totalTime > 0) 
        //service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
    
    displayStartTime = 0;
    stack.mLaunchStartTime = 0;

“this time”包括actiivty启动时间和activity内容绘制时间;
“total time”包括“this time”和activity stack建立时间;
通常,“this time”和“total time”是相同的,只在两者不同时才会打印“total time”。

4,ipv4的正则表达式:

^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.)3(25[0-5]|2[0-4][0-9]|1[09][0-9]|[1-9][0-9]|[0-9])$

5,在非Activity中调用startActivtiy()时,需要给Intent添加flag:

Intent.FLAG_ACTIVITY_NEW_TASK

6,在Activity中设置手机屏幕保持常亮状态:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

7,WebView加载本地文件:

webView.loadUrl("file:///android_asset/test.html");

8,TextView实现中文加粗效果:

textView.getPaint().setFakeBoldText(true);

9,TextView使用自定义字体:

Typeface tf=Typeface.createFromAsset(getAssets(),"fonts/***.TTF");
textView.setTypeface(tf);

10,EditText默认弹出数字键盘,但是允许输入字母等特定字符:

<EditText
    android:digits="0123456789abcdefghijklmnopqrstuvwxyz./?<>"
    android:inputType="number"/>

11,android app的包名应遵循“DNS反转”约定,以保证包名唯一性。

12,Activity全屏设置:

12.1 AndroidManifest.xml中为Activity添加如下属性:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

12.2 Activity代码中设置:

requestWindowFeature(Window.FEATURE_NO_TITLE);
int flags = WindowManager.LayoutParams.FLAG_FULLSCREEN;
int mask = WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setFlags(flags, mask);
// 注意:以上代码要写在setContentView()之前。

13,TextView增加删除线:

textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

14,TextView设置ColorSelector:

Context ctx = textView.getContext();
ColorStateList colorStateList = ctx.getResources().getColorStateList(R.color.***_selector);
textView.setTextColor(colorStateList);

15,在代码中设置View的background属性之后padding属性会失效,需要重新设置

16,缺少*.so文件,会导致调用错误java.lang.ExceptionInInitializerError

17,Eclipse中出现“ADB server didn’t ACK”错误解决方案:

1)关闭Eclipse
2)通过命令行进入 Android SDK tools目录
3)执行adb kill-server命令
4)执行adb start-server
5)如果启动ADB server时没有出错信息,则adb启动成功
6)重启Eclipse

18,将jar包转成dex格式:

dx --dex --output=out_name.jar origin.jar

19,查看app内存使用情况:

adb shell dumpsys meminfo -a [package_name]

20,android app数据备份与恢复:

adb backup -f [backup_file_name] -noapk [package_name]
adb restore [backup_file_name]

21,设置音量:

public void setVolume(float scale) 
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.setMode(AudioManager.STREAM_MUSIC);
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    audioManager
            .setStreamVolume(AudioManager.STREAM_MUSIC, (int) (maxVolume * scale), AudioManager.FLAG_PLAY_SOUND);

22,获取Android app流量消耗数据:

1)进入手机根目录:adb shell
2)获取app_id:cat data/system/packages.list | grep “pkg_name”(pkg_name为app包名)
3)获取上行流量:cat proc/uid_stat/app_id/tcp_snd(app_id通过第2步获取,流量单位是byte)
4)获取下行流量:cat proc/uid_stat/app_id/tcp_rcv(app_id通过第2步获取,流量单位是byte)

23,动态改变dialog尺寸:

//在dialog.show()之后调用
public static void setDialogWindowAttr(Dialog dlg)
        WindowManager.LayoutParams lp = dlg.getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = LayoutParams.MATH_PARENT;//宽高可设置具体大小
        lp.height = LayoutParams.MATH_PARENT;
        dlg.getWindow().setAttributes(lp);
    

24,Eclipse设置单行代码宽度:

Preferences—Java—Code Style—Formatter—Edit…—Line Wrapping—General Settings—Maximum line width

25,查看keystore文件:

keytool -list -v -keystore <keystore_file>

26,查看已安装apk包名:

adb shell pm list package

27,gradle配置中denpencies的类库的存放目录:

.gradle/caches/modules-2/files-2.1

28,查看gradle最新版本:

https://gradle.org/releases
http://services.gradle.org/distributions/

29,查看as的gradle插件最新版本:

https://jcenter.bintray.com/com/android/tools/build/gradle/
ps:gradle插件存储目录:/.gradle/caches/modules-2/files-2.1/com.android.tools.build/gradle/

30,adb查看当前运行的activity:

adb shell dumpsys activity activities | grep "Run"



(未完待续)

以上是关于Android的各种零碎知识点(持续补充)的主要内容,如果未能解决你的问题,请参考以下文章

Java基础零碎知识点总结(持续补充)

Python的零碎的补充知识

Android零碎知识之Style and Theme

零碎的知识(持续更新)

计算机相关零碎知识(持续更新)

Android 零碎知识点---2