如何在每个打开的应用程序中运行一段代码? [英] How to run a piece of code every open App?

查看:146
本文介绍了如何在每个打开的应用程序中运行一段代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//in onCreate
btnOK.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v){
                         
     Action();//***run this function only once whenever push btnOK ,and when open app again run only once too
    
     Action2();//run normally 

}});

//----Outside
public void Action(){
   //action....
}
public void Action2(){
   //action....
}





非常感谢:D





实际上我发现了一些代码,但它只在安装时运行一次



THANKS A LOT really :D


actually I found some code but it's only run in once when install

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) {
    // run your one time code
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("firstTime", true);
    editor.commit();
}

推荐答案

参见 http://developer.android.com/training/basics/activity-lifecycle/index.html [ ^ ]了解有关应用程序生命周期的详细信息。您可以通过实施相关方法在特定点执行操作。
See http://developer.android.com/training/basics/activity-lifecycle/index.html[^] for details about the life cycle of applications. You can perform actions at specific points by implementing the relevant methods.


这篇关于如何在每个打开的应用程序中运行一段代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆