安装后仅启动一次活动

时间:2016-12-15 15:02:14

标签: android android-activity sharedpreferences splash-screen splash

我有QR扫描仪应用程序。应用程序中有3个活动。

1)主要活动 - 打开相机并开始扫描的按钮

2)QR活动 - 扫描QR码

3)网络活动 - 成功扫描后,在应用程序中打开网页

此处,主要活动和QR活动应仅在初始安装后启动一次。我读过有关使用共享首选项的地方。但我有点困惑,我在哪里检查变量,如在哪个活动。我应该检查主要活动中的共享变量吗?

这是我的第一个应用。对不起,如果这是一个愚蠢的怀疑。

4 个答案:

答案 0 :(得分:2)

没错,你必须用SharedPreferences来做。

Here is a good explaination about how to use them

在显示的第一个活动 中,您必须在onCreate方法中添加这些行:

//this retrieve the sharedpreference element
SharedPreference myPref = this.getSharedPreferences(
  "prefName", Context.MODE_PRIVATE);
//this retrieve the boolean "firstRun", if it doesn't exists, it places "true"
var firstLaunch = myPref.getBoolean("firstLaunch", true);
//so, if it's not the first run do stuffs
if(!firstLaunch){
  //start the next activity
  finish();
}      
//else, if it's the first run, add the sharedPref
myPref.edit().putBoolean("firstLaunch", false).commit();

希望这会有所帮助

答案 1 :(得分:1)

要完成@Pier Giorgio Misley的回答,您可以对主要活动进行“firstLaunch”检查,或者将其置于另一个“泼水”活动中

将它放入主活动中只需将ui设置为某种中性色,直到您决定是否应该完成活动并启动Web活动或显示主活动逻辑

或者,你可以创建一个“启动”屏幕,它可以作为桥梁活动(显示一些徽标或漂亮的背景颜色),检查变量并决定打开哪个活动Android splash

答案 2 :(得分:0)

正如码头所提到的那样,省去它曾经被看过一次就是要走的路。但是,我发现在某些旧设备上,共享首选项不可靠!

我建议使用SQLite数据库。

按如下方式创建表

TABLE NAME: SEEN_ACTIVITY
Column 1: ID INT PRIMARY KEY
Column 2: SEEN VARCHAR

然后,一旦活动启动,检查是否有id =' 0'在SEEN_ACTIVITY。如果没有,则按如下方式插入一个,(0,true)。

然后,每次应用启动时,检查记录是否存在(0,true)。如果没有,请启动额外活动。

答案 3 :(得分:0)

#!/bin/bash

cd /
NOW=$(date +"%m_%d_%Y")
echo "Please input: 1=full 2=system 3=home"
read choice

if  ["$choice"="1" || "$choice"="2"]; then
echo "--- STARTING SYSTEMBACKUP ---"
tar -cvpzf systembackup_$NOW.tar.gz --exclude=/proc --exclude=/lost+found --exclude=/systembackup.tar.gz --exclude=/mnt --exclude=/sys --exclude=/dev --exclude=/run --exclude=/media --exclude=/home /
echo "--- SYSTEM BACKUP DONE ---"
fi

if  ["$choice"="1" || "$choice"="3"]; then
echo "--- STARTING HOMEBACKUP (excluding ~/Seafile) ---"
tar -cvpzf homebackup_$NOW.tar.gz --exclude=/home/matias/Seafile /home
echo "--- HOMEBACKUP DONE ---"
fi

在新的Splash Activity中写了这段代码