Android专题安卓工具相关Android经验

微信 支付、分享 组件化

2019-06-24  本文已影响53人  程序员阿兵

微信分享,就需要有一个“${applicationId}.wxapi.WXEntryActivity”,这个操作在多包名或者是组件化的项目中很不方便,可以使用设置代理Alies或者使用注解生成器去实现解耦

介绍方法

下面介绍第一种方式:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface EntryGenerator {
    String packageName();
    Class<?> entryTemplate();
}

final class EntryVisitor extends SimpleAnnotationValueVisitor7<Void, Void> {

    private final Filer FILER;
    private String mPackageName = null;

    EntryVisitor(Filer FILER) {
        this.FILER = FILER;
    }

    @Override
    public Void visitString(String s, Void p) {
        mPackageName = s;
        return p;
    }

    @Override
    public Void visitType(TypeMirror t, Void p) {
        generateJavaCode(t);
        return p;
    }

    private void generateJavaCode(TypeMirror typeMirror) {
        final TypeSpec targetActivity =
                TypeSpec.classBuilder("WXEntryActivity")
                        .addModifiers(Modifier.PUBLIC)
                        .addModifiers(Modifier.FINAL)
                        .superclass(TypeName.get(typeMirror))
                        .build();

        final JavaFile javaFile = JavaFile.builder(mPackageName + ".wxapi", targetActivity)
                .addFileComment("微信入口文件")
                .build();
        try {
            javaFile.writeTo(FILER);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/**
 * Created by 桂雁彬 on 2019/6/22
 */

@SuppressWarnings("unused")
@AutoService(Processor.class)
public final class LatteProcessor extends AbstractProcessor {

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        final Set<String> types = new LinkedHashSet<>();
        final Set<Class<? extends Annotation>> supportAnnotations = getSupportedAnnotations();
        for (Class<? extends Annotation> annotation : supportAnnotations) {
            types.add(annotation.getCanonicalName());
        }
        return types;
    }

    private Set<Class<? extends Annotation>> getSupportedAnnotations() {
        final Set<Class<? extends Annotation>> annotations = new LinkedHashSet<>();
        annotations.add(EntryGenerator.class);
        return annotations;
    }

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment env) {
        generateEntryCode(env);
        return true;
    }

    private void scan(RoundEnvironment env,
                      Class<? extends Annotation> annotation,
                      AnnotationValueVisitor visitor) {

        for (Element typeElement : env.getElementsAnnotatedWith(annotation)) {
            final List<? extends AnnotationMirror> annotationMirrors =
                    typeElement.getAnnotationMirrors();

            for (AnnotationMirror annotationMirror : annotationMirrors) {
                final Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues
                        = annotationMirror.getElementValues();

                for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry
                        : elementValues.entrySet()) {
                    entry.getValue().accept(visitor, null);
                }
            }
        }
    }

    private void generateEntryCode(RoundEnvironment env) {
        final EntryVisitor entryVisitor =
                new EntryVisitor(processingEnv.getFiler());
        scan(env, EntryGenerator.class, entryVisitor);
    }
}
@SuppressWarnings("unused")
@EntryGenerator(
        packageName = "com.diabin.fastec.example",
        entryTemplate = WXEntryTemplate.class
)
public interface WeChatEntry {
}

上面指定了注解并且指定了 需要修改的包名和自己自定义的类,然后就可以在自定义的类下微信操作了

public class WXEntryTemplate extends BaseWXEntryActivity {

   @Override
   protected void onResume() {
       super.onResume();
       finish();
       overridePendingTransition(0, 0);
   }

   @Override
   protected void onSignInSuccess(String userInfo) {
       LatteWeChat.getInstance().getSignInCallback().onSignInSuccess(userInfo);
   }
}

下面介绍第二种方式:Activity-alias 了解一下

   <!--微信友盟分享-->
        <activity
            android:name="com.linewell.bigapp.component.accomponentshare.wxapi.WXEntryActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:theme="@style/AppTheme" />
        <activity
            android:name="com.linewell.bigapp.component.accomponentshare.wxapi.WXH5EntryActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:process=":webview"
            android:theme="@style/AppTheme" />
        <activity
            android:name="com.linewell.bigapp.component.accomponentshare.wxapi.WXSnapshotEntryActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:process=":snapshot"
            android:theme="@style/AppTheme" />
        <activity
            android:name="com.linewell.bigapp.component.accomponentshare.wxapi.WXPayEntryActivity"
            android:exported="true"
            android:launchMode="singleTop" />
        <activity-alias
            android:name="${applicationId}.wxapi.WXEntryActivity"
            android:exported="true"
            android:targetActivity="com.linewell.bigapp.component.accomponentshare.wxapi.WXEntryActivity" />
        <activity-alias
            android:name="${applicationId}.wxapi.WXPayEntryActivity"
            android:exported="true"
            android:targetActivity="com.linewell.bigapp.component.accomponentshare.wxapi.WXPayEntryActivity" />
        <!--QQ友盟分享-->

上面的代码是不是很简单无脑,只需要设置对应占位符包名下就行

上一篇下一篇

猜你喜欢

热点阅读