Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing AsynTask to show progress bar while attempting to SSH to Server
    primarykey
    data
    text
    <p>First of all, i have to state that i am new to Java in general &amp; Android. I do however have the basics, or at least getting there.</p> <p>The Purpose of my Application: At the company, we have a Remote server that we SSH to, and do some work. At some times, the server is unreachable and therefore disrupts our work.</p> <p>My application is suppose to do the following:</p> <p>1- Using Jsch, i SSH to the server, if there is a response, then, i will attempt again in 15 minutes, if there is no response, i want to notify.</p> <p>i have successfully done the above in non android version of Java, and was able to do it in Android version, however on the main thread, thus i cannot update anything on the UI. In essence the Progress Bar.. In the regular version, the UI freezes, and in the AsyncTask version provided below. i get an exception as soon as i hit the button</p> <p>Below is the code i am using, to be honest, i read all over that the best solution is AsyncTask, but since i am new to that, i am not sure were my wrong is. I honestly assume its may be in the AsyncTask and AsyncTask . </p> <p>I am not sure what to use there... Below is my code, hopefully someone can point out my mistake.</p> <pre><code>package com.example.myapp; import java.io.IOException; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.HandlerThread; import android.os.StrictMode; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.Toast; import android.os.Handler; public class VedasServerMonitorActivity extends Activity { /** Called when the activity is first created. */ Button button; EditText IP; EditText UserName; EditText Password; EditText Port; ProgressBar progressBar1; String UserStr; String PassStr; String IPStr; int PortInt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.button1); IP = (EditText) findViewById(R.id.serverIp); UserName = (EditText) findViewById(R.id.userName); Password = (EditText) findViewById(R.id.password); Port = (EditText) findViewById(R.id.port); progressBar1 = (ProgressBar) findViewById(R.id.progressBar1); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { new asyncTaskUpdateProgress().execute(); } }); } public class asyncTaskUpdateProgress extends AsyncTask&lt;Void, Void, Void&gt; { @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub } @Override protected void onPreExecute() { // TODO Auto-generated method stub progressBar1.setVisibility(View.VISIBLE); UserStr = UserName.getText().toString(); PassStr = Password.getText().toString(); IPStr = IP.getText().toString(); PortInt = Integer.parseInt(Port.getText().toString()); button.setClickable(true); progressBar1.setVisibility(View.INVISIBLE); } @Override protected void onProgressUpdate(Void... values) { // TODO Auto-generated method stub progressBar1.setVisibility(View.INVISIBLE); } @Override protected Void doInBackground(Void... arg0) { boolean ok = false; try { SSHTest sshtest = new SSHTest(); ok = sshtest.sshconnect(UserStr, PassStr, IPStr, PortInt); } catch (Exception e) { e.printStackTrace(); Log.i("ERROR HERE", "doInBackground: IOException");} if (ok) { Toast.makeText(getApplicationContext(), "Connection Susccessfull", Toast.LENGTH_LONG) .show(); } else { Toast.makeText(getApplicationContext(), "Unable to connect", Toast.LENGTH_LONG).show(); notify(getApplicationContext(), true); } return null; } protected void notify(Context context, Boolean on) { NotificationManager nm = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); ComponentName comp = new ComponentName(context.getPackageName(), getClass().getName()); Intent intent = new Intent().setComponent(comp); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); Notification n = new Notification(R.drawable.warning, "Message", System.currentTimeMillis()); n.setLatestEventInfo(context, "Vedas Server Monitor", "Port Un-rechable", pendingIntent); nm.notify(22, n); } } } </code></pre> <p>android× 194760</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload