Android自定义透明背景的Dialog
Java注解
注解,是描述Java代码的代码,它能够被编译器解析,向编译器、虚拟机说明一些事情,就像java中给程序员看的注释一样。
Android应用开发这方面比较火的是Butter Knife ,本文讲述如何自定义注解替换findViewById()。
实现注解(annotation)的思路:通过反射获取到类中使用注解的变量,方法,再调用不同的方法对这些变量,方法进行处理以达到目的。
主要涉及三方面:
- 定义一个注解类
- 定义一个注解帮助类
- 使用注解
Java反射简单应用
简介
反射,用来在运行时获取给定类的构造函数,变量,方法,并对其作以修改,而不必在编译时获取该类。
Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use of reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions.
–https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/package-summary.html
简单使用
定义一个待反射的类ATestClass.java
Android通过Hook启动未注册Activity
简介
hook是钩子的意思,hook的过程是通过反射、代理等改变系统原有的行为以达到自己的目的。
本文主要是通过hook android 中的ActivityManagerService和Handler.CallBack,欺骗系统调起activity的过程,在调用startActivity时将targetIntent通过proxy伪装为proxyIntent,等到通过系统验证,正式启动activity时,再讲proxyIntent恢复为targetIntent,从而实现调用未在AndroidManifest.xml中注册的activity。
需要注意,本方法只在Api<26下有效。具体原因见后面。
具体实现
AndroidService相关知识
启动一个Service
MyServices.java
必须继承自Service,或者如IntentService本身就是等其子类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class MyServices extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d("TAG","onBind");
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("TAG","onCreate");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("TAG", "onDestroy: ");
}
}AndroidManifest.xml
注册MyServices
1
2
3
4
5
6
7
8<application>
<service android:name=".MyServices"
android:exported="true">
<intent-filter>
<action android:name="cf.android666.myservices" />
</intent-filter>
</service>
</application>MainActivity.java
在java中调用Service,需要
ServiceConnection
类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected: 服务绑定");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected: 服务解绑");
}
};
Intent intent = new Intent(context, MyServices.class);
bindService(intent, mConnection, Service.BIND_AUTO_CREATE);//绑定Service
//startService(intent); 启动service
unbindService(mConnection);//解绑ServicebindService()
和startService()
的区别在于:**
bindService()
将service和当前的activity绑定在一起,activity销毁时,service也会被销毁;**
startService()
则只是“启动”service,在此后service的活动和activity无关,并一直存活。
Service具体分析
Service在AndroidManifest.xml中的属性:
1 | android:name=".MyService"//必须被指定 |
Android控件组
这几天的工作中用到了控件组来实现复杂布局,效果不错,记录下来备用。
1. 定义控件组布局xxx_layout.xml
在这里定义要使用的控件组布局,这里的布局决定了布局显示的样子。
1 | <?xml version="1.0" encoding="utf-8"?> |