我在我的Android应用中设置了一个启动画面,它运行网络调用的长度,然后切换到主(地图)活动。
我想知道我是否可以在此启动画面中使用第二张图像作为列出赞助商的方式。我现在的飞溅持续时间长达5或6秒,所以我宁愿不添加第二次Splash活动。
我希望是我可以在当前活动中添加第二张图片。第一个用于2秒,第二个用于网络呼叫的其余部分。
这是我的代码的一部分。如果可以添加到现有代码中,请告诉我。谢谢!
// Splash screen timer
private static int SPLASH_TIME_OUT = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
ConnectivityManager conectivtyManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conectivtyManager.getActiveNetworkInfo() != null
&& conectivtyManager.getActiveNetworkInfo().isAvailable()
&& conectivtyManager.getActiveNetworkInfo().isConnected()) {
isConnected = true;
} else {
isConnected= false;
}
if(isConnected)
new LoadAllVendors().execute();
else
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent1 = new Intent(SplashScreen.this, ShoppingListActivity.class);
finish();
startActivity(intent1);
}
}, 5000);
}
}
class LoadAllVendors extends AsyncTask<String, String, String> {
/**
* getting All vendors from url
*/
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_vendors, "GET", params);
JSONObject json1 = jParser.makeHttpRequest(url_all_products, "GET", params);
JSONObject json2 = jParser.makeHttpRequest(url_all_vps, "GET", params);
dataSource = new VendorsDataSource(getApplicationContext());
dataSource1 = new ProductsDataSource(getApplicationContext());
dataSource2 = new VandPDataSource(getApplicationContext());
dataSource.open();
dataSource1.open();
dataSource2.open();
// Check your log cat for JSON reponse
//Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
int success1 = json1.getInt(TAG_SUCCESS);
int success2 = json2.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
vendors = json.getJSONArray(TAG_VENDORLOCNS);
vendorIDs = new ArrayList<>();
// looping through All Products
List<Vendor> allVendors = new ArrayList<>();
for (int i = 0; i < vendors.length(); i++) {
JSONObject c = vendors.getJSONObject(i);
Vendor vendor = new Vendor(c.getInt(TAG_VENDORID), c.getString(TAG_NAME), c.getDouble(TAG_VENDORLOCNLONG), c.getDouble(TAG_VENDORLOCNLAT), c.getInt(TAG_VENDORHEARTS), c.getInt(TAG_VENDORTODOS)
, c.getString(TAG_VENDOROWNER), c.getString(TAG_VENDOREMAIL), c.getString(TAG_VENDORPHONE), c.getString(TAG_ABOUT), c.getString(TAG_WEBSITE), c.getString(TAG_VENDORIMAGE));
allVendors.add(vendor);
MarkedVendor v = new MarkedVendor(c.getInt(TAG_VENDORID), c.getString(TAG_NAME), false, false);
vendorIDs.add(vendor.getId());
dataSource.createVendor(vendor);
dataSource.createSavedVendor(v);
}
dataSource.deleteVendorTable(vendorIDs);
dataSource.close();
}
if (success1 == 1) {
// products found
// Getting Array of Products
products = json1.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
Product product = new Product(c.getInt(TAG_PRODUCTID), c.getString(TAG_PRODUCTNAME), c.getString(TAG_DISTINGUISHER));
dataSource1.createProduct(product);
}
dataSource1.close();
}
if (success2 == 1) {
// products found
// Getting Array of Products
vandps = json2.getJSONArray(TAG_VPS);
vpIDs = new ArrayList<>();
// looping through All Products
List<VandP> allVandPs = new ArrayList<>();
for (int i = 0; i < vandps.length(); i++) {
JSONObject c = vandps.getJSONObject(i);
VandP vandp = new VandP(c.getInt(TAG_VPID), c.getInt(TAG_VP_VENDORID), c.getInt(TAG_VP_PRODUCTID));
dataSource2.createVandP(vandp);
allVandPs.add(vandp);
vpIDs.add(vandp.getVpID());
}
dataSource2.deleteVandPTable(vpIDs);
dataSource2.close();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
Intent intent = new Intent(SplashScreen.this, MapsActivity.class);
Intent helpIntent = new Intent(SplashScreen.this, HelpActivity.class);
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUNMapsActivity", true);
if(!isFirstRun) {
startActivity(intent);
finish();
}
else {
startActivity(helpIntent);
finish();
}
}
});
}
}
}
答案 0 :(得分:1)
我只想在这里添加另一个处理程序
Handler mHandler;
...
if(isConnected){
new LoadAllVendors().execute();
mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
//set the second image
}
}, 2000);
}
protected void onDestroy(){
...
// make sure to clean up handlers when activity is destroyed
if (mHandler != null)
mHandler.removeCallbacksAndMessages(null);
}
此外,您无需在runOnUiThread
阻止中执行onPostExecute
。它已经在UI线程上运行,因此您应该将其删除。