2013-10-15 66 views
1

我是android新手。我开发了一个小型聊天应用程序,它具有注册页面,登录屏幕和好友列表屏幕。当用户注册并登录时,他将被重定向到朋友列表页面。它运作良好。我用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(); 

} 

    }); 


} 
+2

后一些代码,是什么你已经完成 –

+0

在你的服务中创建一个线程并启动它。保持线程活着直到你想让你的应用程序运行:) –

+0

线程内应该做些什么@ satya – user2873624

回答

0

也许 '前景' 服务是你想要的。

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

前台服务是一个被认为是东西 用户正在积极了解并因此不能对系统 候选人杀时内存不足的服务。前台服务必须为位于“正在进行” 标题下的状态栏提供 通知,这意味着除非 服务停止或从前台移除,否则不能撤消通知。

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

相关问题