I have a method that takes lat/lng from mysql and puts the markers on the map.
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
Everything in this way works.
But when i try to put this method with ProgressDialog markers do not appear. After push the button i see only "loading..." and "Done".
This is a way that does not work:
I
pg = new ProgressDialog(this);
pg.setMessage("wait...");
II
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pg.show();
new BackgroundJob().execute();
Toast.makeText(MapsActivity.this, "loading...", Toast.LENGTH_SHORT).show();
}
});
III
private class BackgroundJob extends AsyncTask<Void, Void, Void>
{
#Override
protected Void doInBackground(Void... params) {
myMethod();
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(MapsActivity.this, "Done", Toast.LENGTH_SHORT).show();
pg.cancel();
}
}
what is wrong here?
Related
I'm trying to show one Progress Dialog inside my MainActivity but is impossible:
This class is inside My MainActivity:
class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
ProgressDialog progressDialog;
private String titulo;
private String mensaje;
private MainActivity activity;
public MyAsyncTask(MainActivity activity){
this.activity = activity;
}
#Override
protected Void doInBackground(Void... voids) {
this.activity.createDirs();
this.activity.copyAssets();
return null;
}
#Override
protected void onPreExecute() {
this.progressDialog = new ProgressDialog(MainActivity.this);
this.progressDialog.setTitle("test");
this.progressDialog.setMessage("test message...");
this.progressDialog.show();
Toast.makeText(MainActivity.this, "OPA", Toast.LENGTH_LONG).show();
super.onPreExecute();
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
}
This code is inside My MainActivity.java: Basically when the user press one button I'm creating one reference to my MyAsyncTask and executing it.
btnInit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(flag==1){
myAsyncTask = new MyAsyncTask(new MainActivity());
myAsyncTask.execute();
try {
myAsyncTask.get();
}catch (Exception e){
}
Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
startActivity(intent);
flag=0;
}else{
Toast.makeText(MainActivity.this,"error", Toast.LENGTH_LONG).show();
}
}
The code of the method doInBackground() of the class MyAsyncTask is being executed without problems. I will appreciate any idea guys, thanks so much.
Your thread is blocked, when you call myAsyncTask.get() that's why your Progress Dialog is not displayed so remove the myAsyncTask.get() and add those line on onPostExecute
Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
startActivity(intent);
full code :
btnInit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(flag==1){
myAsyncTask = new MyAsyncTask(new MainActivity());
myAsyncTask.execute();
flag=0;
}else{
Toast.makeText(MainActivity.this,"error", Toast.LENGTH_LONG).show();
}
}
and on your asyncTask :
class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
ProgressDialog progressDialog;
private String titulo;
private String mensaje;
private MainActivity activity;
public MyAsyncTask(MainActivity activity){
this.activity = activity;
}
#Override
protected Void doInBackground(Void... voids) {
this.activity.createDirs();
this.activity.copyAssets();
return null;
}
#Override
protected void onPreExecute() {
this.progressDialog = new ProgressDialog(MainActivity.this);
this.progressDialog.setTitle("test");
this.progressDialog.setMessage("test message...");
this.progressDialog.show();
Toast.makeText(MainActivity.this, "OPA", Toast.LENGTH_LONG).show();
super.onPreExecute();
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
startActivity(intent);
}
}
I have already scanned all of the related questons&answers here, but I still couldn't find the solution.
the service class file:
public class otser extends Service {
private WindowManager windowManager;
private ImageView chatHead;
#Override public IBinder onBind(Intent intent) {
return null;
}
#Override public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
FileObserver observer = new FileObserver(android.os.Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots") {
#Override
public void onEvent(int event, String file) {
event &= FileObserver.ALL_EVENTS;
if(event == FileObserver.CREATE){
Toast.makeText(getApplicationContext(), "screenshot taken",
Toast.LENGTH_LONG).show();
}
}
};
observer.startWatching(); // start the observer
Toast.makeText(getApplicationContext(), "service: OK",
Toast.LENGTH_SHORT).show();
}}
According to other posts, I double checked the path, via creating a file programatically, it does exist, and correct.
The Activity class:
public class Home extends Activity {
Button showChatHead;
Button stopService;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
showChatHead = (Button) findViewById(R.id.gomb);
stopService= (Button) findViewById(R.id.gomb2);
showChatHead.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), ChatHeadService.class);
startService(i);
}
});
stopService.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), ChatHeadService.class);
stopService(i);
}
});
}}
The service class linked well, the last Toast message pops up, as a conclusion only the observer is being skipped.
Any ideas?
Thanks in advance!
newer release:
FileObserver fileObserver = new FileObserver(android.os.Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots") {
#Override
public void onEvent(int event, String path) {
Toast.makeText(getApplicationContext(), "method entered", Toast.LENGTH_SHORT).show();
if (event == FileObserver.CREATE) {
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "File created", Toast.LENGTH_SHORT).show();
}
});
}
}
};
fileObserver.startWatching();
The onEvent method is not entered, the "method entered" Toast didn't appear.
in documentation of the onEvent-Method is mentioned, that it will run at another thread and you should consider this. using a handler, the Toast message should appear, like this:
private Handler handler = new Handler();
#Override
public void onCreate() {
fileObserver = new FileObserver("path") {
#Override
public void onEvent(int event, String path) {
if (event == FileObserver.CREATE) {
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(FileWatcher.this, "File created", Toast.LENGTH_SHORT).show();
}
});
}
}
};
fileObserver.startWatching();
This question already has an answer here:
Using AsyncTask
(1 answer)
Closed 8 years ago.
I am trying to wait for a background process to finish and then do some stuff accordingly after it finishes.
Basically I have a class TwitterActivity and an inner class CheckInternetConnection which extends AsyncTask. I have also a button mSignin and I set the event handling for it. In addition I have also a boolean hasInternet.
My aim is when the mSignin button will be pressed I will call CheckInternetConnection. This is supposed to update my boolean value hasInternet. Then accordingly I expect to do some stuffs.
But I want exactly to do inside onClick() method.
Is there any way how to achieve it? Thanks.
public class TwitterActivity extends Activity
{
private boolean hasInternet = false;
private Button mSignin;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter);
mSignin = (Button)findViewById(R.id.login_id);
mSignin.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
new CheckInternetConnection().execute();
if(hasInternet)
//do some stuff
else
//do some other stuff
}
});
}
class CheckInternetConnection extends AsyncTask<Void, Void, Boolean>{
#Override
protected void onPostExecute(Boolean result){
if(result)
hasInternet = true;
else
hasInternet = false;
}
#Override
protected Boolean doInBackground(Void... params) {
return true;
}
}
}
There are ways how you can wait for an AsyncTask to finish, but I don't think you wanna do that because it will block the UI Thread. Instead, move that check to onPostExecute. You can also already check for a connection when the button gets clicked.
public class TwitterActivity extends Activity
{
private boolean hasInternet = false;
private Button mSignin;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter);
mSignin = (Button)findViewById(R.id.login_id);
mSignin.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
checkInternet();
}
});
}
private void checkInternet(){
if(hasInternet)
//do some stuff
else
new CheckInternetConnection().execute();
}
class CheckInternetConnection extends AsyncTask<Void, Void, Boolean>{
#Override
protected void onPostExecute(Boolean result){
if(result)
hasInternet = true;
else
hasInternet = false;
checkInternet();
}
#Override
protected Boolean doInBackground(Void... params) {
return true;
}
}
}
The purpose of an asynchronous task is to handle an expensive procedure that won't block the main thread. If this is a quick step, don't make it a separate task, simply do it right then and there.
One way to do it would be:
public class TwitterActivity extends Activity {
private boolean hasInternet = false;
private Button mSignin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter);
mSignin = (Button)findViewById(R.id.login_id);
mSignin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new AsyncTask<Void, Void, Boolean>() {
#Override
protected Boolean doInBackground(Void... params) {
// TODO: put your async job here
return true;
}
#Override
protected void onPostExecute(Boolean result){
hasInternet = result;
if( hasInternet ) {
// do some stuff
//
} else {
// do some other stuff
}
}
}.execute();
}
});
}
}
Can I know how to create AsyncTask for Android from all of these? I would like to remove the buttons and let all of these run in the background then redirect to the MainPage.java after clicking Logout.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button btnDelete = (Button)findViewById(R.id.delete);
Button btnSignUp = (Button)findViewById(R.id.btnSignUp);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),HomePage.class);
startActivity(intent);
}
});
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,SignUp.class);
startActivity(intent);
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
cDB.resetTables();
mDB.resetTables();
db.resetTables();
pDB.resetTables();
}
});
}
here is one AsyncTask Example. This will show a peogress dialog while executing the task.
private class LoginProcessing extends AsyncTask<Object, Void, Void> {
private LoginCredentials myLoginCredentials;
private ProgressDialog progressDialog;
public LoginProcessing(LoginCredentials Credentials) {
super();
myLoginCredentials=Credentials;
}
protected void onPreExecute (){
progressDialog = ProgressDialog.show(context, "", "Please Wait...",true);
}
#Override
protected Void doInBackground(Object... arg0) {
// TODO Auto-generated method stub
//Code to do the process in background
return null;
}
protected void onPostExecute(Void result){
progressDialog.dismiss();
//Your code after the process
}
}
You can call this Task as,
new LoginProcessing(loginCredentials).execute();
In this Example loginCredentials is the parameter I am passing to the AsyncTask. You can change it to your own parameter.
This is my program to load a webpage into a web view. My problem is how to add ProgressDialog while loading content from web view.
public class Openpage extends Activity {
WebView wb=null;
String s1=null;
#SuppressLint({ "SetJavaScriptEnabled", "HandlerLeak" })
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsfeed);
Intent i=getIntent();
s1=i.getStringExtra("link");
Toast.makeText(this, s1, Toast.LENGTH_LONG).show();
Log.d(s1, "hai");
ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("Loading...");
new MyTask(progress).execute();
}
public class MyTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress;
public MyTask(ProgressDialog progress) {
this.progress = progress;
}
public void onPreExecute() {
progress.show();
}
public Void doInBackground(Void...voids) {
wb=(WebView)findViewById(R.id.webView1);
wb.loadUrl(s1);
return null;
}
public void onPostExecute(Void void1) {
progress.dismiss();
}
}
You can get to know how much the page has loaded from
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if(progress == 100){
// Page has fully loaded. Cancel the progress dialog here
}
}
});
You can read it from here