(此处可更改)
protected void onCreate(Bundle savedInstanceState) {
LogHelper.releaseLog("StartActivity", "StartActivity init");
// add Third party jump
Intent intent = getIntent();
if(null != intent){
startThirdJump(intent,savedInstanceState);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);}
private void startThirdJump(Intent intent, Bundle savedInstanceState){
Bundle extraBundle = intent.getExtras();
if(null != extraBundle && extraBundle.containsKey("Data")){
String extraString = extraBundle.getString("Data");
LogHelper.releaseLog("StartActivity", "extraString:"+extraString);
parseExtraJump(extraString);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
setExtraJump();
}
}
parseExtraJump方法:根据从Bundle中获取的字符串,解析出相应的页面ID和参数Map
private void parseExtraJump(String info){
if(mExtraJumpParser==null){
mExtraJumpParser = new ExtraJumpParser();
}
mExtraJumpParser.mParseData = info;
mExtraJumpParser.run();
}
此处是利用一个Parser来解析的,其实完全可以在主线程中新开一个方法来解析。或提供Helper帮助类来解析。
public class ExtraJumpParser extends BaseParser{
private KEY_PAGEID mPageID = null;
private Map mJumpMap = null;
public KEY_PAGEID getExtraPageID(){
return mPageID;
}
@Override
public void run()
{
parseExtraJump();
}
private void parseExtraJump(){
mPageID = null;
String[] paramsStrings = mParseData.split("&");
String[] params;
mJumpMap = new HashMap();
for(int i=0;i getJumpParam(){
return mJumpMap;
}
}
该Parser的
原理是:根据传人的字符串参数,按照“&”符号将信息拆解,再根据“=”将信息分为key,value结构。原结构为("page='detail'&sid='88988'")。然后对外提供获取方法。
private void setExtraJump() {
KEY_PAGEID pageId = mExtraJumpParser.getExtraPageID();
if (pageId != null) {
Map param = mExtraJumpParser.getJumpParam();
if (pageId == KEY_PAGEID.PAGE_PLAY) {
PLAY_DATA playData = new PLAY_DATA();
playData.sid = (String) param.get(KEY_PAGEDATA.SID);
playData.contentType = (String) param.get(KEY_PAGEDATA.CONTENTTYPE);
playData.supplyType = (String) param.get(KEY_PAGEDATA.SUPPLYTYPE);
param.put(KEY_PAGEDATA.PLAYDATA, playData);
StaticFunction.getPageHelper().jumpPage(KEY_PAGEID.PAGE_PLAY,param);
}
else {
StaticFunction.getPageHelper().jumpPage(pageId, param);
}
}
StaticFunction.getPageHelper().finishPage(null);
}
页面跳转的帮助类:PageHelper
public class PageHelper {
private Map> mActivityInfo = new HashMap>();
private ArrayList mActivityList = new ArrayList();
private ArrayList mPageIDList = new ArrayList();
/**
* 释放资源
*/
public void release() {
mActivityInfo.clear();
mActivityList.clear();
mPageIDList.clear();
}
/**
* 添加activity实例
*
* @param context
* 上下文
*/
public void addActivity(Activity context) {
if (mActivityList != null) {
mActivityList.add(context);
}
}
/**
* 跳转页面
*
* @param context
* 当前页面的上下文
* @param id
* 跳转页面ID
* @param value
* 跳转页面所需参数
*/
public void jumpPage(KEY_PAGEID id, Map value) {
if (mActivityInfo != null) {
mActivityInfo.put(id, value);
mPageIDList.add(id);
}
if (mActivityList != null && mActivityList.size() > 0) {
Class> className = getJumpClassName(id);
Activity context = mActivityList.get(mActivityList.size() - 1);
Intent intent = new Intent(context, className);
context.startActivity(intent);
}
}
/**
* 第三方跳转入口
*
* @param context
* 上下午
* @param action
* 跳转ACTION,统一在Define ACTION_EXTRAAPP中定义
* @param page
* 跳转页面,统一在 Define PAGE_EXTRAAPP中定义
*/
public void jumpExtraAPP(Context context, String action, String page) {
String pageInfoString = page;
Intent testIntent = new Intent();
testIntent.setAction(action);
testIntent.putExtra("Data", pageInfoString);
testIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(testIntent);
}
/**
* 返回至某一页面
*
* @param id
* 页面ID
*/
public void backToPage(KEY_PAGEID id) {
if (mPageIDList.contains(id)) {
int index = mPageIDList.lastIndexOf(id);
KEY_PAGEID tmpID = null;
Activity context = null;
int needFinishCount = (mPageIDList.size() - 1) - index;
for (int i = 0; i < needFinishCount; i++) {
tmpID = mPageIDList.get(mPageIDList.size() - 1);
mActivityInfo.remove(tmpID);
context = mActivityList.get(mActivityList.size() - 1);
mActivityList.remove(mActivityList.size() - 1);
mPageIDList.remove(mPageIDList.size() - 1);
context.finish();
}
}
}
/**
* 销毁当前页面并返回至上级页面
*
* @param id
* 页面id
*/
public void finishPage(KEY_PAGEID id) {
if (mActivityInfo != null && id != null) {
if (!mActivityInfo.containsKey(id) && mActivityInfo.size() > 0) {
return;
}
mActivityInfo.remove(id);
mPageIDList.remove(id);
}
if (StaticFunction.getHandler().hasMessages(KEY_MSGINFO.KEY_PAGEFINISH)) {
StaticFunction.getHandler().removeMessages(KEY_MSGINFO.KEY_PAGEFINISH);
}
if (mActivityList != null && mActivityList.size() > 0) {
Activity context = mActivityList.get(mActivityList.size() - 1);
mActivityList.remove(mActivityList.size() - 1);
context.finish();
}
if (mActivityList.size() == 0 && id != null) {
GeneralHelper.release();
}
}
/**
* 延迟销毁页面
*
* @param id
* 页面id
*/
public void delayFinishPage(KEY_PAGEID id) {
Message msg = Message.obtain(StaticFunction.getHandler(), KEY_MSGINFO.KEY_PAGEFINISH, id);
StaticFunction.getHandler().sendMessageDelayed(msg, CommonDefine.ACTIVITY_FINISH_WAITING);
}
/**
* 获取当前页面的跳转参数信息
*
* @param id
* 页面ID
* @return 返回参数信息
*/
public Map getPageInfo(KEY_PAGEID id) {
if (mActivityInfo.containsKey(id)) {
return mActivityInfo.get(id);
}
return null;
}
/**
* 获取当前激活页面的Activity上下文
*
* @return 返回Activity上下文,失败返回null
*/
public Activity getCurActivity() {
if (mActivityList != null && mActivityList.size() > 0) {
return mActivityList.get(mActivityList.size() - 1);
}
return null;
}
/**
* 获取当前的页面id
*/
public KEY_PAGEID getCurPageID(){
if(mPageIDList!=null&&mPageIDList.size()>0){
return mPageIDList.get(mPageIDList.size()-1);
}
return KEY_PAGEID.PAGE_NULL;
}
/////////////////////////////////////////
// TODO
private Class> getJumpClassName(KEY_PAGEID id) {
switch (id) {
case PAGE_DETAIL:
break;
case PAGE_HOME:
return LauncherActivity.class;
case PAGE_LIVE:
return LiveActivity.class;
case PAGE_LEAGUE:
return LeagueActivity.class;
case PAGE_PLAY:
return PlayActivity.class;
case PAGE_EVENTDETAIL:
return EventDetailActivity.class;
case PAGE_COLLECTION:
return CollectionActivity.class;
default:
break;
}
return null;
}
public void jumpExtraAPP(String action, String page) {
try {
String pageInfoString = page;
Intent testIntent = new Intent();
testIntent.setAction(action);
Log.i("info", pageInfoString);
testIntent.putExtra("Data", pageInfoString);
testIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(testIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
-----------------------------------------------------------------
另一种跳转,使用ComponentName实现 :
跳转至登录页
public void jumpToLoginActivity( ){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName("com.mcptt", "com.mcptt.LoginActivity");
intent.setComponent(cn);
String usernumber = "mcptt_usernumber";
String password = "mcptt_password";
String server_domain = "mcptt_service_domain";
String server_ip = "mcptt_service_ip";
String server_port = "mcptt_service_port";
String login_type = "key_login_type";
String login_auto = "key_login_auto";
Bundle bundle = new Bundle();
bundle.putInt(login_type, 0);// 0 域名登录,1 ip登录,互斥
bundle.putString(server_domain, "real.caltta.com.cn:80");
//bundle.putString(server_ip, "114.55.250.9");
//bundle.putInt(server_port, 13001);
bundle.putString(usernumber, "20025201001");
bundle.putString(password, "111111");
bundle.putBoolean(login_auto, true);
intent.putExtras(bundle);
try {
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}