1

我是安卓新手。我开发了一个小型聊天应用程序,它有注册页面、登录屏幕和好友列表屏幕。当用户注册并登录时,他将被重定向到好友列表页面。它运行良好。我在用户第二次注册时使用了 sharedpreference,他们直接重定向到朋友列表页面。但在这里不是我关闭我的应用程序。

谁能帮我?

提前致谢。

这是我的 Registration.java

private ServiceConnection mConnection = new ServiceConnection() {


public void onServiceConnected(ComponentName className, IBinder service) {

        imService = ((IMService.IMBinder)service).getService();
    }

    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        // Because it is running in our same process, we should never
        // see this happen.
     imService = null;
        Toast.makeText(SignUp.this, R.string.local_service_stopped,
                Toast.LENGTH_SHORT).show();
    }
};

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);



setContentView(R.layout.sign_up_screen);
setTitle("Sign up");


Button signUpButton = (Button) findViewById(R.id.signUp);
Button cancelButton = (Button) findViewById(R.id.cancel_signUp);
usernameText = (EditText) findViewById(R.id.userName);
passwordText = (EditText) findViewById(R.id.password);
passwordAgainText = (EditText) findViewById(R.id.passwordAgain);
eMailText = (EditText) findViewById(R.id.email);

SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
if(pref.getBoolean("activity_executed", false)){
Intent intent = new Intent(this, FriendList.class);
startActivity(intent);
finish();
} else {
Editor ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.putString("username", usernameText.getText().toString());
ed.putString("password", passwordText.getText().toString());
ed.commit();
}

signUpButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0)
{   
if (usernameText.length() > 0 &&    
passwordText.length() > 0 &&
passwordAgainText.length() > 0 &&
eMailText.length() > 0
)

{

if (passwordText.getText().toString().equals(passwordAgainText.getText().toString())){

if (usernameText.length() >= 5 && passwordText.length() >= 5) {

Thread thread = new Thread(){
String result = new String();
@Override
public void run() {
result = imService.signUpUser(usernameText.getText().toString(),
passwordText.getText().toString(),
eMailText.getText().toString());

handler.post(new Runnable(){

public void run() {
if (result.equals(SERVER_RES_RES_SIGN_UP_SUCCESFULL)) {
/*editor.putString("register","true");
editor.commit();*/
Toast.makeText(getApplicationContext(),R.string.signup_successfull,    Toast.LENGTH_LONG).show();
startActivity(new Intent (SignUp.this, Login.class));
SignUp.this.finish();

}
 else if (result.equals(SERVER_RES_SIGN_UP_USERNAME_CRASHED)){
Toast.makeText(getApplicationContext(),R.string.signup_username_crashed,     Toast.LENGTH_LONG).show();
//showDialog(SIGN_UP_USERNAME_CRASHED);
}
else //if (result.equals(SERVER_RES_SIGN_UP_FAILED))
{
Toast.makeText(getApplicationContext(),R.string.signup_failed,   Toast.LENGTH_LONG).show();

}   
 }

 });
 }

 };
thread.start();
 }
else{
Toast.makeText(getApplicationContext(),R.string.username_and_password_length_short, Toast.LENGTH_LONG).show();

}   
}
 else {
      Toast.makeText(getApplicationContext(),R.string.signup_type_same_password_in_password_fields, Toast.LENGTH_LONG).show();

//showDialog(TYPE_SAME_PASSWORD_IN_PASSWORD_FIELDS); }

}
else {
 Toast.makeText(getApplicationContext(),R.string.signup_fill_all_fields,    Toast.LENGTH_LONG).show();
 }  
}
});


}

这是 Login.java

 private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {

        imService = ((IMService.IMBinder)service).getService();

        if (imService.isUserAuthenticated() == true)
        {
         Intent i = new Intent(Login.this, FriendList.class);   
startActivity(i);
Login.this.finish();
        }
    }

    public void onServiceDisconnected(ComponentName className) {

     imService = null;
        Toast.makeText(Login.this, R.string.local_service_stopped,
                Toast.LENGTH_SHORT).show();
    }
   };
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

 startService(new Intent(Login.this, IMService.class)); 


    setContentView(R.layout.login_screen);
    setTitle("Login");

    Button loginButton = (Button) findViewById(R.id.login);
    cancelButton = (Button) findViewById(R.id.cancel_login);
    usernameText = (EditText) findViewById(R.id.userName);
    passwordText = (EditText) findViewById(R.id.password);

    loginButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0)
  { 
 if (imService == null) {
 Toast.makeText(getApplicationContext(),R.string.not_connected_to_service,      Toast.LENGTH_LONG).show();

return;
}
 else if (imService.isNetworkConnected() == false)
{
Toast.makeText(getApplicationContext(),R.string.not_connected_to_network, Toast.LENGTH_LONG).show();

}
else if (usernameText.length() > 0 &&
passwordText.length() > 0)
{

Thread loginThread = new Thread(){
 private Handler handler = new Handler();
@Override
public void run() {
String result = null;
try {
result = imService.authenticateUser(usernameText.getText().toString(),   passwordText.getText().toString());
} catch (UnsupportedEncodingException e) {

e.printStackTrace();
}
 if (result == null || result.equals(AUTHENTICATION_FAILED))
{

handler.post(new Runnable(){
 public void run() {    
Toast.makeText(getApplicationContext(),R.string.make_sure_username_and_password_correct, Toast.LENGTH_LONG).show();


}   
});

}
else {


handler.post(new Runnable(){
public void run() { 
Intent i = new Intent(Login.this, FriendList.class);    
//i.putExtra(FRIEND_LIST, result);
startActivity(i);   
Login.this.finish();
}   
});

}

}
};
loginThread.start();

 }
else {

Toast.makeText(getApplicationContext(),R.string.fill_both_username_and_password,     Toast.LENGTH_LONG).show();

}   
}
    });

    cancelButton.setOnClickListener(new OnClickListener(){

public void onClick(View arg0)
{   
imService.exit();
finish();

}

    });


}
4

1 回答 1

0

也许“前台”服务就是您想要的。

http://developer.android.com/guide/components/services.html#Foreground

前台服务是一种被认为是用户主动意识到的服务,因此不会在内存不足时被系统杀死。前台服务必须为状态栏提供通知,该通知位于“正在进行”标题下,这意味着除非服务停止或从前台删除,否则无法解除通知。

例如,从服务播放音乐的音乐播放器应该设置为在前台运行,因为用户明确知道它的操作......

于 2013-10-15T11:11:29.813 回答