智慧北京:引导页面数据的缓存跳转

本文介绍了一个Android应用的启动流程,包括如何通过按钮点击事件来设置应用是否首次启动,并据此进行不同的页面跳转处理。此外,还涉及了使用SharedPreferences来保存应用状态的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、给Button设置监听器

2、当点下按钮时,设置Boolean的缓存数据

3、跳转到MainUI,结束自己

———————WelcomeUI.java—————————————

package huaxa.it.zhihuidemo;

import huaxa.it.zhihuidemoUtils.CacheUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;

/**
 * @项目名: ZhiHuiDemo
 * @包名: huaxa.it.zhihuidemo
 * @类名: WelcomeUI
 * @创建者: 黄夏莲
 * @创建时间: 2016年10月3日 ,上午10:52:21
 * 
 * @描述: TODO
 */
public class WelcomeUI extends Activity
{

	.....

	protected void doJump()
	{
		// 页面跳转
		// 根据情况进行页面跳转
		// 如果是第一次打开应用程序,那么久进入引导页面,否则进入主页
		Boolean isFirstStart = CacheUtils.getBoolean(this, KEY_FIRST_START,
				true);
		if (isFirstStart)
		{
			Log.d(TAG, "进入引导页面");
			Intent intent = new Intent(this, GuideUI.class);
			startActivity(intent);

		} else
		{
			Log.d(TAG, "进入主页面");
			Intent intent = new Intent(this,MainUI.class);
			startActivity(intent);
		}
		finish();

	}
}


mPager.setOnPageChangeListener(this);

public void onClick(View v)
	{
		// 设置点击事件
		if(v == start_btn){
			clickStart();
		}
		
	}

	private void clickStart()
	{
		//设置已经开启过应用
		CacheUtils.setBoolean(this,WelcomeUI.KEY_FIRST_START, false);
		//页面跳转
		Intent intent = new Intent(this,MainUI.class);
		startActivity(intent);
		//结束自己
		finish();
	}
package huaxa.it.zhihuidemoUtils;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


/**
 * @项目名: ZhiHuiDemo
 * @包名: huaxa.it.zhihuidemoUtils
 * @类名: CacheUtils
 * @创建者: 黄夏莲
 * @创建时间: 2016年10月3日 ,下午6:09:45
 * 
 * @描述:跳转页面
 * 
 */
public class CacheUtils
{
<span style="white-space:pre">	</span>private final static String<span style="white-space:pre">	</span>sharedPreferencesName<span style="white-space:pre">	</span>= "zhbj";
<span style="white-space:pre">	</span>private static SharedPreferences mpreferences;//静态的,从内存取(只需取1次),比读文件快(每次都要读取)
<span style="white-space:pre">	</span>private static SharedPreferences getsp(Context context){
<span style="white-space:pre">		</span>if(mpreferences == null){
<span style="white-space:pre">			</span>mpreferences = context.getSharedPreferences(
<span style="white-space:pre">					</span>sharedPreferencesName, Context.MODE_PRIVATE);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return mpreferences;
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 通过sp获得boolean类型的数据,没有默认为false
<span style="white-space:pre">	</span> * 
<span style="white-space:pre">	</span> * @param context
<span style="white-space:pre">	</span> *            :上下文
<span style="white-space:pre">	</span> * @param key
<span style="white-space:pre">	</span> *            :存储的key
<span style="white-space:pre">	</span> * @return
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static boolean getBoolean(Context context, String key)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>// TODO Auto-generated method stub
//<span style="white-space:pre">		</span>SharedPreferences mpreferences = context.getSharedPreferences(
//<span style="white-space:pre">				</span>sharedPreferencesName, Context.MODE_PRIVATE);
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>mpreferences = getsp(context);
<span style="white-space:pre">		</span>return mpreferences.getBoolean(key, false);// 第二个参数为缺省值,如果preference中不存在该key,将返回缺省值
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 通过sp获得boolean类型的数据,没有默认为false
<span style="white-space:pre">	</span> * 
<span style="white-space:pre">	</span> * @param context
<span style="white-space:pre">	</span> *            :上下文
<span style="white-space:pre">	</span> * @param key
<span style="white-space:pre">	</span> *            :存储的key
<span style="white-space:pre">	</span> * @param defValue
<span style="white-space:pre">	</span> *            :默认值
<span style="white-space:pre">	</span> * @return
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static boolean getBoolean(Context context, String key,
<span style="white-space:pre">			</span>boolean defValue)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>// TODO Auto-generated method stub
//<span style="white-space:pre">		</span>SharedPreferences mpreferences = context.getSharedPreferences(
//<span style="white-space:pre">				</span>sharedPreferencesName, Context.MODE_PRIVATE);
<span style="white-space:pre">		</span>mpreferences = getsp(context);
<span style="white-space:pre">		</span>return mpreferences.getBoolean(key, defValue);
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 
<span style="white-space:pre">	</span> * 设置Boolean的缓存数据
<span style="white-space:pre">	</span> * 
<span style="white-space:pre">	</span> * @param context
<span style="white-space:pre">	</span> *            :上下文
<span style="white-space:pre">	</span> * @param key
<span style="white-space:pre">	</span> *            :缓存对应的key
<span style="white-space:pre">	</span> * @param value
<span style="white-space:pre">	</span> *            :缓存对应的值
<span style="white-space:pre">	</span> * @return
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static void setBoolean(Context context, String key, boolean value)
<span style="white-space:pre">	</span>{
//<span style="white-space:pre">		</span>SharedPreferences mpreferences = context.getSharedPreferences(
//<span style="white-space:pre">				</span>sharedPreferencesName, Context.MODE_PRIVATE);
<span style="white-space:pre">		</span>mpreferences = getsp(context);
<span style="white-space:pre">		</span>Editor editor = mpreferences.edit();// 获取编辑器
<span style="white-space:pre">		</span>editor.putBoolean(key, value);
<span style="white-space:pre">		</span>editor.commit();


<span style="white-space:pre">	</span>}
}

package huaxa.it.zhihuidemo;

import android.app.Activity;
import android.os.Bundle;

/**
 * @项目名:     ZhiHuiDemo
 * @包名:         huaxa.it.zhihuidemo
 * @类名:         MainUI
 * @创建者:     黄夏莲
 * @创建时间: 2016年10月4日 ,下午11:38:11
 * 
 * @描述:          主页面 
 */
public class MainUI extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}
}

<activity
            android:name=".MainUI"
            android:label="@string/app_name" >
        </activity>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值