Receiving SMS messages in my app

I am creating an app that receives SMS text messages, I have created a class that extends BroadcastReceiver to receive and display SMS messagesin a toast within the app. I have also added permissions too my manifest along with the declaration of the receiver. The problem is it still does not seem to display the message. It will only appear on the top bar as a notification. Hopefully someone will be able to see where i am going wrong. Do i have to have some code in my mainactivity in order to display the message?? or should it display in my all my activities straight from the receiver clas?? code below: **UPDATE**
I ended up deleting my receiver and recreating everything, now it seems to work fine
public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); if (extras ==null) return; //toast Toast.makeText(context, "Received", Toast.LENGTH_LONG).show(); Object[] pdus = (Object[]) extras.get("pdus"); for (int i = 0; i < pdus.length; i++) { SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]); String sender = SMessage.getOriginatingAddress(); String body = SMessage.getMessageBody().toString(); Intent in = new Intent("SmsMessage.intent.MAIN").putExtra("get_msg", sender+":"+body); context.sendBroadcast(in); this.abortBroadcast(); } } }
A toast can generally not be shown from a BroadcastReceiver (unless it's part of a process which also happens to have a visible activity). Use a notification instead.

以上就是Receiving SMS messages in my app的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准