问题
First of all, i have to state that i am new to Java in general & Android. I do however have the basics, or at least getting there.
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.
My application is suppose to do the following:
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.
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
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 .
I am not sure what to use there... Below is my code, hopefully someone can point out my mistake.
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<Void, Void, Void> {
@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);
}
}
}
android× 194760
回答1:
There are some correction that you need to make in the doInBackground()
. But before that a small detailing about the AsyncTask
.
AsyncTask
is used when you have any non-UI back ground activity to perform. The function onPreExecute()
is used to show any UI action
(in most cases its showing of a dialog) before you enter the background thread. The function doInBackground()
is used to perform the non-ui action (in most cases fetching data from server). While doing the background activity in doInBackground()
you may wish to show some progress which you do by using publishProgress()
which will internally call the onProgressUpdate()
method. On completion of the background activity in doInBackground()
you return the result
of the activity, if you have any. After you return from the doInBackground()
method internally there is call made to the onPostExecute()
which will receive the result
you have returned in doInBackground()
as a parameter. Now onPostExecute()
will run on a UI thread and most of the UI action like dismissing of dialog which was shown in onPreExecute()
, displaying the result
on some UI component etc. happens in this method.
Now to the mistake you are doing in you code:
You are showing a toast
or a notification
based on the result of your server data fetch using a function notify
but you are still in the background non-ui thread. Now this result should ideally be returned and checked in the onPostExecute()
and based on its value you can show the UI component of toast
or notification
.
I hope this explanation helps you in solving your problem.
EDIT
In your case since you can send the boolean type result variable ok
to onPostExecute()
. For that you need to make the following changes:
in class declaration:
public class asyncTaskUpdateProgress extends AsyncTask<Void, Void, Boolean>
and
protected void onPostExecute(Boolean result){
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);
}
}
and finally in
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");}
return ok;
}
回答2:
You can try this
in your protected void onPreExecute()
need to add
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
//reset progress bar status
progressBarStatus = 0;
then
protected void onProgressUpdate(Void... values) {
progressBarStatus= progressBarStatus + x; // x means any value
progressBar.setProgress(progressBarStatus);
}
after that you need to finish your progressBar in onPostExecute()
. like
protected void onPostExecute(Void result) {
progressBar.dismiss();
}
来源:https://stackoverflow.com/questions/11175272/using-asyntask-to-show-progress-bar-while-attempting-to-ssh-to-server