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