开源中国APP Android端源码分析系列(一)

简述

这篇文章是基于OSCHINA
Android客户端4.1.7版本的分析,之前很多人都分析过源码,但是都是几年前的代码分析,随着时间的推移,开源中国的源码也在变化,接下来的一段时间我将分享我通过学习开源中国的代码所获得东西。

启动页面

研究一个APP或者一个网站的源码,最开始的时候一定会从APP或者网站的入口学习。OSCHINA的入口从下的配置文件可以找到AndroidManifest.xml


        

        
            
                

                
            
        
        ......
        ......

通过可以判断开源中国的入口文件就是SplashActivity

类的继承关系

extends
extends
SwipeBackActivity
BaseActivity
SplashActivity

SplashActivity源码

package net.oschina.app.improve.main.splash;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;

import com.baidu.mobstat.SendStrategyEnum;
import com.baidu.mobstat.StatService;

import net.oschina.app.OSCApplication;
import net.oschina.app.R;
import net.oschina.app.Setting;
import net.oschina.app.improve.account.AccountHelper;
import net.oschina.app.improve.app.AppOperator;
import net.oschina.app.improve.base.activities.BaseActivity;
import net.oschina.app.improve.bean.Launcher;
import net.oschina.app.improve.bean.User;
import net.oschina.app.improve.main.MainActivity;
import net.oschina.app.improve.main.introduce.IntroduceActivity;
import net.oschina.app.improve.main.tabs.DynamicTabFragment;
import net.oschina.app.improve.main.update.OSCSharedPreference;
import net.oschina.app.improve.utils.CacheManager;

import java.io.File;

import butterknife.Bind;

/**
 * 启动页设计
 * Created by huanghaibin on 2017/11/25.
 */

public class SplashActivity extends BaseActivity {


    @Bind(R.id.frameSplash)
    FrameLayout mFlameSplash;

    @Bind(R.id.fl_content)
    FrameLayout mFlameContent;

    private boolean isShowAd;

    public static void show(Context context) {
        System.out.println("SplashActivity:"+"show()方法");
        context.startActivity(new Intent(context, SplashActivity.class));
    }

    @Override
    protected int getContentView() {
        System.out.println("SplashActivity:"+"getContentView()方法");
        return R.layout.activity_splash;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
    }


    @Override
    protected void initData() {
        super.initData();
        System.out.println("SplashActivity:"+"initData()方法");
        StatService.setSendLogStrategy(this, SendStrategyEnum.APP_START, 1, false);
        StatService.start(this);
        Launcher launcher = CacheManager.readJson(OSCApplication.getInstance(), "Launcher", Launcher.class);
        String savePath = OSCApplication.getInstance().getCacheDir() + "/launcher";
        System.out.println("SplashActivity:"+"launcher方法");
        File file = new File(savePath);
        if (launcher != null && !launcher.isExpired() && file.exists()) {
            System.out.println("SplashActivity:"+"initData()方法");
            isShowAd = true;
            mFlameSplash.setVisibility(View.GONE);
            mFlameContent.setVisibility(View.VISIBLE);
            addFragment(R.id.fl_content, AdFragment.newInstance(launcher));
        }
        AppOperator.runOnThread(new Runnable() {
            @Override
            public void run() {
                doMerge();
            }
        });
    }

    private void doMerge() {
        // 判断是否是新版本

        if (Setting.checkIsNewVersion(this)) {
            // Cookie迁移
            System.out.println("SplashActivity:"+"doMerge()方法");
            String cookie = OSCApplication.getInstance().getProperty("cookie");
            if (!TextUtils.isEmpty(cookie)) {
                OSCApplication.getInstance().removeProperty("cookie");
                User user = AccountHelper.getUser();
                user.setCookie(cookie);
                AccountHelper.updateUserCache(user);
                OSCApplication.reInit();
            }
        }

        // 栏目相关数据合并操作
        DynamicTabFragment.initTabPickerManager();

        if (isShowAd)
            return;
        // Delay...
        try {
            Thread.sleep(800);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 完成后进行跳转操作
        redirectTo();
    }

    private void redirectTo() {
        System.out.println("SplashActivity:"+"redirectTo() 方法");
        if (OSCSharedPreference.getInstance().isFirstInstall()) {
            IntroduceActivity.show(this);
        } else {
            MainActivity.show(this);
        }
        finish();
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
    }
}

一个activity启动最开始应该是onCreate()方法,这个类中没有onCreate方法。是因为onCreate方法在抽象类BaseActivity中,基类中写了onCreate方法,以后的无论什么类继承基类就都可以,免去了重复代码。

你可能感兴趣的:(技术文章,移动端,源码分析)