Skip to content

zhl3391/AptIntent

Repository files navigation

AptIntent

Build Status

AptIntent是一个Android平台基于annotation的库,其目的是简化创建带数据的Intent和获取Intent内的数据。

使用方法

1.添加依赖

Gradle

在你的工程目录下的build.gradle添加android-apt插件,添加jitpackmaven库:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
    	...
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

allprojects {
	repositories {
		...
		maven { url "https://www.jitpack.io" }
	}
}

然后在你的模块下的build.gradle应用android-apt插件,添加AptIntent依赖:

apply plugin: 'android-apt'

android {
  ...
}

dependencies {
	compile 'com.github.zhl3391.AptIntent:aptintent:1.0.1'
	apt 'com.github.zhl3391.AptIntent:aptintent-compiler:1.0.1'
}

2.定义接口

public interface IntentCreator {

    String KEY_STRING       = "string";
    String KEY_INT          = "int";
    String KEY_BOOLEAN      = "boolean";
    String KEY_OBJECT       = "object";
    String KEY_ARRAY_LIST   = "array_list";
    String KEY_BUNDLE       = "bundle";

    @CreateIntent(MainActivity.class)
    Intent mainActivityIntent(Context context,
                              Bundle bundle,
                              @Extra(KEY_STRING)String stringTest,
                              @Extra(KEY_INT) int intTest,
                              @Extra(KEY_BOOLEAN) boolean booleanTest,
                              @Extra(KEY_OBJECT) ObjectTest objectTest,
                              @Extra(KEY_ARRAY_LIST) ArrayList<String> arrayListTest);
}

定义一个IntentCreator接口,定义一个mainActivityIntent的方法,通过@CreateIntent注解声明, 通过@Extra注解声明参数。
注:必须传入Context

3.获取参数和跳转

public class MainActivity extends AppCompatActivity {

    @ExtraField(KEY_STRING)
    String mStringTest;
    @ExtraField(KEY_INT)
    int mIntTest;
    @ExtraField(KEY_BOOLEAN)
    boolean mBooleanTest;
    @ExtraField(KEY_OBJECT)
    ObjectTest mObjectTest;
    @ExtraField(KEY_ARRAY_LIST)
    ArrayList<String> mArrayListTest;
    @ExtraField(KEY_BUNDLE)
    String mBundleTest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AptIntent.bind(this);
    }

    public void jump(View view) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("arrayList");
        Bundle bundle = new Bundle();
        bundle.putString(KEY_BUNDLE, "bundle");
        Intent intent = AptIntent.create(IntentCreator.class).mainActivityIntent(this,
                bundle, "haha", 888, true, new ObjectTest(), arrayList);
        startActivity(intent);
    }
}

通过@ExtraField注解声明成员变量,调用AptIntent.bind(this)获取intent内数据。
调用AptIntent.create(IntentCreator.class)创建IntentCreator接口的实现,再调用mianActivityIntent创建intent
注:@ExtraField注解的成员变量不能用privatestatic修饰。

License

Copyright 2016 zhl3391

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

an annotation lib to create intent with extras and get extras

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages