Show ProgressDialog fails - android

I know that there is a lot of questions about this, but I'm desperated...
I'm trying to show a Dialog while doing an AsyncTask, that must be easy but I'm no able to do it...
I had tried with .execute() and .get() and I had put a for to make the task take a long time, but nothing..
Here is my class:
public class OrdenNuevaActivity extends Activity {
#Override
protected void onCreate(Bundle bundle) {
Log.v("FLUJO", this.getClass().toString());
super.onCreate(bundle);
setContentView(R.layout.activity_orden_nueva);
aceptarOrdenNuevaButton = (Button) findViewById(R.id.aceptarOrdenNuevaButton);
aceptarOrdenNuevaButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoStringConSpinnerDentroDeLaClase tareaCambiarEstadoOrdenEnBDServer = new TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoStringConSpinnerDentroDeLaClase(idOrden, Estados.ACEPTADO,OrdenNuevaActivity.this);
try {
tareaCambiarEstadoOrdenEnBDServer.execute();
} catch (Exception e) {
e.printStackTrace();
}
Toast toast1 = Toast.makeText(getApplicationContext(), "Orden Aceptada", Toast.LENGTH_SHORT);
toast1.show();
NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();
UtilsVictor.addPuebloAlAceptarOrden(context,terminoOrdenAceptadaDetalleTV.getText().toString());
finish();
} else {
AlertDialog alertDialog = new AlertDialog.Builder(OrdenNuevaActivity.this).create();
alertDialog.setTitle("ERROR!");
alertDialog.setMessage("NO hay cobertura, vuelva a intentarlo mas tarde");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.setIcon(R.drawable.common_signin_btn_icon_normal_light);
alertDialog.show();
}
}
});
}
public class TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoStringConSpinnerDentroDeLaClase extends AsyncTask<String,Integer,Boolean>{
private static final String TAG = "AppMovil";
private Integer mOrdenId;
private String mEstadoString;
boolean resul = true;
boolean tareaRealizada = false;
boolean catchPasado = false;
private Context mContext;
private ProgressDialog mPD;
public TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoStringConSpinnerDentroDeLaClase(Integer idO ,String estadoO, Context cntx) {
mContext = cntx;
mOrdenId = idO;
mEstadoString = estadoO;
mPD = new ProgressDialog(mContext);
}
public boolean getResultado(){
return resul;
}
public boolean getTareaRealizada(){
return tareaRealizada;
}
public boolean getCatchPasado(){
return catchPasado;
}
#Override
protected void onPreExecute() {
mPD.setTitle("Please Wait..");
mPD.setMessage("Loading...");
mPD.setCancelable(false);
mPD.show();
}
#Override
protected Boolean doInBackground(String... params) {
for(int i=0; i<999999;i++){
Log.v("A", "A");
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("id", mOrdenId);
jsonObject.put("estado", mEstadoString);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URIS.URI_TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoString);
post.setHeader("content-type", "application/json");
try {
StringEntity stringEntity = new StringEntity(jsonObject.toString());
post.setEntity(stringEntity);
HttpResponse response = httpClient.execute(post);
int responseCode = response.getStatusLine().getStatusCode();
if(responseCode >200 & responseCode < 400) {
Log.v(TAG, "Orden Enviada OK a nuestro Server");
tareaRealizada = true;
resul = true;
} else {
Log.v(TAG, "FALLO al Enviar Orden a nuestro Server");
resul = false;
tareaRealizada = true;
}
} catch (Exception e) {
catchPasado = true;
resul = false;
e.printStackTrace();
}
return resul;
}
#Override
protected void onCancelled() {
if(mPD.isShowing()){
mPD.dismiss();
}
}
#Override
protected void onPostExecute(Boolean result) {
if(mPD.isShowing()){
mPD.dismiss();
}
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
}
}
Where is the mistake?

Related

Context issue with AsyncTask

When clicking a button then I want to show an alertDialog showing an animationDrawable and performing an asyncTask. I found that when I set the context of the dialog to getApplicationContext() then the app crashed , and when I set the context to the parent class activity then the dialog is dismissed very well when I call the dismiss method in the activity class ; but when I want to dismiss it in the onPostExecute of the AsyncTask then the dialog is not dismissed ! Here are codes :
public class SyncActivity extends Activity {
...
private MessageDialogView dlg = null; // an AlertDialog showing a message
private Patienter dwait = null; // an AlertDialog containing an imageview showing an animation-list
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.synchro);
...
dlg = new MessageDialogView(SyncActivity.this, getLayoutInflater());
dwait = new Patienter(SyncActivity.this, getLayoutInflater());
...
}
public void synchroniser(View view) { // called when a button is clicked
dwait.show();
new RequestTask().execute("http://192.168.1.8/impots/data/syncro/webVersAndroid/parcelles.txt");
}
private class RequestTask extends AsyncTask<String, Void, Void> {
private String err = "";
private boolean error = false;
private String[] enregs;
#Override
protected Void doInBackground(String... s_url) {
enregs = new String[s_url.length];
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null) {
if (networkInfo.isAvailable() && networkInfo.isConnected()) {
System.setProperty("http.keepAlive", "false");
HttpURLConnection con = null;
BufferedReader reader = null;
for (int u=0; u<s_url.length; u++) {
String tmp;
String lines = "";
try {
URL url = new URL(s_url[u]);
if (url != null) {
con = (HttpURLConnection) url.openConnection();
if (con != null && con.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream in = con.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
boolean firstLine = true;
while ((tmp = reader.readLine()) != null) {
if (firstLine) {
firstLine = false;
lines += tmp;
}
else
lines += "\r\n" + tmp;
}
enregs[u] = lines;
}
}
} catch (MalformedURLException e) {
error = true;
err = getResources().getString(R.string.errBadUrl);
} catch (IOException e) {
error = true;
err = getResources().getString(R.string.errAccessError);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
if (con != null)
con.disconnect();
}
}
}
else {
error = true;
err = getResources().getString(R.string.errNotConnected);
}
} else {
error = true;
err = getResources().getString(R.string.errNoNetwork);
}
if (!error)
populateDB();
return null;
}
#Override
protected void onPostExecute(Void result) {
dwait.dismiss();
if (error) {
displayError(err);
}
}
private void displayError(String msg) {
dlg.setTitre(getString(R.string.titreErrMsgBox));
dlg.setMsg(msg);
dlg.show();
}
...
}
}
}
Code of the dialog :
public class Patienter extends AlertDialog {
private View contenu;
AnimationDrawable frameAnimation = null;
public Patienter(Context context, LayoutInflater inflater) {
super(context);
contenu = inflater.inflate(R.layout.patienter, null);
setCancelable(false);
setCanceledOnTouchOutside(false);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(contenu);
ImageView img = (ImageView)contenu.findViewById(R.id.imgWait);
img.setBackgroundResource(R.drawable.wait);
frameAnimation = (AnimationDrawable) img.getBackground();
frameAnimation.start();
}
}
So how to work with the context ?
Set your Context in Constructor of AsyncTask & pass the context from where AsyncTask called.
Show your Dialogue in onPreExecute() of AsyncTask
Cancel your Dialogue on onPostExecute().
private class RunInBackground extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
private Context contextInsideDialogue;
public RunInBackground(Context activity) {
try {
this.contextInsideDialogue = activity;
dialog = new ProgressDialog(activity,
android.R.style.Theme_DeviceDefault_Light_Panel);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.getWindow().setGravity(Gravity.BOTTOM);
} catch (Exception e) {
}
}
#Override
protected void onPreExecute() {
try {
dialog.setMessage("Please wait. . ");
dialog.show();
} catch (Exception e) {
}
}
#Override
protected void onPostExecute(Void result) {
try {
if (dialog.isShowing()) {
dialog.dismiss();
}
} catch (Exception e) {
}
}
}
& Invoke it as
RunInBackground task = new RunInBackground (MyActivity.this);
task.execute();

How to perform AsyncTask for checking internet connection

I have the following code, and what I'm trying to do is to programmatically check if there is an internet connection or not.
Since I'm getting a NetworkOnMainThreadException, and have been advised to use AsyncTask.
I want to perform network operation on hasActiveInternetConnection(Context context)and return true if connected to a network, else false .
How do I do this using AsyncTask?
public class NetworkUtil extends AsyncTask<String, Void, String>{
Context context;
public NetworkUtil(Context context){
this.context = context;
}
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... arg0) {
if (new CheckNetwork(context).isNetworkAvailable())
{
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
boolean url= (urlc.getResponseCode() == 200);
String str = String.valueOf(url);
return str;
} catch (IOException e) {
}
}
// your get/post related code..like HttpPost = new HttpPost(url);
else {
Toast.makeText(context, "no internet!", Toast.LENGTH_SHORT).show();
}
return null;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
use this class for checking internet connectivity...
public class CheckNetwork {
private Context context;
public CheckNetwork(Context context) {
this.context = context;
}
public boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
then.....
use the this ASynTask for httppost.
public class NetworkUtil extends AsyncTask<String, Void, String> {
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(YourActivity.this);
dialog.setMessage("Loading...");
dialog.setCancelable(false);
dialog.show();
super.onPreExecute();
}
#Override
protected String doInBackground(String... arg0) {
if (new CheckNetwork(YourActivity.this).isNetworkAvailable()) {
// your get/post related code..like HttpPost = new HttpPost(url);
} else {
// No Internet
// Toast.makeText(YourActivity.this, "no internet!", Toast.LENGTH_SHORT).show();
}
return null;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
There is no way to get an Internet connexion state, you will always have the network connection state.
But I found a pretty nice answer here: you send a request to google.com !! :) you can also try to ping to google by unix commandes if you want !!
here the guy is using a thread , and waiting a little bit for the answer, then returning a boolean . you do your staff in the handler . this is his code :
public static void isNetworkAvailable(final Handler handler, final int timeout) {
// ask fo message '0' (not connected) or '1' (connected) on 'handler'
// the answer must be send before before within the 'timeout' (in milliseconds)
new Thread() {
private boolean responded = false;
#Override
public void run() {
// set 'responded' to TRUE if is able to connect with google mobile (responds fast)
new Thread() {
#Override
public void run() {
HttpGet requestForTest = new HttpGet("http://m.google.com");
try {
new DefaultHttpClient().execute(requestForTest); // can last...
responded = true;
}
catch (Exception e) {
}
}
}.start();
try {
int waited = 0;
while(!responded && (waited < timeout)) {
sleep(100);
if(!responded ) {
waited += 100;
}
}
}
catch(InterruptedException e) {} // do nothing
finally {
if (!responded) { handler.sendEmptyMessage(0); }
else { handler.sendEmptyMessage(1); }
}
}
}.start();
}
Then, I define the handler:
Handler h = new Handler() {
#Override
public void handleMessage(Message msg) {
if (msg.what != 1) { // code if not connected
} else { // code if connected
}
}
};
...and launch the test:
isNetworkAvailable(h,2000); // get the answser within 2000 ms
Your AsyncTask should look like this:
private class NetworkUtilTask extends AsyncTask<Void, Void, Boolean>{
Context context;
public NetworkUtilTask(Context context){
this.context = context;
}
protected Boolean doInBackground(Void... params) {
return hasActiveInternetConnection(this.context);
}
protected void onPostExecute(Boolean hasActiveConnection) {
Log.d(LOG_TAG,"Success=" + hasActiveConnection);
}
}
You would then execute it like the following:
NetworkUtilTask netTask = new NetworkUtilTask(context);
netTask.execute();

Memory leak — multiple instances of activity

I've got a couple of activities.
The app is running out of memory. I tried to gc.clean() in all activities (in onDestroy), null references, launchMode="singleTop" but without any success.
In MAT I've found out that I have multiple objects of the same activity and it's a default behaviour, if I'm not mistaken.
For example, at first launch I will have the main activity, if I quit and launch again, I will have 2 instances.
This makes the VM grow until the force close dialog is shown.
Example of my code:
public class Splash extends Activity
{
private static Handler handler;
private static Thread splashThread;
private MyProgressDialog dialog;
private Netroads netroads;
private int debug;
private AsyncTask<Void,Void,Void> mConnectionTask;
private boolean checkInternetConnection = false;
private SharedPreferences ref;
private SharedPreferences.Editor ed;
private boolean firstOperationKey = false;
private boolean userPay = false;
private String macAddress = "";
private String answer = "";
private String extra = "";
private int status = Consts.STATUS_SERVER_BUSY;
private String paymentType = "";
private boolean paymentStatus = false;
private String versionName = "";
private RelativeLayout splash_layout;
private RelativeLayout welcome_message_layout;
private TextView welcome_message_text;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash_layout);
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
netroads = ((Netroads)getApplicationContext());
ref = getSharedPreferences("NETROADS",MODE_PRIVATE);
ed = ref.edit();
//ed.putString("userID","2");
//ed.putBoolean("LOGOUT",false);
//ed.commit();
//change background by LANGUAGEMODENUMBER
splash_layout = (RelativeLayout)findViewById(R.id.splash_layout);
welcome_message_layout = (RelativeLayout)findViewById(R.id.welcome_message_layout);
//welcome_message_layout = new RelativeLayout(getApplicationContext());
welcome_message_text = (TextView)findViewById(R.id.welcome_message_text);
//welcome_message_text = new TextView(getApplicationContext().);
if(ref.getBoolean("LANGUAGEMODECHANGE",false))
{
welcome_message_layout.setBackgroundResource(R.drawable.welcome_message);
welcome_message_text.setText(getString(R.string.welcome));
switch(ref.getInt("LANGUAGEMODENUMBER",Consts.DEFAULT))
{
case Consts.HEBREW:
splash_layout.setBackgroundResource(R.drawable.welcome_hebrew);
break;
case Consts.ENGLISH:
splash_layout.setBackgroundResource(R.drawable.welcome_english);
break;
case Consts.RUSSIAN:
splash_layout.setBackgroundResource(R.drawable.welcome_russian);
break;
case Consts.ARABIC:
splash_layout.setBackgroundResource(R.drawable.welcome_arabic);
break;
default:
splash_layout.setBackgroundResource(R.drawable.bg);
break;
}//close switch
}
if(ref.getBoolean("LANGUAGEMODECHANGE",false))
{
ed.putBoolean("LANGUAGEMODECHANGE",false);
ed.commit();
}
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifiManager.isWifiEnabled())
{
WifiInfo info = wifiManager.getConnectionInfo();
macAddress = info.getMacAddress();
}
else
{
wifiManager.setWifiEnabled(true);
WifiInfo info = wifiManager.getConnectionInfo();
macAddress = info.getMacAddress();
wifiManager.setWifiEnabled(false);
}
PackageInfo pinfo;
try {
pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
versionName = pinfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
handler = new Handler();
splashThread = (Thread) getLastNonConfigurationInstance();
if (splashThread != null && splashThread.isAlive())
{
dialog = MyProgressDialog.show(this,"","");
}
}//close onCreate
public void onResume()
{
super.onResume();
debug = 0;
if(debug == 1)
{
Intent intent = new Intent(Splash.this, Main.class);
startActivity(intent);
Splash.this.finish();
}
else
{
dialog = MyProgressDialog.show(this,"","");
if(netroads.checkGPS(Splash.this))
{
mConnectionTask = new AsyncTask<Void, Void, Void>()
{
#Override
protected Void doInBackground(Void... params)
{
for(int i=0;i<3;i++)
{
HttpGet requestForTest = new HttpGet("http://m.google.com");
try
{
new DefaultHttpClient().execute(requestForTest);
checkInternetConnection = true;
}
catch (Exception e)
{
checkInternetConnection = false;
}
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
dialog.dismiss();
mConnectionTask = null;
if(checkInternetConnection)
{
dialog = MyProgressDialog.show(Splash.this,"","");
startSplashThread();
}
else
{
netroads.showRegularDialog(getString(R.string.connection_problem), getString(R.string.internet_connection_problem), getString(R.string.ok), Splash.this);
}
}
};
mConnectionTask.execute(null, null, null);
}
else
{
dialog.dismiss();
netroads.showGPSAlert(Splash.this);
}
}
}//close onResume
public void startSplashThread()
{
splashThread = new splashThread();
splashThread.start();
}//close splashThread
// Save the thread
#Override
public Object onRetainNonConfigurationInstance() {
return splashThread;
}
#Override
protected void onDestroy()
{
super.onDestroy();
if (dialog != null)
{
dialog.dismiss();
dialog = null;
}
netroads.unbindDrawables(findViewById(R.id.splash_layout));
System.gc();
}//close onDestroy
public class splashThread extends Thread
{
#Override
public void run()
{
try
{
if(ConnectionDetector.checkInternetConnection())
{
if(ref.getString("userID","").equalsIgnoreCase("") || ref.getBoolean("LOGOUT",false))
firstOperationKey = true; //first operation after download from google play
else
{
checkUserActive();
netroads.getUsersCount(Splash.this);
}
handler.post(new splashRunnable());
}
}
finally
{
splashThread.interrupt();
}
}
}//close splashThread
public class splashRunnable implements Runnable
{
public void run()
{
dialog.dismiss();
if(firstOperationKey)
{
Intent intent = new Intent(Splash.this,AppDescription.class);
startActivity(intent);
finish();
}
else if(status == Consts.STATUS_OPERATION_SUCCESSFUL)
{
if(netroads.answerFromServerIsOK(answer))
{
Intent intent = new Intent(Splash.this,Main.class);
startActivity(intent);
finish();
}
else
{
Log.e(Consts.TAG,"STATUS = OK, ANSWER = -2 (CLEAR SESSION)");
clearSession();
}
}
else if(status == Consts.STATUS_USER_UNEXISTS)
{
clearSession();
}
else
{
netroads.answerFromServerIsOK(answer);
netroads.serverException(Splash.this,status);
}
}
}//close splashRunnable
public void clearSession()
{
ed.putBoolean("LOGOUT",true);
ed.commit();
startSplashThread();
}//close clearSession
public boolean checkUserActive()
{
Log.e(Consts.TAG,"checkUserActive()");
String url = new String(Consts.SERVER + "checkUserActive");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userID",ref.getString("userID","")));
nameValuePairs.add(new BasicNameValuePair("MAC",macAddress));
httppost.addHeader("appPassword",Consts.APP_PASSWORD);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity httpEntity = response.getEntity();
String xmlString = EntityUtils.toString(httpEntity);
Log.d(Consts.TAG,xmlString);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(xmlString));
Document doc = db.parse(inStream);
NodeList nodeList;
boolean normalize = false;
try
{
doc.getDocumentElement().normalize();
nodeList = doc.getElementsByTagName("Client");
normalize = true;
}
catch(Exception e)
{
nodeList = null;
}
if(normalize)
if(nodeList.getLength() > 0)
{
for(int i=0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
Element firstElement = (Element) node;
try{
NodeList myNodeList = firstElement.getElementsByTagName("userID");
Element myElement = (Element)myNodeList.item(0);
myNodeList = myElement.getChildNodes();
Log.d(Consts.TAG,"checkUserActive - userID:"+myNodeList.item(0).getNodeValue());
myNodeList = firstElement.getElementsByTagName("status");
myElement = (Element)myNodeList.item(0);
myNodeList = myElement.getChildNodes();
status = Integer.parseInt(myNodeList.item(0).getNodeValue());
myNodeList = firstElement.getElementsByTagName("answer");
myElement = (Element)myNodeList.item(0);
myNodeList = myElement.getChildNodes();
answer = myNodeList.item(0).getNodeValue();
myNodeList = firstElement.getElementsByTagName("extra");
myElement = (Element)myNodeList.item(0);
myNodeList = myElement.getChildNodes();
extra = myNodeList.item(0).getNodeValue();
Netroads.paymentKey = extra;
}catch(Exception e){e.printStackTrace();}
}//close for
return true;
}//close if
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return false;
}//close checkUserActive
#Override
protected void onActivityResult(int requestCode,int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//if user is google play, paypal,...
if(resultCode == Consts.PAYMENT_STATUS_IAP)
{
Log.e(Consts.TAG,"onActivityResult() - Splash");
try
{
paymentStatus = data.getExtras().getBoolean("paymentStatus");
}
catch(Exception e){paymentStatus = false;}
if(paymentStatus)
{
paymentType = data.getExtras().getString("paymentType");
netroads.startSetUser(Splash.this,paymentType,"1","2",macAddress,versionName,ref.getString("userID",""));//TODO Chane to auto function
}
else
{
netroads.showRegularDialog(getString(R.string.netroads_server),getString(R.string.user_cancel_purchase),getString(R.string.ok),Splash.this);
}
}
}//close onActivityResult
}//close Splash
Where is the leak?
EDIT:
Here are the photos:
Could it be ralated to WifiManager that is not releasing a Context reference?
http://code.google.com/p/android/issues/detail?id=43945
I think you may be looking at this the wrong way.
What is the crash exactly? Does it lead to one of the splash_layout.setBackgroundResource(...) calls?
If yes, it is possible that your images are way too big (how much to they weight?).
If that is the case you should consider resizing the images and maybe look at http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

how to check internet connection in asynctask in android

#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.videoview);
mVideoView = (VideoView) findViewById(R.id.videoView1);
scanCode = getIntent().getExtras().getString("ScanCode");
new GetVideoUrlAsyn().execute();
mVideoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
startActivity(new Intent(getBaseContext(),
PostVideoMenuActivity.class));
}
});
}
private class GetVideoUrlAsyn extends AsyncTask<Void, Void,Void> {
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
String URL = "http://www.storybehindthestore.com/sbtsws/service/saveinfo.asmx/StoryScanHistorySave";
WebServiceCall webservice = new WebServiceCall();
nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("iSiteID","1"));
nameValuePairs.add(new BasicNameValuePair("sVideoURL",scanCode));
Log.e("scancode",""+ scanCode);
nameValuePairs.add(new BasicNameValuePair("sDeviceID",SplashActivity.deviceId));
Log.e("sDeviceID",""+ SplashActivity.deviceId);
nameValuePairs.add(new BasicNameValuePair("sDeviceModel",SplashActivity.deviceModelName));
Log.e("sDeviceModel",""+ SplashActivity.deviceModelName);
responce = webservice.callhttppost_numvaluepair(URL, nameValuePairs);
// Log.e("Stiryscanstorysave responce", responce);
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//progressDialog.dismiss();
if (responce.contains("InCorrect QR Code !")) {
AlertDialog adddrug1 = new AlertDialog.Builder(
VideoActivity.this)
.setTitle("Message")
.setMessage("InCorrect QR Code !")
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
startActivity(new Intent(getBaseContext(),
HomeActivity.class));
}
}).create();
adddrug1.show();
}else {
StoryScanSaveListGet();
mVideoView.setVideoURI(Uri.parse(scanCode));
mVideoView.setMediaController(new MediaController(VideoActivity.this));
mVideoView.requestFocus();
mVideoView.start();
}
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
/*progressDialog = new ProgressDialog(VideoActivity.this);
progressDialog.setCancelable(false);
progressDialog.show(); */
}
public void StoryScanSaveListGet() {
// TODO Auto-generated method stub
id.clear();
site_id.clear();
store_name.clear();
store_url.clear();
store_phone.clear();
video_count.clear();
try {
JSONObject jmain = new JSONObject(responce);
JSONArray jarray = jmain.getJSONArray("tblCustomer");
JSONObject j_one = (JSONObject) jarray.get(0);
Log.v("Responce", responce);
for (int i = 0; i < jarray.length(); i++) {
j_one = (JSONObject) jarray.get(i);
id.add(j_one.get("id").toString());
site_id.add(j_one.get("site_id").toString());
store_name.add(j_one.get("store_name").toString());
store_url.add(j_one.get("store_url").toString());
store_phone.add(j_one.get("store_phone").toString());
video_count.add(j_one.get("video_count").toString());
}
storeurl =j_one.get("store_url").toString();
storephone = j_one.get("store_phone").toString();
videocount = j_one.get("video_count").toString();
storename = j_one.get("store_name").toString();
Log.i("id", ""+ id);
Log.i("site_id", ""+ site_id);
Log.i("store_name", ""+ store_name);
Log.i("store_url", ""+ store_url);
Log.i("store_phone", ""+ store_phone);
Log.i("video_count", ""+ video_count);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
responce = null;
}
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
Log.d("tag", "onPause called");
super.onPause();
stopPosition = mVideoView.getCurrentPosition(); // stopPosition is an
// int
mVideoView.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d("TAG", "onResume called");
mVideoView.seekTo(stopPosition);
mVideoView.start(); //
}
hii i want to open alert if internet is not available and also check if response getting time out..how is it posible??? help me..here is my code of asynctask which is new GetVideoUrlAsyn().execute(); method..
Check network connectivity before executing AsyncTask,
if(isNetworkAvailable(this))
{
new GetVideoUrlAsyn().execute();
}
and this is the method definition,
public boolean isNetworkAvailable(Context ctx)
{
ConnectivityManager cm = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()&& cm.getActiveNetworkInfo().isAvailable()&& cm.getActiveNetworkInfo().isConnected())
{
return true;
}
else
{
return false;
}
}
do something like this inside your method callhttppost_numvaluepair(URL, nameValuePairs) of WebServiceCall class,
HttpParams basicparams = new BasicHttpParams();
URI uri = new URI(url);
HttpPost method = new HttpPost(uri);
int timeoutConnection = 60000;//set your timeout period
HttpConnectionParams.setConnectionTimeout(basicparams,
timeoutConnection);
DefaultHttpClient client = new DefaultHttpClient(basicparams);
//and then rest of your code
Note:
Don't forget to put the permission in manifest file,
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
You should use a broadcast receiver as mentioned in other SO answers, but since you have asked how to do this otherwise, here you go:
* Checks for an existing network connectivity
*
* #param context
* The {#link Context} which is needed to tap
* {#link Context#CONNECTIVITY_SERVICE}
* #return True if network connection is available
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
Will need additional permission to be specified in the manifest file
check internet before execute asyncTask
if(checkNetwork)
{
new GetVideoUrlAsyn().execute();
}
else
{
// do stuff
}
if(CheckNetwork){
new GetVideoUrlAsyn().execute();
}else{
// Check internet connection
}
and
private boolean CheckNetwork() {
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}

android textview does not show after setting visible from another thread

I am having problems making my TextViews visible from another thread using the method showDisclaimer(). I have used runOnUiThread() to set the visibility of my TextViews. Basically, I want to show these views after importing the csv to the database. Can you take a look and see what I missed?
public class MainActivity extends Activity {
final static int INDEX_ACCTTYPE = 0;
final static int INDEX_ECN = 1;
final static int INDEX_TLN = 2;
final static int INDEX_SIN = 3;
final static int INDEX_MOBILE = 4;
final static int INDEX_CITY = 5;
final static int INDEX_START_DATE = 6;
final static int INDEX_START_TIME = 7;
final static int INDEX_END_DATE = 8;
final static int INDEX_END_TIME = 9;
final static int INDEX_REASON = 10;
final static int INDEX_DETAILS = 11;
DatabaseHandler db;
String str;
ProgressDialog pd;
final private String csvFile = "http://www.meralco.com.ph/pdf/pms/pms_test.csv";
final private String uploadDateFile = "http://www.meralco.com.ph/pdf/pms/UploadDate_test.txt";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView homeText1 = (TextView) findViewById(R.id.home_text1);
TextView homeText2 = (TextView) findViewById(R.id.home_text2);
TextView homeText3 = (TextView) findViewById(R.id.home_text3);
TextView homeText4 = (TextView) findViewById(R.id.home_text4);
homeText1.setVisibility(View.INVISIBLE);
homeText2.setVisibility(View.INVISIBLE);
homeText3.setVisibility(View.INVISIBLE);
homeText4.setVisibility(View.INVISIBLE);
//db = new DatabaseHandler(MainActivity.this);
if(dbExists()){
db = new DatabaseHandler(MainActivity.this);
Log.d("Count", "" + db.count());
if(!uploadDateEqualsDateInFile())
promptOptionalUpdate("There is a new schedule");
showDisclaimer();
Log.i("oncreate", "finished!");
return;
}
promptRequiredUpdate("Schedule not updated");
//showDisclaimer();
Log.i("oncreate", "finished!");
}
public void promptOptionalUpdate(String Message) {
AlertDialog.Builder builder = new AlertDialog.Builder(
this);
builder.setMessage(Message)
.setCancelable(false)
.setPositiveButton("Update Now",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("SHOW ALERT -->!", "update");
dropOldSchedule();
triggerDownload();
dialog.cancel();
}
})
.setNegativeButton("Later",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("SHOW ALERT -->!", "cancel");
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void dropOldSchedule(){
//TODO drop old schedule
}
public void triggerDownload() {
if (!checkInternet()) {
showAlert("An internet connection is required to perform an update, please check that you are connected to the internet");
return;
}
if(pd!=null && pd.isShowing()) pd.dismiss();
pd = ProgressDialog.show(this, "Downloading schedule",
"This may take a few minutes...", true, false);
Thread thread = new Thread(new Runnable() {
public void run() {
db = new DatabaseHandler(MainActivity.this);
db.beginTransaction();
try {
URL myURL = new URL(csvFile);
BufferedReader so = new BufferedReader(new InputStreamReader(myURL.openStream()));
while (true) {
String output = so.readLine();
if (output != null) {
String[] sched = output.split(",");
try {
db.addRow(sched[INDEX_SIN], sched[INDEX_CITY],
sched[INDEX_START_DATE], sched[INDEX_START_TIME],
sched[INDEX_END_DATE], sched[INDEX_END_TIME],
sched[INDEX_DETAILS], sched[INDEX_REASON]);
} catch (IndexOutOfBoundsException e) {
db.addRow(sched[INDEX_SIN], sched[INDEX_CITY],
sched[INDEX_START_DATE], sched[INDEX_START_TIME],
sched[INDEX_END_DATE], sched[INDEX_END_TIME],
"", sched[INDEX_REASON]);
e.printStackTrace();
}
}
else {
break;
}
}
so.close();
} catch (MalformedURLException e) {
e.printStackTrace();
db.endTransaction();
} catch (IOException e) {
e.printStackTrace();
db.endTransaction();
}
Log.d("Count", ""+db.count());
db.setTransactionSuccessful();
db.endTransaction();
runOnUiThread(new Runnable() {
public void run() {
while (!pd.isShowing());
getUploadDate();
writeUploadDateInTextFile();
showDisclaimer();
pd.dismiss();
}
});
}
});
thread.start();
//while(thread.isAlive());
Log.d("triggerDownload", "thread died, finished dl. showing disclaimer...");
}
public void getUploadDate() {
Log.d("getUploadDate", "getting upload date of schedule");
if(pd!=null && pd.isShowing()) pd.dismiss();
pd = ProgressDialog.show(this, "Getting upload date",
"This may take a few minutes...", true, false);
Thread thread = new Thread(new Runnable() {
public void run() {
try {
URL myURL = new URL(uploadDateFile);
BufferedReader so = new BufferedReader(new InputStreamReader(myURL.openStream()));
while (true) {
String output = so.readLine();
if (output != null) {
str = output;
}
else {
break;
}
}
so.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
public void run() {
while (!pd.isShowing());
pd.dismiss();
}
});
}
});
thread.start();
while (thread.isAlive());
Log.d("getUploadDate","thread died, upload date="+str);
}
public void writeUploadDateInTextFile() {
Log.d("writeUploadDateTextFile", "writing:"+str);
try {
OutputStreamWriter out = new OutputStreamWriter(openFileOutput(
"update.txt", 0));
out.write(str);
out.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
public void showDisclaimer() {
Log.d("ShowDisclaimer", "showing disclaimer");
TextView homeText1x = (TextView) findViewById(R.id.home_text1);
TextView homeText2x = (TextView) findViewById(R.id.home_text2);
TextView homeText3x = (TextView) findViewById(R.id.home_text3);
TextView homeText4x = (TextView) findViewById(R.id.home_text4);
homeText3x
.setText("You may view the schedule of pre-arranged power interruptions by clicking any of these buttons : SIN, City or Date. " +
"The schedule has been updated as of " + str
+ ". Meralco is exerting all efforts to restore electric service as scheduled." +
" The schedule, however, may change without further notice. For verification, follow-ups, or " +
"latest updates, please contact our CALL CENTER through telephone nos. 16211, " +
"fax nos. 1622-8554/1622-8556 or email address callcenter.tech.assist#meralco.com.ph.");
homeText1x.setVisibility(View.VISIBLE);
homeText2x.setVisibility(View.VISIBLE);
homeText3x.setVisibility(View.VISIBLE);
homeText4x.setVisibility(View.VISIBLE);
Log.d("ShowDisclaimer", "finished showing disclaimer");
}
public void promptRequiredUpdate(String Message) {
Log.d("required update","required!");
AlertDialog.Builder builder = new AlertDialog.Builder(
this);
builder.setMessage(Message)
.setCancelable(false)
.setPositiveButton("Update Now",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("SHOW ALERT -->!", "update");
triggerDownload();
dialog.cancel();
}
})
.setNegativeButton("Later",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("SHOW ALERT -->!", "cancel");
dialog.cancel();
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public boolean uploadDateEqualsDateInFile() {
Log.d("uploadDateEqualsDateInFile","comparing schedule upload dates");
getUploadDate();
try {
String recordedDate = "";
InputStream instream = openFileInput("update.txt");
if (instream != null) { // if file the available for reading
Log.d("uploadDateEqualsDateInFile","update.txt found!");
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line = null;
while ((line = buffreader.readLine()) != null) {
recordedDate = line;
Log.d("uploadDateEqualsDateInFile","recorded:"+recordedDate);
}
Log.d("uploadDateEqualsDateInFile","last upload date: " + str + ", recorded:" +recordedDate);
if(str.equals(recordedDate)) return true;
return false;
}
Log.d("uploadDateEqualsDateInFile","update.txt is null!");
return false;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public void showAlert(String Message) {
AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext());
builder.setMessage(Message).setCancelable(false)
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public boolean checkInternet() {
ConnectivityManager cm = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo infos[] = cm.getAllNetworkInfo();
for (NetworkInfo info : infos)
if (info.getState() == NetworkInfo.State.CONNECTED
|| info.getState() == NetworkInfo.State.CONNECTING) {
return true;
}
return false;
}
public boolean dbExists() {
File database=getApplicationContext().getDatabasePath(DatabaseHandler.DATABASE_NAME);
if (!database.exists()) {
Log.i("Database", "Not Found");
return false;
}
Log.i("Database", "Found");
return true;
}
#Override
protected void onDestroy() {
super.onDestroy();
if (db != null) {
db.close();
}
}
#Override
protected void onPause() {
super.onPause();
if (db != null) {
db.close();
}
}
}
simplymoody, do what Chirag Raval suggested by declaring private static TextView homeText1, globally and in your onCreate initialise them. However, you should always update the UI from the main thread. But showDisclaimer() is still running on the background thread, so you could run the showDisclaimer() a couple of different ways. One would be to do what you have done in getUploadDate():
runOnUiThread(new Runnable() {
public void run() {
showDisclaimer()
}
});
Or you could use a handler:
private Handler showDisclaimerHandler = new Handler(new Handler.Callback() {
#Override
public boolean handleMessage(Message msg) {
homeText3x.setText("TEXT");
homeText1x.setVisibility(View.VISIBLE);
homeText2x.setVisibility(View.VISIBLE);
homeText3x.setVisibility(View.VISIBLE);
homeText4x.setVisibility(View.VISIBLE);
Log.d("ShowDisclaimer", "finished showing disclaimer");
return false;
}
});
And everywhere in a background thread you wish to run your showDisclaimer(), instead you run:
showDisclaimerHandler.sendEmptyMessage(0);

Categories

Resources