Android修炼之道—高仿陌陌的项目

wei2bei132 任东卫 [email protected]

以前就在eoe上有关注这位大神,曾下载研究过他写的高仿开心,人人的android客户端。后来也在网上看到他写的高仿陌陌的,一直没有时间研究一下,今天就专门下载,看看聊天流程中有没有我可以借鉴学习的地方。该项目eoe上的地址:http://www.eoeandroid.com/thread-304239-1-1.html

Android修炼之道—高仿陌陌的项目_第1张图片Android修炼之道—高仿陌陌的项目_第2张图片

接下来,开始看源码了:

1.架构

父类0

public abstract class BaseActivity extends FragmentActivity {

    /** 初始化视图 **/
    protected abstract void initViews();

    /** 初始化事件 **/
    protected abstract void initEvents();

}

父类1

public abstract class TabItemActivity extends BaseActivity {
    protected abstract void init();
}

子类2

public class ContactTabsActivity extends TabItemActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacttabs);
    }
    @Override
    protected void initViews() {  
    }
    @Override
    protected void initEvents() {
    }
    @Override
    protected void init() {  
    }
}

可以看到,例如 NearByActivity 类中onCreate()的调用顺序是:

         setContentView(R.layout.activity_nearby);

    initPopupWindow();
    initViews();
    initEvents();
    init();


在BaseActivity中存储任务的队列
protected List<AsyncTask<Void, Void, Boolean>> mAsyncTasks
            = new ArrayList<AsyncTask<Void, Void, Boolean>>();

protected void putAsyncTask(AsyncTask<Void, Void, Boolean> asyncTask) {
    mAsyncTasks.add(asyncTask.execute());
}

protected void clearAsyncTask(){
    Iterator<AsyncTask<Void, Void, Boolean>> iterator = mAsyncTasks
            .iterator();
            while(iterator.hasNext()){
                AsyncTask<Void, Void, Boolean> asyncTask =
                    iterator.next();
                if(asyncTask != null && !asyncTask.isCancelled()){
                    asyncTask.cancel(true);
                }
            }
            mAsyncTasks.clear();
}

@Override
    protected void onDestroy() {
        clearAsyncTask();
        super.onDestroy();
    }

在BaseApplication中存储静态变量

    public List<NearByPeople> mNearByPeoples = new ArrayList<NearByPeople>();
    public List<NearByGroup> mNearByGroups = new ArrayList<NearByGroup>();

    public static List<String> mEmoticons = new ArrayList<String>();
    public static Map<String, Integer> mEmoticonsId = new HashMap<String, Integer>();
    public static List<String> mEmoticons_Zem = new ArrayList<String>();
    public static List<String> mEmoticons_Zemoji = new ArrayList<String>();


关于本地模拟数据,作者是这样做的

public Map<String, SoftReference<Bitmap>> mStatusPhotoCache
        = new HashMap<String, SoftReference<Bitmap>>();

public Bitmap getStatusPhoto(String imageName) {
    if (mStatusPhotoCache.containsKey(imageName)){
        Reference<Bitmap> reference = mStatusPhotoCache.get(imageName);
        if (reference.get() == null || reference.get().isRecycled()){
            mStatusPhotoCache.remove(imageName);
        } else {
            return reference.get();
        }
    }
    InputStream is = null;
    Bitmap bitmap = null;
    try{
        is = getAssets().open(STATUS_PHOTO_DIR + imageName);
        bitmap = BitmapFactory.decodeStream(is);
        if(bitmap == null){
            throw new FileNotFounceException(imageName + "is not find");
        }
        mStatusPhotoCache.put(imageName, new SoftReference<Bitmap>(bitmap));

        return bitmap;
    } catch (Exception e){
        return null;
    } finally {
        try {
            if (is != null){
                is.close();
                is = null;
            }
        } catch (IOException e){
            
        }
    }
    }
}

2.库

陌陌首先是定位,所以可以看到百度LBS的库

Android修炼之道—高仿陌陌的项目_第3张图片Android修炼之道—高仿陌陌的项目_第4张图片

因为陌陌是根据位置定位,这个很好理解,可是在armeabi中有libimomoUrl.so我就不懂了,网上搜了一下也没有介绍,可能是作者自己写的。

1.百度地图采用的是2.2版本

在AndroidManifest.xml中定义

<service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote" >
            <intent-filter>
                <action android:name="com.baidu.location.service_v2.2" >
                </action>
            </intent-filter>
</service>
在BaseApplication中开启定位服务

// 获取当前用户位置
        mLocationClient = new LocationClient(getApplicationContext());
        mLocationClient.setAK("60b43d1a9513d904b6aa2948b27b4a20");
        mLocationClient.registerLocationListener(new BDLocationListener() {

            @Override
            public void onReceivePoi(BDLocation arg0) {
            }

            @Override
            public void onReceiveLocation(BDLocation arg0) {
                mLongitude = arg0.getLongitude();
                mLatitude = arg0.getLatitude();
                Log.i("地理位置", "经度:" + mLongitude + ",纬度:" + mLatitude);
                mLocationClient.stop();
            }
        });
        mLocationClient.start();
        mLocationClient.requestOfflineLocation();
        System.out.println("开始获取");

关于另一个.so,有如下类:

public class JniManager {
    static {
        // 加载so文件
        System.loadLibrary("immomoUrl");
    }
    private static JniManager mJniManager;

    /**
     * 获取单例对象
     *
     * @return
     */
    public static JniManager getInstance() {
        if (mJniManager == null) {
            mJniManager = new JniManager();
        }
        return mJniManager;
    }

    // 获取用户帮助URL地址
    public native String getHelpUrl();

    // 获取用户协议URL地址
    public native String getProtocolUrl();

    // 获取用户协议对话框URL地址
    public native String getAgreementDialogUrl();

    // 获取群组等级URL地址
    public native String getGroupLevelUrl();
}


3.工具

记录一下常用的工具类 NetWorkUtil

public class NetWorkUtils{
	private Context mContext;
	private State wifiState = null;
	private State mobileState = null;

	public NetWorkUtils(Context context){
		mContext = context;
	}


//import android.net.NetworkInfo.State;
	public enum NetWorkState{
		WIFI, MOBILE, NONE;
	}

//import android.net.ConnectivityManager;
	public NetWorkState getConnectState(){
		ConnectivityManager manager = (ConectivityManager) mContext
			.getSystemService(Context.CONNECTIVITY_SERVICE);
		manager.getActivieNetworkInfo();
		wifiState = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
			.getState();
		mobileState = manager.getNetworkInfo(ConnectivityMnager.TYPE_MOBILE)
			.getState();
		if (wifiState != null && mobileState != null
				&& State.CONNECTED != wifiState
				&& State.CONNECTED == mobileState) {
			return NetWorkState.MOBILE;
		} else if (wifiState != null && mobileState != null
				&& State.CONNECTED != wifiState
				&& State.CONNECTED != mobileState){
			return NetWorkState.NONE;
		} else if (wifiState != null && State.CONNECTED == wifiState){
			return NetWorkState.NONE;
		}
	}
}


你可能感兴趣的:(Android修炼之道—高仿陌陌的项目)