在我的应用程序中,我使用服务来满足某些要求,当我从 Activity1 切换到 Activity2 时,我会出现几秒钟的空白屏幕。在具有一些服务的 Activity2 中,这是我正在使用的代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(isOnline()){
}else{
alert();
}
dialog = new ProgressDialog(this);
dialog.setCancelable(false);
app_preferences = new AppPreferences(this);
new LongOperation().execute();
setContentView(R.layout.activity_accounts);
}
使用 asynctask 从 onCreate() 启动服务。new LongOperation().execute();
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
ProgressBar_show();
}
@Override
protected String doInBackground(String... params) {
LoggingAccounts();
return null;
}
@Override
protected void onPostExecute(String result) {
ProgressBar_hide();
}
}
private void LoggingAccounts() {
Account_Added_Log_States();
ArrayList<AccountInfo> _Accounts_list = new ArrayList<AccountInfo>();
accounts_data = new AccountDataSource(this);
accounts_data.open();
_Accounts_list = accounts_data.getAllAccounts();
accounts_data.close();
Log.i(TAG, "No of acc's-" + _Accounts_list.size());
for(AccountInfo acc : _Accounts_list) {
Log.i(TAG, "Acc type " +acc.getAcc_Type());
if(acc.getAcc_Type().equals(UsedStrings.GoogleAccount) && Gtalk_Log_State ) {
/////// service start
final Intent gtalk_intent = new Intent(AccountsActivity.this, GtalkService.class);
gtalk_intent.putExtra("Key", "gtalk service*****");
gtalk_intent.putExtra("user_name", acc.getAcc_Name());
gtalk_intent.putExtra("user_pass", acc.getAcc_Pass());
Thread t = new Thread(){
public void run(){
startService(gtalk_intent);
}
};
t.start();
}
}
}
像这样,我在此活动中使用 3 种不同的服务。当我从活动切换到黑屏时。