Android,如何在没有可见视图的情况下显示Snackbar?

时间:2016-01-18 23:18:18

标签: android android-snackbar

即使用户不在我的应用程序中,我也试图显示在snakbar中收到的任何短信,那么是否可以显示它?

以下是我之前使用Toast的代码:

    public class SmsReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getExtras();
        try {
            if (bundle != null) {
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (int i = 0; i < pdusObj.length; i++) {
                    SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdusObj[i]);

                    String message = sms.getDisplayMessageBody();

                    Toast toast= Toast.makeText(context ,message, Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.TOP, 0, 150);
                    toast.show();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:1)

参考http://developer.android.com/reference/android/support/design/widget/Snackbar.html

Snackbar will try and find a parent view to hold Snackbar's view from the value given to view.

在这种情况下你应该使用Toast或Notification!