0

即使用户在我的应用程序之外,我也试图显示在 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();
        }
    }

}
4

1 回答 1

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!

于 2016-01-19T04:04:38.000 回答