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);
}
}
Related
I created Activity that includes ProgressDialog and its methods.
public class ProgressActivity extends AppCompatActivity implements Thread.UncaughtExceptionHandler {
public final ProgressDialog progressDialog;
public void dismissPD() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
public void setPDMessage(String msg) {
progressDialog.setMessage(msg);
}
public void showPD(String msg) {
ProgressDialog.show(this, "", msg);
}
#Override
public void uncaughtException(Thread thread, Throwable ex) {
ex.printStackTrace();
}
public ProgressActivity() {
progressDialog = new ProgressDialog(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
dismissPD();
}
}
Now I extend my new activity from it, but I get NullpointerException inside constructur on progressDialog = new ProgressDialog(this)
As I see, I have a wrong context when constructor runs. Is there a right approach to realize it in my way?
I solved the problem by overriding oncreate mothod of the main Activity:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = new ProgressDialog(this);
}
I have two activities, one is starting another with an Intent. The child activity has a large work to do in onCreate. The first activity is only for loading. When I start child activity, I wait on black screen after it finishes loading.
How to wait on the first activity and switch after to the second when it finishes creating (some listener or callback?)
You have to use AsyncTask.
Try this code
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// call your long task from here
doWork();
}
// write this method in your First Activity
private void doWork()
{
new AsyncTask<Void, Void, String>()
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
#Override
protected String doInBackground(Void... params) {
// do your long task here
// And return your result to the onPostExecute method.
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// after finishing your task dismis your dialog.
progressDialog.dismiss();
// And go to your next Activity.
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);
}
}.execute();
}
}
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?
Edit 1
I need to call a method from BroadcastReceiver and method exist in the Activity class mention below.
I tried this code and got NULL_POINTER_EXCEPTION where I create the reference the MainActivity class.
Correct me what I'm doing wrong ?
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void myTesting(){
Toast.makeText(MainActivity.this, "Welcome to Activity", Toast.LENGTH_SHORT).show();
}
}
BroadcastReceiver.java
public class BootCompeteReceiver extends BroadcastReceiver {
public Context mContext;
private MainActivity mainActivity;
#Override
public void onReceive(Context context, Intent intent) {
mContext = context;
try {
mainActivity = new MainActivity();
mainActivity.myTesting();
} catch (Exception e) {
Toast.makeText(context, ""+e, Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
Do something like that:
public class MainActivity extends Activity {
private BroadcastReceiver receiver = new BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
MainActivity.this.myTesting();
} catch (Exception e) {
Toast.makeText(MainActivity.this, ""+e, Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intFilt = new IntentFilter(Constants.YOUR_BROADCAST_RECEIVER_ACTION);
registerReceiver(receiver, intFilt);
}
public void myTesting(){
Toast.makeText(MainActivity.this, "Welcome to Activity", Toast.LENGTH_SHORT).show();
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
You can startActivity in onReceive, and call myTesting in onCreate of your Activity.
You can try this:
public class MainActivity extends Activity {
private static volatile int INSTANCE_COUNTER = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
INSTANCE_COUNTER++;
IntentFilter intentFilter = new IntentFilter("com.your.package.ACTION");
registerReceiver(mWhateverReceiver, intentFilter);
if (getIntent().getBooleanExtra("fromYourReceiver", false)) {
myTesting();
}
}
private void myTesting() {
// Do something here
}
private BroadcastReceiver mWhateverReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
myTesting();
}
};
public static boolean isInstanceExist() {
return INSTANCE_COUNTER > 0;
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
INSTANCE_COUNTER--;
unregisterReceiver(mWhateverReceiver);
}
}
Your receiver
public class BootCompeteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (MainActivity.isInstanceExist()) {
// There is already one instance of MainActivity, so broadcast this
// event to trigger the receiver inside MainActivity to do your task
Intent broadcastIntent = new Intent("com.your.package.ACTION");
context.sendBroadcast(broadcastIntent);
} else {
// There is no instances of MainActivity exist, so start a new one
// with the action that let the instance know what it should do
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.putExtra("fromYourReceiver", true);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
}
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