20
2014
11

Android开发学习:首次启动判断

Android开发学习:开发过程中会需要对App应用进行判断是否是首次启动,或安装后第一次打开应用。

package com.zhengdecai.isfirststart;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 首次启动判断
 * 
 * @author 郑德才
 *
 */
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView isFisrt =(TextView) findViewById(R.id.isFisrt);
SharedPreferences sharedPreferences = this.getSharedPreferences(
"share", MODE_PRIVATE);//此处表示该应用程序专用
boolean isFirstRun = sharedPreferences.getBoolean("isFirstRun", true);//此处表示如果key "isFirstRun"对应的value没有值则默认为true,否则就把对应的value取出赋值给变量isFirstRun
Editor editor = sharedPreferences.edit();
editor.putBoolean("isFirstRun", false); //此处表示putBoolean(key, value),将value写入对应的key,而且是一一对应的
editor.commit(); //将isFirstRun写入editor中保存
if (isFirstRun) {
Log.d("debug", "第一次运行");
editor.putBoolean("isFirstRun", false);
Toast.makeText(this, "是第一次运行程序喔!", Toast.LENGTH_SHORT).show();
isFisrt.setText("是第一次运行程序喔!");
editor.commit();
} else {
Log.d("debug", "不是第一次运行");
Toast.makeText(this, "已经不是第一次运行程序了!", Toast.LENGTH_SHORT).show();
isFisrt.setText("已经不是第一次运行程序了!");
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。