Android 常用

出现空指针问题
clear rebuild即可

Git terminal

Android studio terminal 设置中文
git config --global i18n.commitencoding utf-8
git config --global i18n.logoutputencoding utf-8
set LESSCHARSET=utf-8

git reset -- hard更新全部代码到最新
git checkout -- ./**单独更新某一个目录

还原当前分支代码
git checkout -f && git clean -df

git reset --hard && git clean -df

打包

  • 打包资源文件,通过AAPT(Android Asset Packaging Tool)打包成R.java类(资源索引表)以及.arsc资源文件。
  • 处理AIDL文件,检查app中是否有aidl文件,如果有会通过aidl工具(源码位于system/tools/aidl)打包成java接口类
  • 编译R.java源码部分以及aidl.java通过javac生成对应的.class文件。
  • 将上面生成的.class文件和第三方jar或者library通过dx工具打包生成dex文件。
  • 生成未签名的apk,包括apkbuilder工具将所有没有编译的资源、.arsc资源、.dex文件打包到一个完成apk文件中
  • 生成签名的apk,包括jarsigner工具对未签名的apk验证签名。得到一个签名后的apk(signed.apk)
  • zipAlign工具对齐上面签名的apk文件。

MVP .link

MVP :
优点:分离M层和V层的代码,P层建立关联,实现M层和V层的解耦。
缺点:每增加一个功能,需要增加相应的接口回调。
核心:接口实现隔离,业务层交给P层。

p层的逻辑处理单一的功能,不要融合一个模块下的增删改查的整个功能。
由于p层持有了v层的引用,通常在p层使用弱引用来持有view层实例,在p层销毁的时候需要将v层的引用销毁掉。
契合类指的p层和v层的接口类放在一个contract接口类中,契合类方便管理业务层的功能,将单个功能放到一个contract契合类中。

public interface AddBookShelfContract {
    interface View extends BaseContract.BaseView {
        void addBookShelfSuccess(BookShelfItem... bookShelfItem);
        void addBookShelfFail();
 }

    interface Presenter extends BaseContract.BasePresenter<View> {
        void addBookShelf(String tokenId, BookShelfItem... bookShelfItem);
    }
}

MVVM

MVVM:观察者模式,通过数据的改变来通知相应的View改变的过程。M层和上面的MVP中的M层是一样的,都是网络请求+数据缓存来实现该层的,里面的双V,一个指的ViewModel实现的,另外一个AndroidDataBinding实现V层,ViewModel层获取到M层的数据后,通过观察者模式通知AndroidDataBinding在UI上的改变。

adb连接

打开cmd
adb tcpip 8888 重置端口
adb devices 查看设备
adb connect xxx:8888   连接
adb disconnect xxx:8888  断开
出现error: device not found
adb nodaemon server  检查5037端口
cannot bind 'tcp:5037'  5037端口被占用
netstat -ano | findstr "5037"   查看是什么进程占用5037端口
tasklist | findstr "18728"   查看这个进程是由哪个程序创建
taskkill /f /pid 18728  将adb进程结束掉

pm path com.wologic.wms
package:/data/app/com.wologic.wms-pHfDy8o7QTt7FXdBu3HK0g==/base.apk

adb shell am broadcast -a com.android.test --es test_string "this is test string" --ei test_int 100 --ez test_boolean true
String类型,int类型,boolean类型

应用安装失败,使用install安装,查看报错
adb install Android_65632.apk

adb shell am start -S -W com.wologic.wms/.activity.LoginActivity

分隔符

String temp = etBoxCode.getText().toString();
String[] array = temp.split("[;]");
BoxCode = array[0];

数组参数上传

String[] s1 = new String[]{"qqqq","qqqqaa","ddddd"};
Log.e(TAG, "doUpdate: " + new Gson().toJson(s1));
["qqqq","qqqqaa","ddddd"]

//第二种方式
List<String> list = new ArrayList<>();
for (int i = 0; i < list2.size(); i++) {
   list.add(list2.get(i).getBoxCode());
}
Log.e(TAG, "doUpdate: " + new Gson().toJson(list));
["qqqq","qqqqaa","ddddd"]

//第三种方式
List<ClassBean> list = new ArrayList<>();
ClassBean bean = new ClassBean();
bean.setName("xiaoming");
list.add(bean);
Log.e(TAG, "doUpdate: " + new Gson().toJson(list));
[{"name":"xiaoming"}]

权限

String[] perms = {Manifest.permission.CAMERA};

    private void verifyPermission() {

        int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);

        if (permission != PermissionChecker.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, perms, 0X01);
        }
    }

& ^


& | 左边无论真假,右边都进行计算
&& || 左边为假(),右边不进行计算

&
只有1&11,其他得0,同理可得6&5算出二进制得100
1001 & 110 = 0000
1001 & 111 = 0001
1111 & 110 = 0110
1111 & 111 = 0111

10 -1 = 9 即是 1001
00不能起作用更容易碰撞。
16 -1 = 15 即是 1111
11,能起作用更不容易碰撞。
hashmap,空间换时间。0.75, 2倍。

|
只有0|0时候为0 ,其他为1
6|5等于7

^
任何相同二进制异或运算都得0
1^1=0,0^0=0
不相同的异或运算都得1
1^0=1,0^1=1
6的二进制为1105的二进制为101
同理6^5011

~
1变成00变成1

<<
空缺补0,被移除的高位丢弃
3<<2则是3*2*2也就是3乘以22次幂

>>
最高位是什么就补什么
3>>1则是3/2也就是3除以21次幂

无符号右移
无论高位是0还是1都补0

SparseArray

sparsearray,内存节约,key只能是int
get()二分查找,速度不会慢value,
delete()设置delete标志位,使用时赋值,不会真正删除,复用
arraycope,向后推移
int[5] mKey
object[5] mValue 

arraymap,simplearraymap,== hashmap + sparsearray
追加:+1

生成随机数

new Random().nextInt(5)

本地json读取

Gson gson = new Gson();
Type type = new TypeToken<TestAlbum>() {
}.getType();

TestAlbum testAlbum = gson.fromJson(Utils.getApp().getString(R.string.free_music_json), type);

    <string name="free_music_json">
        "{
	\"data\": [{
		\"id\": 6,
		\"link\": \"\",
		\"name\": \"面试\",
		\"order\": 1,
		\"visible\": 1
	}, {
		\"id\": 9,
		\"link\": \"\",
		\"name\": \"Studio3\",
		\"order\": 1,
		\"visible\": 1
	}, {
		\"id\": 5,
		\"link\": \"\",
		\"name\": \"动画\",
		\"order\": 2,
		\"visible\": 1
	}, {
		\"id\": 1,
		\"link\": \"\",
		\"name\": \"自定义View\",
		\"order\": 3,
		\"visible\": 1
	}, {
		\"id\": 2,
		\"link\": \"\",
		\"name\": \"性能优化 速度\",
		\"order\": 4,
		\"visible\": 1
	}, {
		\"id\": 3,
		\"link\": \"\",
		\"name\": \"gradle\",
		\"order\": 5,
		\"visible\": 1
	}, {
		\"id\": 4,
		\"link\": \"\",
		\"name\": \"Camera 相机\",
		\"order\": 6,
		\"visible\": 1
	}, {
		\"id\": 7,
		\"link\": \"\",
		\"name\": \"代码混淆 安全\",
		\"order\": 7,
		\"visible\": 1
	}, {
		\"id\": 8,
		\"link\": \"\",
		\"name\": \"逆向 加固\",
		\"order\": 8,
		\"visible\": 1
	}],
	\"errorCode\": 0,
	\"errorMsg\": \"\"
}"
    </string>

String JsonData = util.getJson(this, "test");//获取assets目录下的json文件数据
Gson gson = new Gson();
testBean testBean = gson.fromJson(JsonData, testBean.class);
for (com.example.test_master.testBean.DataDTO datum : testBean.getData()) {
    Log.e(TAG, "initView: " + datum.getName());
}

    public static String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

asset.test.txt
{"data":[{"id":6,"link":"","name":"面试","order":1,"visible":1},{"id":9,"link":"","name":"Studio3",
"order":1,"visible":1},{"id":5,"link":"","name":"动画","order":2,"visible":1},{"id":1,"link":"",
"name":"自定义View","order":3,"visible":1},{"id":2,"link":"","name":"性能优化 速度","order":4,"visible":1},
{"id":3,"link":"","name":"gradle","order":5,"visible":1},{"id":4,"link":"","name":"Camera 相机",
"order":6,"visible":1},{"id":7,"link":"","name":"代码混淆 安全","order":7,"visible":1},{"id":8,
"link":"","name":"逆向 加固","order":8,"visible":1}],"errorCode":0,"errorMsg":""}

广播通信

接受方
AndroidManifast

<uses-permission android:name="com.wologic.wms.broadcast.QRBroadcastReceiver.ACTION" />

<permission android:name="com.wologic.wms.broadcast.QRBroadcastReceiver.ACTION" />

<receiver
    android:name=".broadcast.QRBroadcastReceiver">
    <intent-filter>
        <action android:name="com.wologic.wms.broadcast.QRBroadcastReceiver.ACTION" />
    </intent-filter>
</receiver>

public static final String BROADCAST_ACTION = "com.wologic.wms.broadcast.QRBroadcastReceiver.ACTION";

if (intent.getAction().equals(BROADCAST_ACTION)) {
   String qrData = intent.getStringExtra("QR_DATA");
    Log.e("测试接收到广播:", qrData);
}

发送方

Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION);
intent.putExtra("QR_DATA", result.getText());
intent.setComponent(new ComponentName("com.wologic.wms","com.wologic.wms.broadcast.QRBroadcastReceiver"));
sendBroadcast(intent);

//KeyEvent.KEYCODE_MENU
	//KeyEvent.KEYCODE_BACK
	 public static void sendKeyEvent(final int KeyCode) {
	       new Thread() {     //不可在主线程中调用
	            public void run() {
	                try {
	                    Instrumentation inst = new Instrumentation();
	                    inst.sendKeyDownUpSync(KeyCode);
	               } catch (Exception e) {
	                    e.printStackTrace();
	                }
	             }
	   
	        }.start();
	 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值