如何在 BroadcastReceiver 中添加 Snackbars?
How to add Snackbars in a BroadcastReceiver?
Snackbars 通过在屏幕底部显示一条简短消息来提供有关操作的轻量级反馈。 Snackbars 可以包含一个动作。
Android还提供了一个toast,主要用于系统消息传递。 Toasts 与 snackbars 类似,但不包含动作且不能从屏幕上滑出。
我的问题
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class TestReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Toast.makeText(context, "status", Toast.LENGTH_LONG).show();
}
}
是否可以像 Toast
那样在 BroadcastReceiver
中显示 Snackbar
?
is it posible to show snakbars in BroadcastReceiver like Toast?
由 activity 或片段注册的 BroadcastReceiver
,通过 registerReceiver()
,可以要求 activity 或片段显示小吃店。
清单注册的 BroadcastReceiver
没有 UI,因此它没有地方显示小吃店。它可以做的是 post 进程内事件总线上的事件(例如,LocalBroadcastManager
,greenrobot 的 EventBus,Square 的 Otto),让你在前台的任何 UI知道收到广播。如果 UI 层收到消息,则 activity 或片段可以显示一个小吃店。如果事件总线事件未被拾取,如果合适,您也许可以显示 Notification
作为回退。
正如@CommonsWare先生所说,BroadcastReceiver
和Activity/Fragment
之间必须有一些传递机制,其中UI被附加。
我尝试在这里添加 interface
例如:
public class TestReceiver extends BroadcastReceiver {
private DoSomethingInterface callback1;
public TestReceiver() {
}
@Override
public void onReceive(final Context context, final Intent intent) {
// pass content text to show in SnackBar
if(callback1 != null) {
callback1.passText("status");
} else {
Log.e("log","callback from UI is not registered yet..");
}
}
public void registerReceiver(DoSomethingInterface receiver) {
this.callback1 = receiver;
}
public interface DoSomethingInterface {
public void passText(String text);
}
}
在显示 SnackBar 的 Activity 或 Fragment 中实施 DoSomethingInterface
。确保您需要添加 CoordinatorLayout
以显示 SnakeBar
:
public class MainActivity extends Activity implements DoSomethingInterface {
public void onCreate() {
...
// pass reference to interface from onCreate()
BroadcastReceiver mReceiver = new TestReceiver();
mReceiver.registerReceiver(this);
...
}
@Override
public void passText(String text) {
Snackbar.make(<reference to your coordinator layout>, "text", Snackbar.LENGTH_LONG)
.setAction("Ok", <listener>)
.setActionTextColor(Color.GREEN)
.show();
我的工作代码....
public abstract class TestReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
onNetworkChange();
}
protected abstract void onNetworkChange();
}
在主要活动中
public class MainActivity extends Activity {
public void onCreate() {
... mReceiver = new TestReceiver () {
@Override
protected void onNetworkChange() {
snackbar = Snackbar.make(Clayout, "Please check your internet connection and try again", Snackbar.LENGTH_SHORT);
snackbar.setAction("X", snackbarClickListener);snackbar.setActionTextColor(Color.GREEN);
ColoredSnackbar coloredsnakbar=new ColoredSnackbar();
coloredsnakbar.confirm(snackbar).show();
}
};
}
}
我有一个解决方法,无需在所有活动 onCreate()
方法中对小吃店进行编码。
我们可以使用应用程序class来调用BroadcastReciever
。
如下代码。
public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {
public static Activity appactivity;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
appactivity = activity;//here we get the activity
Intent i = new Intent(this, InternetConnectionInformation.class);
sendBroadcast(i);//here we are calling the broadcastreceiver to check connection state.
}
}
现在我们也可以使用 BroadcastReceiver class 来显示 snackbar
public class InternetConnectionInformation extends BroadcastReceiver{
static Snackbar snackbar; //make it as global
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) {
InternetConnectionInformation.snack(null, 0, "Network Connection failed.",context.getApplicationContext());
}else{
InternetConnectionInformation.hideSnackbar();
}
}
public static void snack (HashMap<String,View.OnClickListener> actions,int priority,String message,Context context) {
if(MyApplication.appactivity != null){
snackbar = Snackbar.make(MyApplication.appactivity.findViewById(android.R.id.content), message, Snackbar.LENGTH_INDEFINITE);//MyApplication.appactivity from Application class.
if (actions != null) {
Iterator iterator = actions.entrySet().iterator();
snackbar.setDuration(Snackbar.LENGTH_INDEFINITE);
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry) iterator.next();
snackbar.setAction((String) pair.getKey(), (View.OnClickListener) pair.getValue());
iterator.remove(); // avoids a ConcurrentModificationException
}
}
switch (priority) {
case 0:
snackbar.getView().setBackgroundColor(context.getResources().getColor(R.color.accentPink));
break;
case 1:
snackbar.getView().setBackgroundColor(Color.parseColor("#66ccff"));
break;
case 2:
snackbar.getView().setBackgroundColor(Color.parseColor("#66ff33"));
break;
}
snackbar.show();
}
}
private static void hideSnackbar(){
if(snackbar !=null && snackbar.isShown()){
snackbar.dismiss();
}
}
}
要隐藏快餐栏,我们需要检查连接状态。所以 BroadcastReceiver 注册在 Manifest
<receiver android:name="your.package.name.InternetConnectionInformation">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
这很好用并且已经过测试。希望对大家有所帮助。
Snackbars 通过在屏幕底部显示一条简短消息来提供有关操作的轻量级反馈。 Snackbars 可以包含一个动作。
Android还提供了一个toast,主要用于系统消息传递。 Toasts 与 snackbars 类似,但不包含动作且不能从屏幕上滑出。
我的问题
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class TestReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Toast.makeText(context, "status", Toast.LENGTH_LONG).show();
}
}
是否可以像 Toast
那样在 BroadcastReceiver
中显示 Snackbar
?
is it posible to show snakbars in BroadcastReceiver like Toast?
由 activity 或片段注册的 BroadcastReceiver
,通过 registerReceiver()
,可以要求 activity 或片段显示小吃店。
清单注册的 BroadcastReceiver
没有 UI,因此它没有地方显示小吃店。它可以做的是 post 进程内事件总线上的事件(例如,LocalBroadcastManager
,greenrobot 的 EventBus,Square 的 Otto),让你在前台的任何 UI知道收到广播。如果 UI 层收到消息,则 activity 或片段可以显示一个小吃店。如果事件总线事件未被拾取,如果合适,您也许可以显示 Notification
作为回退。
正如@CommonsWare先生所说,BroadcastReceiver
和Activity/Fragment
之间必须有一些传递机制,其中UI被附加。
我尝试在这里添加 interface
例如:
public class TestReceiver extends BroadcastReceiver {
private DoSomethingInterface callback1;
public TestReceiver() {
}
@Override
public void onReceive(final Context context, final Intent intent) {
// pass content text to show in SnackBar
if(callback1 != null) {
callback1.passText("status");
} else {
Log.e("log","callback from UI is not registered yet..");
}
}
public void registerReceiver(DoSomethingInterface receiver) {
this.callback1 = receiver;
}
public interface DoSomethingInterface {
public void passText(String text);
}
}
在显示 SnackBar 的 Activity 或 Fragment 中实施 DoSomethingInterface
。确保您需要添加 CoordinatorLayout
以显示 SnakeBar
:
public class MainActivity extends Activity implements DoSomethingInterface {
public void onCreate() {
...
// pass reference to interface from onCreate()
BroadcastReceiver mReceiver = new TestReceiver();
mReceiver.registerReceiver(this);
...
}
@Override
public void passText(String text) {
Snackbar.make(<reference to your coordinator layout>, "text", Snackbar.LENGTH_LONG)
.setAction("Ok", <listener>)
.setActionTextColor(Color.GREEN)
.show();
我的工作代码....
public abstract class TestReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
onNetworkChange();
}
protected abstract void onNetworkChange();
}
在主要活动中
public class MainActivity extends Activity {
public void onCreate() {
... mReceiver = new TestReceiver () {
@Override
protected void onNetworkChange() {
snackbar = Snackbar.make(Clayout, "Please check your internet connection and try again", Snackbar.LENGTH_SHORT);
snackbar.setAction("X", snackbarClickListener);snackbar.setActionTextColor(Color.GREEN);
ColoredSnackbar coloredsnakbar=new ColoredSnackbar();
coloredsnakbar.confirm(snackbar).show();
}
};
}
}
我有一个解决方法,无需在所有活动 onCreate()
方法中对小吃店进行编码。
我们可以使用应用程序class来调用BroadcastReciever
。
如下代码。
public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {
public static Activity appactivity;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
appactivity = activity;//here we get the activity
Intent i = new Intent(this, InternetConnectionInformation.class);
sendBroadcast(i);//here we are calling the broadcastreceiver to check connection state.
}
}
现在我们也可以使用 BroadcastReceiver class 来显示 snackbar
public class InternetConnectionInformation extends BroadcastReceiver{
static Snackbar snackbar; //make it as global
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) {
InternetConnectionInformation.snack(null, 0, "Network Connection failed.",context.getApplicationContext());
}else{
InternetConnectionInformation.hideSnackbar();
}
}
public static void snack (HashMap<String,View.OnClickListener> actions,int priority,String message,Context context) {
if(MyApplication.appactivity != null){
snackbar = Snackbar.make(MyApplication.appactivity.findViewById(android.R.id.content), message, Snackbar.LENGTH_INDEFINITE);//MyApplication.appactivity from Application class.
if (actions != null) {
Iterator iterator = actions.entrySet().iterator();
snackbar.setDuration(Snackbar.LENGTH_INDEFINITE);
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry) iterator.next();
snackbar.setAction((String) pair.getKey(), (View.OnClickListener) pair.getValue());
iterator.remove(); // avoids a ConcurrentModificationException
}
}
switch (priority) {
case 0:
snackbar.getView().setBackgroundColor(context.getResources().getColor(R.color.accentPink));
break;
case 1:
snackbar.getView().setBackgroundColor(Color.parseColor("#66ccff"));
break;
case 2:
snackbar.getView().setBackgroundColor(Color.parseColor("#66ff33"));
break;
}
snackbar.show();
}
}
private static void hideSnackbar(){
if(snackbar !=null && snackbar.isShown()){
snackbar.dismiss();
}
}
}
要隐藏快餐栏,我们需要检查连接状态。所以 BroadcastReceiver 注册在 Manifest
<receiver android:name="your.package.name.InternetConnectionInformation">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
这很好用并且已经过测试。希望对大家有所帮助。