API License

BaseProject是一个简易的Android基础项目,方便您快速进行开发。

基础项目:

Gradle依赖

dependencies {
    implementation 'com.classic.android:core:1.6'
}

基于RxJava的版本: 示例代码点这里

Gradle依赖

dependencies {
    implementation 'com.classic.android:rxjava:2.1'
}

基于OkHttp的版本: 示例代码点这里

Gradle依赖

dependencies {
    implementation 'com.classic.android:okhttp:2.1'
}

工具类:

Gradle依赖

dependencies {
    implementation 'com.classic.android:utils:1.1'
}
文件名 描述
AppInfoUtil 应用程序相关信息工具类
BitmapUtil bitmap工具类
ByteUtil 字节处理工具类
CloseUtil Closeable工具类
ConversionUtil 单位转换工具类
CursorUtil Cursor工具类
DataUtil 数据非空检查工具类
DateUtil 日期操作工具类
DeviceUtil 设备信息工具类
EditTextUtil 文本输入框工具类
FileUtil 文件操作工具类
HtmlUtil HTML处理类
IntentUtil Intent工具类
IpUtil 获取设备IP地址
KeyBoardUtil 输入法工具类
MatcherUtil 正则匹配工具类
MoneyUtil 高精度数据计算工具类
NetworkUtil 网络状态工具类
ResourceUtil AssetsRaw资源文件操作工具类
SizeUtil 字节、毫秒相关常量
StringUtil 字符串工具类
ToastUtil Toast工具类
ViewHolder View复用工具类
WifiHelper WIFI工具类
WindowUtil 屏幕相关工具类

使用步骤

第一步:按需添加依赖

第二步:

public class YourApplication extends Application {

    @Override public void onCreate() {
        super.onCreate();

        ...

        final BasicProject.Builder builder = new BasicProject.Builder()
                .setDebug(BuildConfig.DEBUG)
                .setRootDirectoryName(getPackageName())
                //自定义异常信息处理,实现ICrashProcess
                .setExceptionHandler(new CustomCrashProcessImpl())
                .setLog(BuildConfig.DEBUG ? LogLevel.ALL : LogLevel.NONE);

        BasicProject.config(builder);
    }
}

代码示例

Activity示例

public class TestActivity extends BaseActivity {
    private RecyclerView mRecyclerView;

    @Override public int getLayoutResId() {
        return R.layout.activity_main;
    }

    //初始化一些数据
    @Override public void initData() {
        super.initData();
        Intent intent = getIntent();
        params = intent.getStringExtra(...);
    }

    //初始化view
    @Override public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mRecyclerView = (RecyclerView) findViewById(R.id.main_rv);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        if(savedInstanceState == null){
            /**
             * 使用Fragment
             * 参数1:被替换为Fragment的视图id
             * 参数2:BaseFragment对象
             */
            changeFragment(R.id.fragment_layout, new ImageFragment());
        }

    }

    //以下为可选方法,根据需要进行重载.
    //方法执行顺序:
    //onSetContentViewBefore() --> initData() --> initView(Bundle savedInstanceState) --> register()

    //这里可以注册一些广播、服务
    @Override public void register() { }
    //注销广播、服务
    @Override public void unRegister() { }
    //只有第一次才会执行,这里可以做一些界面功能引导
    @Override public void onFirst() { }
    //这个方法会在setContentView(...)方法之前执行
    @Override public void onSetContentViewBefore() { }
    //view点击事件统一处理
    @Override public void viewClick(View v) { }
}

Fragment示例

public class TestFragment extends BaseFragment {
    private TextView mTitle;

    @Override public int getLayoutResId() {
        return R.layout.activity_listview_item;
    }

    @Override public void initView(View parentView, Bundle savedInstanceState) {
        super.initView(parentView, savedInstanceState);
        mTitle = (TextView) parentView.findViewById(R.id.item_title_tv);
    }

    //以下为可选方法,根据需要进行重载.
    //方法执行顺序:
    //initData() --> initView(View parentView, Bundle savedInstanceState) --> register()

    //这里可以注册一些广播、服务
    @Override public void register() { }
    //注销广播、服务
    @Override public void unRegister() { }
    //只有第一次才会执行,这里可以做一些界面功能引导
    @Override public void onFirst() { }
    @Override public void initData() { }
    //view点击事件统一处理
    @Override public void viewClick(View v) { }

    //Fragment被切换到前台时调用
    @Override public void onFragmentShow() { }
    //Fragment被切换到后台时调用
    @Override public void onFragmentHide() { }
}

启动页示例

public class SplashActivity extends BaseSplashActivity {

    @Override protected void setSplashResources(List<SplashImgResource> resources) {
        /**
         * SplashImgResource参数:
         * mResId - 图片资源的ID。
         * playerTime - 图片资源的播放时间,单位为毫秒。。
         * startAlpha - 图片资源开始时的透明程度。0-255之间。
         * isExpand - 如果为true,则图片会被拉伸至全屏幕大小进行展示,否则按原大小展示。
         */
        resources.add(new SplashImgResource(R.mipmap.splash, 1500, 100f, true));
        resources.add(new SplashImgResource(R.mipmap.splash1, 1500, 100f, true));
        resources.add(new SplashImgResource(R.mipmap.splash2, 1500, 100f, true));
    }

    @Override protected boolean isAutoStartNextActivity() {
        return false;
    }
    @Override protected Class<?> nextActivity() {
        return null;
        //如果isAutoStartNextActivity设置为true,这里需要指定跳转的activity
        //return MainActivity.class;
    }

    @Override protected void runOnBackground() {
        //这里可以执行耗时操作、初始化工作
        //请注意:如果执行了耗时操作,那么启动页会等到耗时操作执行完才会进行跳转
        //try {
        //  Thread.sleep(15 * 1000);
        //} catch (InterruptedException e) {
        //  e.printStackTrace();
        //}
    }
}

打印日志 更多使用方法点这里

XLog.d(content);
XLog.e(content);
XLog.w(content);
XLog.v(content);
XLog.json(jsonContent);
XLog.xml(xmlContent);

Android6.0权限管理 更多使用方法点这里

//以使用相机为例,在Activity/Fragment添加以下代码

private static final int REQUEST_CODE_CAMERA = 101;//请求相机权限的requestCode

@AfterPermissionGranted(REQUEST_CODE_CAMERA)
public void useCamera() {
    if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
        ToastUtil.showToast(getApplicationContext(), "相机权限已授权,可以开始使用相机了");
    } else {
        //请求权限
        EasyPermissions.requestPermissions(this, "应用需要访问你的相机进行拍照",
                                           REQUEST_CODE_CAMERA, Manifest.permission.CAMERA);
    }
}

@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
    //用户授权成功
}

@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
    //用户拒绝授权
}

感谢

XLog

EasyPermissions

关于

License

Copyright 2015 classic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.