From main
activity i call this background process. here in this background there is onprexecute
method there is an if else
condition in else
part i need to add a Snackbar
public class background extends AsyncTask<String,Void,String> {
private ProgressDialog dialog;
private ProgressDialog progressDialog;
private ConnectivityManager cm;
private String jsonurl, jsonstring;
public static String listRequest;
private mobile_form mform;
private Context ctx;
ProgressBar progressbar;
background (Context ctx){
this.ctx = ctx;
cm = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
dialog = new ProgressDialog(ctx);
progressbar = new ProgressBar(ctx);
progressbar.getIndeterminateDrawable().setColorFilter(Color.parseColor("#039BE5"), android.graphics.PorterDuff.Mode.SRC_IN);
mform = new mobile_form();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
jsonurl = "https://crackstrickblog.000webhostapp.com/json_get_data.php";
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected) {
dialog.setCancelable(false);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();
dialog.setContentView(progressbar);
}
else {
// here i need to add snackbar like this
//Snackbar.make(this.findViewById(android.R.id.content), "Message", Snackbar.LENGTH_LONG).show();
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected) {
ctx.startActivity(new Intent(ctx, mobile_form.class));
if (dialog.isShowing())
dialog.dismiss();
}
}
@Override
protected String doInBackground(String... voids) {
return null;
}
}
This Snackbar.make(this.findViewById(android.R.id.content), "Message", Snackbar.LENGTH_LONG).show();
will not work. findViewById
is a method of activity class not of AsyncTask
.
Use interface as a callback.
interface Callback {
public void showSnackBar();
}
In AsyncTask
private Callback callback;
Then
public background (Context ctx){
callback =(Callback) ctx;
Then in onPreExecute
else {
if(callback!=null)
callback.showSnackbar();
}
In activity class implement the interface and the method
public YourActivity extends AppCompatActivity implements Callback {
Then
@Override
public void showSnackBar()
{
// show snack bar in activity
}
You could also use some event bus mechanism instead of the above.
A code snippet to display a basic SnackBar is shown below:
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "www.journaldev.com", Snackbar.LENGTH_LONG);
snackbar.show();
In the above snippet make() method accepts three parameters:
coordinatorLayout : It is the root layout of the activity
www.journaldev.com : This is the message to be appear on snackbar, and we can customise it with our own message
Snackbar.LENGH_LONG : This is last parameter which is the time limit how long snackbar to be displayed
show() method is used to display the SnackBar on the screen.
Does this help you?
您没有像通过异步类那样传递上下文,而是使用您的活动在该上下文中查找视图,您无权访问活动视图。
Snackbar.make(MainActivity.this.findViewById(android.R.id.content), "Message", Snackbar.LENGTH_LONG).show();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.