我已经展示了android小吃店无限长度但如何关闭小吃店没有任何行动或持续时间,而互联网连接。我必须检查互联网连接或not.after互联网连接的小吃店将自动关闭而不采取任何行动或duration.If任何人知道好心帮助我。
以下是我的代码
public static void snack (HashMap actions,int priority,String message,Activity context){
Snackbar B = Snackbar.make(context.findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
if(actions!=null){
Iterator iterator = actions.entrySet().iterator();
B.setDuration(Snackbar.LENGTH_INDEFINITE);
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry)iterator.next();
B.setAction((String)pair.getKey(),(View.OnClickListener)pair.getValue());
iterator.remove(); // avoids a ConcurrentModificationException
}}
switch (priority)
{
case 0:
B.getView().setBackgroundColor(context.getResources().getColor(R.color.color_pinkbutton));
break;
case 1:
B.getView().setBackgroundColor(Color.parseColor("#66ccff"));
break;
case 2:
B.getView().setBackgroundColor(Color.parseColor("#66ff33"));
break;
}
B.show();在调用上面提到的方法之后,使用activity的方法如下所示
If (NetworkCheck.isNetworkAvailable(this) == false) {
MyApplication.snack(null, 0, "Network Connection failed.",class.this);
else发布于 2016-09-07 23:53:42
我创建了这个单例实用类。它保持了应用程序类的整洁,并为小吃店未来的可维护性做了最好的准备。
public class SnackBarUtils {
private static SnackBarUtils mInstance = null;
private Snackbar mSnackBar;
private SnackBarUtils() {
}
public static SnackBarUtils getInstance() {
if (mInstance == null) {
mInstance = new SnackBarUtils();
}
return mInstance;
}
public void hideSnackBar() {
if (mSnackBar != null) {
mSnackBar.dismiss();
}
}
public void showProblemSnackBar(final Activity activity, final String message) {
mSnackBar = Snackbar.make(activity.findViewById(android.R.id.content), message, Snackbar.LENGTH_INDEFINITE);
// Changing action button text color
View sbView = mSnackBar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
mSnackBar.show();
}
}发布于 2016-01-31 14:51:02
您可以按如下方式在代码中进行更改:
Snackbar B; //make it as global
public static void snack (HashMap actions,int priority,String message,Activity context){
B = Snackbar.make(context.findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
if(actions!=null){
Iterator iterator = actions.entrySet().iterator();
B.setDuration(Snackbar.LENGTH_INDEFINITE);
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry)iterator.next();
B.setAction((String)pair.getKey(),(View.OnClickListener)pair.getValue());
iterator.remove(); // avoids a ConcurrentModificationException
}}
switch (priority){
case 0:
B.getView().setBackgroundColor(context.getResources().getColor(R.color.color_pinkbutton));
break;
case 1:
B.getView().setBackgroundColor(Color.parseColor("#66ccff"));
break;
case 2:
B.getView().setBackgroundColor(Color.parseColor("#66ff33"));
break;
}
B.show();使用另一种方法将snackbar隐藏为
private static hideSnackbar(){
if(B !=null && B.isShown()){
B.dismiss();
}
}在你的情况下
If (NetworkCheck.isNetworkAvailable(this) == false) {
MyApplication.snack(null, 0, "Network Connection failed.",class.this)
}else{
MyApplication.hideSnackbar();
}发布于 2016-01-31 14:56:00
您只需创建广播接收器,并在您的活动/片段中接收以下事件并订阅它
完整信息在此处http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
https://stackoverflow.com/questions/35110955
复制相似问题