0

我正在尝试做一个安全应用程序,如果密码对话框错误(在 Theme.Dialog 中使用 Activity)将重新启动,则需要用户输入密码,但问题是 Activity 没有启动。对于后退按钮,如果我按两次后退,它将直接关闭对话框。

package nyp.android.project;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PasswordDialog extends Activity {

    private static final String TAG = "Logging Activity";
    private static boolean isMyServiceRunning;
    //Context context;   
    Button login;
    EditText inputPassword;
    String password;

    protected void onCreate(Bundle savedInstanceState) 
    {
        //this.context = context;
        super.onCreate(savedInstanceState);
        setContentView (R.layout.password_dialog);



        final SharedPreferences passwdfile = getSharedPreferences(    
                PhoneFinder.PASSWORD_PREF_KEY, 0); 

        final String correctSHA1 = passwdfile.getString(PhoneFinder.PASSWORD_PREF_KEY, null);

        login = (Button) findViewById(R.id.btnLogin);
        inputPassword = (EditText) findViewById(R.id.loginPassword);


        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                password = inputPassword.getText().toString();               
                final String SHA1hash = PhoneFinder.getSHA1(password); 

                if (correctSHA1.equals(SHA1hash)) {
                    Toast.makeText(PasswordDialog.this, "Correct", Toast.LENGTH_LONG).show();
                    finish();   
                    stopService(new Intent(getBaseContext(), MyService.class));
                    Log.v(TAG, "SHA1 Hash:" + SHA1hash);
                    Log.v(TAG, "Correct SHA1:" + correctSHA1);
                }
                else {
                    Toast.makeText(PasswordDialog.this, "Wrong", Toast.LENGTH_LONG).show();
                    Intent Act2Intent = new Intent(getBaseContext(), PasswordDialog.class);              
                    startActivity(Act2Intent);          
                    finish();
                    Log.v(TAG, "SHA1 Hash:" + SHA1hash);
                    Log.v(TAG, "Correct SHA1:" + correctSHA1);


                }

            }
        });
        }

    @Override
    public void onBackPressed()
    {
        Intent indent = new Intent(this, PasswordDialog.class);
        startActivity(indent);
    }


}
4

3 回答 3

1

对于后退按钮的问题:

覆盖活动的方法“onBackPressed()”。请参阅文档

@Override
public void onBackPressed()
{
    Intent indent = new Intent(this, MyActivityToStart.class);
    startActivity(intent);
}

不要调用“super.onBackPressed()”,因为这会关闭活动。

于 2012-07-25T16:27:33.593 回答
0

将其用于 Intent

Intent Act2Intent = new Intent(PresentActivity.this, PasswordDialog.class);              
startActivity(Act2Intent);  
于 2012-07-25T16:28:55.590 回答
0

找到了我只需要调用finish()的问题的解决方案;在 startActivity() 之前

于 2012-07-25T16:53:25.833 回答