Java 始终返回登录屏幕
我有一个应用程序,通过Java 始终返回登录屏幕,java,android,android-manifest,Java,Android,Android Manifest,我有一个应用程序,通过启动活动屏幕启动5秒钟,然后打开登录活动屏幕,然后在输入正确的用户和密码后打开菜单活动(listActivity)然后每行单击打开MyCity活动 更新: 我想得到的是:你在我的应用程序中的任何位置,并且你出于任何原因离开我的应用程序不仅当你按下主页按钮时,而且例如: 您按home按钮检查另一个应用程序,然后想返回到我的应用程序 您已在whatsup或电子邮件上显示新消息,请打开whatsup或电子邮件,然后返回到我的应用程序 3-您离开手机一段时间后,想再次查看我的应用程
启动活动屏幕
启动5秒钟,然后打开登录活动屏幕
,然后在输入正确的用户和密码后打开菜单活动(listActivity)
然后每行单击打开MyCity活动
更新:
我想得到的是:你在我的应用程序中的任何位置,并且你出于任何原因离开我的应用程序不仅当你按下主页按钮时,而且例如:
finish()代码>用于启动活动和登录活动
我在清单中的登录活动中尝试了:android:clearTaskOnLaunch=“true”
,但它不起作用
如有任何建议,将不胜感激
请填写完整的工作代码。
登录活动:
public class Login extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button b = (Button) findViewById(R.id.loginbutton);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText username = (EditText) findViewById(R.id.login);
EditText password = (EditText) findViewById(R.id.password);
if(username.getText().toString().length() > 0 && password.getText().
toString().length() > 0 ) {
if(username.getText().toString().equals("test") && password.getText().
toString().equals("test")) {
Intent intent = new Intent(Login.this, Menu.class);
startActivity(intent);
finish(); }
} }
}); } }
菜单活动:
public class Menu extends ListActivity {
String classes[] = { "City1", "City2", "City3", "City4", "City5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this,
android.R.layout.simple_list_item_1, classes));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try {
Class ourClass = Class.forName("com.test.demo.MyCity");
Intent ourIntent = new Intent(Menu.this, ourClass);
ourIntent.putExtra("cheese", cheese);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}}
我的舱单:
因此,现在当按下“主页”按钮离开我的应用程序,然后返回到我的应用程序:
直接转到登录活动
public void onPause()
{
super.onPause();
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor=preferences.edit();
editor.putString("exit","true");
editor.commit();
}
但现在的主要目标是:
如果:
屏幕锁定当您远离手机时,或轻按电源按钮锁定手机
或
通知中打开的消息
或
从通知中打开电子邮件
或
你必须给
打电话并接听
然后返回到我的应用程序它不会进入登录活动,但您将返回到以前所在的页面
有什么建议吗,谢谢
此更新:
我使用了另一个代码来覆盖主按钮并控制后退按钮,而不是应用:android:clearTaskOnLaunch=“true”
要在清单中启动活动,只需将向下代码应用于菜单活动:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;}
else if (keyCode == KeyEvent.KEYCODE_HOME) {
Intent i=new Intent(Menu.this,Login.class);
startActivity(i);
finish();
return true;}
return super.onKeyDown(keyCode, event);}
事实上,我不知道你到底在问什么,你想按安卓设备内置的Home键开始一项活动吗 试试这个: 如果它能工作的话,那就太棒了,只需要通过一个意图在那里调用你的登录活动
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.KEYCODE_HOME) {
Toast.makeText(MainActivity.this, "Home Key is pressed
Toast.LENGTH_LONG).show();
return true;
}
Toast.makeText(MainActivity.this, "Didnt work", Toast.LENGTH_SHORT)
.show();
return super.dispatchKeyEvent(e);
};
不能覆盖“主页”按钮的默认行为。通过单击“上一步”按钮可以获得所需的结果,“主页”按钮的默认行为是转到主页屏幕。我的建议超越后退按钮。然后正确使用意图和设置标志,您可以转到登录屏幕
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
super.onKeyDown(keyCode, event);
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
Intent i= new Intent("yourpackage.login");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
finish();
break;
}
return super.onKeyDown(keyCode, event);
}
你的要求与android的工作方式背道而驰 当用户按下home(主页)按钮时,他希望当他返回应用程序时,一切都会保持原样,除非他长时间不在那里,或者如果他运行了一个消耗资源的应用程序 这就好像在windows上,当您单击ALT+TAB时,您不会期望看到在电子邮件客户端(或messenger,或您使用的任何应用程序)上登录
在任何情况下,您都可以将onWindowFocusChanged与任何(onResume、onStart、onRestart、onPause、onStop等)一起使用,并且只处理您希望使用的相关情况。应用程序无法在不修改框架代码的情况下重新路由home按钮。很抱歉,这是不可能的…这是不可能的,因为“主页”按钮是呼叫启动器应用程序的专用按钮,但您可以在kiosk模式下运行应用程序启动器应用程序,例如,请参阅和 要在启动器活动上打开活动,请在manifest.xml上的itentfilter上添加此项
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
您的代码几乎是正确的:要在启动应用程序时清除活动堆栈,您应该将
android:clearTaskOnLaunch=“true”
添加到您的根活动中,即Splash
,而不是登录
(它将是第一个运行的,请参阅确定根活动)
从
此属性仅对启动新任务的活动有意义
(根系活力)对于中的所有其他活动,它将被忽略
任务
因此,如果您将清单中的属性从Login
移动到Splash
,它应该可以工作,您的应用程序将始终从Splash屏幕启动
更新:要在来电、屏幕锁定或通知时重新启动应用程序,您必须将android:noHistory=“true”
(文档)添加到所有其他活动中,或调用finish()
的所有onPause()
方法
值得一提的是,我们的答案是如此的不同和复杂,因为你想要实现的目标完全违背了Android的核心概念(例如,这是一篇关于现有应用程序的好文章),所以真的,不要在你的私人应用程序外做类似的事情。很难准确说出你的要求,然而,我相信这就是你正在寻找的:。总结一下我在那里的帖子: 这是一个设计问题。您的启动程序活动不应是启动屏幕活动。相反,在主活动的onCreate方法中打开splash活动。这样,如果它是新打开的,则调用onCreate并显示启动屏幕。否则,如果只是恢复应用程序,即调用onResume,则不会调用打开启动屏幕活动 然后,您可以将清单更改为:
<activity
android:name=".ui.MainActivity"
android:noHistory="true"
android:screenOrientation="portrait"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ui.SplashActivity"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<activity
android:name=".ui.MainActivity"
android:noHistory="true"
android:screenOrientation="portrait"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ui.SplashActivity"/>
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) && isLock) {
//Logic when Home button pressed
return true;
}
else
return super.dispatchKeyEvent(event);
}
public void onCreate(Bundle ..)
{
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor=preferences.edit();
editor.putString("exit","false");
editor.commit();
.....
}
public void onResume()
{
super.onResume();
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(context);
String exit=preferences.getString("exit","");
if(exit.equals("true"))
{
Intent i=new Intent(this_activity.thi,login_activity.class);
startActivity(i);
finish()
}
}
public void onPause()
{
super.onPause();
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor=preferences.edit();
editor.putString("exit","true");
editor.commit();
}
class BaseActivity extends Activity {
onResume() {
super.onResume();
if (!havingUsernameAndPassword()) {
startActivity(loginActivity);
finish();
}
}
}
class AnyOfYourActivitiesExceptSpashAndLogin extends BaseActivity {
onResume() {
super.onResume();
// ... further code
}
// ... further code
}
class SimpleDataHolder {
public static String username = null;
public static String password = null;
}
class LoginActivity extends Activity {
// ...
// when user has entered valid username/password, store them in SimpleDataHolder:
SimpleDataHolder.username = username; // <-- store username entered
SimpleDataHolder.password = password; // <-- store password entered
}
class BaseActivity extends Activity {
onResume() {
super.onResume();
if (SimpleDataHolder.username == null || SimpleDataHolder.password == null) {
startActivity(loginActivity); // <-- go to login
finish(); // <-- end current activity
}
}
}
class AnyOfYourActivitiesExceptSpashAndLogin extends BaseActivity {
// ... your existing code (if any)
onResume() {
super.onResume(); // <-- this calls BaseActivity.onResume() which checks username/password
// ... your existing code (if any)
}