如何在Android中打开的应用程序上运行代码?

发布于 2024-11-27 15:45:41 字数 1585 浏览 3 评论 0原文

我的 Android 应用程序需要用户创建一个帐户才能使用该应用程序。帐户信息存储在 SQLite 数据库中。当应用程序启动时,我检查用户是否有帐户,如果没有,我会显示用户的注册活动。

现在,我收到用户的报告,他们有时会参加注册活动,即使他们已经创建了帐户。当他们关闭应用程序并再次重新打开它时,就会发生这种情况。

这是我正在使用的代码,我需要弄清楚问题可能是什么:

//MyApplication.java
public class MyApplication extends Application {
    private DataBaseUtility dbu;
    public boolean hasAccount;  

    @Override
    public void onCreate() {
        super.onCreate();

        //Init sqlite database
        this.dbu = new DataBaseUtility(this);

        //This loads the account data from the database and returns true if the user has already created an account
        this.hasAccount = loadAccount();
    }

    public boolean loadAccount() {
        boolean loadedData = false;

        String query = "SELECT data FROM tblaccount WHERE tblaccount.deleted=0";
        Cursor cursor = this.dbu.getCursor(query);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                loadedData = true;
            }
            cursor.close();
        }

        return loadedData;
    }
}

//MainActivity.java
public class MainActivity extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyApplication application = (MyApplication)getApplication();
        if (!application.hasAccount) {
            //Take the user to the sign up activity
        }
}

我的想法是,有时 MainActivity.onCreate()MyApplication.onCreate() 之前运行代码>.可以这样吗?

My Android app need the user to create an account to be able to use the app. The account info is stored in SQLite database. When the application starts I check if the user has an account, if not I show a sign up activity for the user.

Now I get reports from users that they sometimes comes to the sign up activity even if they've already created an account. This happens when they've closed the application and reopen it again.

This is the code I'm using and I need to figure out what the problem might be:

//MyApplication.java
public class MyApplication extends Application {
    private DataBaseUtility dbu;
    public boolean hasAccount;  

    @Override
    public void onCreate() {
        super.onCreate();

        //Init sqlite database
        this.dbu = new DataBaseUtility(this);

        //This loads the account data from the database and returns true if the user has already created an account
        this.hasAccount = loadAccount();
    }

    public boolean loadAccount() {
        boolean loadedData = false;

        String query = "SELECT data FROM tblaccount WHERE tblaccount.deleted=0";
        Cursor cursor = this.dbu.getCursor(query);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                loadedData = true;
            }
            cursor.close();
        }

        return loadedData;
    }
}

//MainActivity.java
public class MainActivity extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyApplication application = (MyApplication)getApplication();
        if (!application.hasAccount) {
            //Take the user to the sign up activity
        }
}

My idea is that maybe sometimes MainActivity.onCreate() runs before MyApplication.onCreate(). Can that be the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墟烟 2024-12-04 15:45:41

applicationonCreate 中,您将检查用户是否有帐户并设置布尔值。

您正在通过 application 的布尔值检查 MainActivityonCreate 用户是否拥有帐户。

applicationonCreate()MainActivityonCreate() 之前执行始终 案例!不可能出现不同的执行路径,并且由于 applicationonCreate() 没有 Runnable,因此它是 100% 保证。

请确保您的 DataBaseUtility 没有任何 Runnables。

无论如何仍然有几种方法可以重现错误!我现在不会陈述这些,但当您看到:

SOLUTION

MainActivity You have忘记更新application.hasAccount成功注册后, 您就可以知道它们~

public class MainActivity extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyApplication application = (MyApplication)getApplication();
        if (!application.hasAccount) {
            //Take the user to the sign up activity
            //if(successful) application.hasAccount = true
        }
}

为了避免数据库异常

我使用这个:

备注 为数据库使用更强大的持久状态保存会更好 - 即 SharedPreferences

boolean isOpened = false;
//When I need to open
if(!isOpened){
    //open
    isOpened = true;
}
//When I need to close
if(isOpened){
    //close
    isOpened = false;
}

onDestroy() {  //every onDestroy
    if(isOpened){
        //close
    }
}

In application's onCreate, you are checking if the user has an account and setting a boolean.

You are checking in the MainActivity's onCreate if the user has an account through the application's boolean.

application's onCreate() executing before MainActivity's onCreate() is always the case! It is impossible for a different execution path to occur and since application's onCreate() does not have a Runnable it is a 100% garantuee.

Please make sure you're DataBaseUtility does not have any Runnables.

Anyway STILL there are several ways to reproduce the error! I will not state these now but you can know them when you see:

SOLUTION

MainActivity You have forgotten to update application.hasAccount upon successfull sign up~

public class MainActivity extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyApplication application = (MyApplication)getApplication();
        if (!application.hasAccount) {
            //Take the user to the sign up activity
            //if(successful) application.hasAccount = true
        }
}

To avoid database exceptions

I use this:

REMARK It would be much better to use more strong persistent status saving for the database -i.e. SharedPreferences

boolean isOpened = false;
//When I need to open
if(!isOpened){
    //open
    isOpened = true;
}
//When I need to close
if(isOpened){
    //close
    isOpened = false;
}

onDestroy() {  //every onDestroy
    if(isOpened){
        //close
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文