2016年11月14日星期一

关于 service的理解

  网上有很多大神对service已经有了 详细的介绍. 在这里 我只是记录下 我关于service的调试跟理解.


1. service 分为两种开启方法 startService与 bindService.
 
>startSetvice开启的话 即使应用关闭 它也会正常使用.
  并且退出应用->重进应用再开启startService时 如果已经存在该服务, 是不会重启服务的.
  最开始开启服务时的顺序是 onCreate > onStartCommand 
  如果服务已经开启再开启服务时 onStartCommand

2.bindService开启的话 随着应用的关闭而会被关闭. 即使你没有使用 unbinderService.
 
>所以使用contentService来链接时候只能是单向链接.
>要注意的是 aidlService的 通信见链接Service也是 单次链接.因为他是通过Ibinder链接的(bindService)

3.IntentService是 异步请求.他的服务器启动并不在 main thread 进行.并且他会自动 stopSelf 来停止服务. 所以他也是单次调用. 因为你不能长期挂起.


 

2016年11月3日星期四

注解的使用(Annotation)

  此帖用来写 本人对注解使用的理解. 并且考虑了下 什么状态下能使用注解.

考虑场景 1: 在你使用注解的地方. 程序调用此地方的方法(或者接口)时 让系统自动调用此方法.
          比如:

ClassA里调用了 注解Anotation时
public class ClassA {
@Anotation(value="kirsong")
public void showA(String name){
System.out.println(name);
}
}

当调用类ClassA的时候 根据方法映射调用 showA方法.

Method methodAnotation=ClassA.class.getDeclaredMethod("showA", new Class[]{String.class});
Anotation an=null;
if(methodAnotation.isAnnotationPrese
//判断该方法是否存在注解
if((an=methodAnotation.getAnnotation(Anotation.class))!=null){
try {
methodAnotation.invoke(mj, an.value());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}


考虑场景2: 根据传送的数据, 分析数据中的注解类并调用他

首先声明注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Anotation {
String value() default "val1";
}

创建使用到注解的类
public class ClassA {
@Anotation(value="kirsong")
public void showA(String name){
System.out.println(name);
}
@Anotation
public void showB(String name){
System.out.println(name);
}
public void showC(String name){
System.out.println(name);
}
}

创建根据注解分析器,并调用
public class TargetAnnotation {

public void setPust(Object cls){
try {
Method[] md=cls.getClass().getDeclaredMethods();
Anotation an=null;
for(Method method:md){
if((an=method.getAnnotation(Anotation.class))!=null){
method.invoke(cls, an.value());
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

使用方法
System.out.print("场景3\n");
ClassA ca=new ClassA();
TargetAnnotation target=new TargetAnnotation();
target.setPust(ca);


使用场景3. 使用注解调用接口

定义接口

public interface InterFaceA {
@Anotation(value="intera")
void interA(String value);
}

调用接口中的注解方法

public void setInterfacePust(Object cls){
try {

Class<?>[] interfaces = cls.getClass().getInterfaces(); 
for(Class<?> interClas:interfaces){
Method[] md=interClas.getDeclaredMethods();
Anotation an=null;
cls.getClass().getInterfaces();
for(Method method:md){
if((an=method.getAnnotation(Anotation.class))!=null){
method.invoke(cls, an.value());
}
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}

调用
InterFaceA intera=new InterFaceA(){

@Override
public void interA(String value) {
System.out.print(value);
}
};
target.setInterfacePust(intera);