How can I use SMS broadcast receiver in for different Activity - android

I have SMS broadcast Receiver Activity. I am using it for one screen(Monitor).
I also want to use this for another screen(Registration). I have different activities for Monitor and Registration. How can I use this?
Currently I am able to use the BroadcastRecevier only for Registration screen
public class SMSReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
Bundle bundle=intent.getExtras();
Object[] messages=(Object[])bundle.get("pdus");
SmsMessage[] sms=new SmsMessage[messages.length];
for(int n=0;n<messages.length;n++){
sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
}
for(SmsMessage msg:sms){
// RegActivity.updateMessageBox(msg.getMessageBody());
MonitorMenu.updateMessageBox(msg.getMessageBody());
}
}
}

Broadcast Receiver will called only when any msg is broadcasted so your receiver will be called when sms will be received.. so whenever your receiver is called save the data in somewhere and use it wherever you want to use..

Related

Error in broadcast receiver for android, it listens to all SMSs that my phone receives

I'm doing an encryption SMS app, in which, user can encrypt the text and send SMS through my apps.
I used the following broadcast receiver.
The problem is that its listen to all the SMS that come to my phone.
How to make it so that it will only listen to the SMS sent from my apps? Other sms's should open as normal, using default SMS application
public class SmsBroadCastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
// Specify the bundle to get object based on SMS protocol "pdus"
Object[] object = (Object[]) bundle.get("pdus");
SmsMessage sms[] = new SmsMessage[object.length];
Intent in=new Intent(context,DisplaySMSActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
String msgContent = "";
String originNum = "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < object.length; i++) {
sms[i] = SmsMessage.createFromPdu((byte[]) object[i]);
// get the received SMS content
msgContent = sms[i].getDisplayMessageBody();
//get the sender phone number
originNum = sms[i].getDisplayOriginatingAddress();
//aggregate the messages together when long message are fragmented
sb.append(msgContent);
//abort broadcast to cellphone inbox
abortBroadcast();
}
//fill the sender's phone number into Intent
in.putExtra("originNum", originNum);
//fill the entire message body into Intent
in.putExtra("msgContent", new String(sb));
//start the DisplaySMSActivity.java
context.startActivity(in);
}
Maybe you are not correctly registering your receiver.
When declaring a Receiver in the manifest (or programmatically), you can also specify an intent filter. You can specify the 'action' you want to receive in your receiver.
For example: "com.your_app_package.sms_encrypted_msg".
Either way, remember to check the action in the onReceive method:
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals('com.your_app_package.sms_encrypted_msg')){
...
}
}

send data from a broadcast to an activity . how to do send?

how to send data from a broadcatReceiver to an activity in android as saying catch the received SMS and send the SMS (is the data) to the activity its my first time that i work with broadcatReceiver so can anyone help me i will appreciate any help.
i read several tutorials about the data transferring that have some steps :
create a class that extends activity
create a class that extends broadcastReceiver
but i did not know how is the communication between these 2 classes.
I would do something like that:
public class YourActivity extends Activity
{
private Handler handler = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.main_layout);
this.handler = new Handler() {
#Override
public void handleMessage(Message msg)
{
SmsMessage sms = (SmsMessage) msg.obj;
String senderNumber = sms.getOriginatingAddress();
}
};
// Register a new receiver that will trigger on SMS_RECEIVED event
IntentFiler filer = new IntentFilert("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSmsReceiver, filter);
}
#Override
protected void onDestroy()
{
super.onDestroy();
// Unregister the receiver
unregisterReceiver(mSmsReceiver);
}
private mSmsReceiver = new BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if (bundle == null || bundle.containsKey("pdus") return;
// Decode the message
SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[0]);
// Notify the activity with the message
Message msg = new Message;
msg.obj = sms;
YourActivity.this.handler.sendMessage(msg);
}
};
}
The receiver and the Activity are separate entities and do not usually interact directly. Imagine the following scenario: you have a receiver getting lots of SMS messages, and every time you get one, you launch an Activity to show the message received. Wouldn't this be very annoying to the user?
I'd says that you can interact with the user by creating a Notification and if the user clicks on it, then you open the Activity you want to show the details.
As how to pass the data to the Activity (hopefully using a notification first), given that SMS messages are short in nature you can just put the data in the Intent.
How to set a Notification to launch an Activity: see Open application after clicking on Notification

Information from BroadcastReceiver to specific Activity via external class

I have "ComposeActivity" which calls the "SendSMS" method after onClick, which than calls metod in SMS class. I had also registered two BroadcastReceiver: SmsDeliveredReceiver and SmsSentReceiver, similar to: https://stackoverflow.com/a/17164931/1888738. How can I inform ComposeActivity, that sms was succesfullly sent, and that activity can clean some EditText's, and maybe show crouton with information that sms was sent or not(and why)? My codes: http://pastebin.com/LNRuSeBu
If you have receivers to handle when the SMS messages are sent or not sent. You could modify the onReceive of both of the receivers to send and intent to the ComposeActivity by creating an intent and calling intent.setComponent to specify where the intent should go. with some data that tells the ComposeActivity the result of trying to send the message.
Update:
public void onReceive(Context context, Intent arg1) {
Intent i = new Intent(action);
i.setComponent(new ComponentName("com.mypackage.compose","ComposeActivity"));
switch (getResultCode()) {
case Activity.RESULT_OK:
Log.d(getClass().getSimpleName(), "SMS delivered");
intent.setAction("com.mypackage.compose.SMS_SENT"); // String you define to match the intent-filter of ComposeActivity.
break;
case Activity.RESULT_CANCELED:
Log.d(getClass().getSimpleName(), "SMS not delivered");
intent.setAction("com.mypackage.compose.SMS_FAILED"); // String you define to match the intent-filter of ComposeActivity.
break;
}
startActivity(intent); // you may not necessarily have to call startActivity but call whatever method you need to to deliver the intent.
}
At that point it should just be matter of addind an intent-filter and a receiver to your compose activity either via the manifest or programatically. Your call. The strings I used were made up but you could pick an exiting intent action string or declare strings that you use in the intent filter. Again up to you. May also be helpful to look at questions about sending explicit intents to components like Android explicit intent with target component
or looking at the android docs.
Ok, after 5 hours of trying, I've already solved this:
in BroadcastReceiver in onReceive:
Intent intent = new Intent();
intent.setAction("SOMEACTION");
context.sendBroadcast(intent);
in Activity:
public BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("SOMEACTION")) {
Log.d(TAG, "Sent");
}
}
};
and in onCreate Activity I registered BroadcastReceiver:
registerReceiver(receiver, new IntentFilter("SOMEACTION"));
Thats all...

Receive bluetooth data in whole app

i develop an Android-Bluetooth App with 3-4 Activitys. Now i have to receive bluetooth data in any of these activitys.
I think i have to implement a Service which contains a BroadcastReceiver which listens to incoming BlueTooth Data and send a Broadcast, but i don't know how to do that.
Thanks in advance.
You can implement your own BroadcastReceiver. So, when your LocalService receive a data, it will notify using sendBroadcast method. Your activities should register the specific BrodcastReceiver.
In your Service
Notify about received messages:
public void onMessageReceived(String message) {
Intent intent = new Intent(ACTION_BLUETOOTH_MESSAGE);
intent.putExtra(BLUETOOTH_MESSAGE_CONTENT, message);
sendBroadcast(intent);
}
On each activity
Registering the broadcast receiver:
registerReceiver(messageReceiver,
new IntentFilter(ACTION_BLUETOOTH_MESSAGE));
Implementation of the broadcast receiver:
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra(BLUETOOTH_MESSAGE_CONTENT);
//Do something you want
}
};

Is it possible to receive SMS message on appWidget?

Is it possible to receive SMS message on appWidget?
I saw android sample source(API Demos).
In API Demos, ExampleAppWidgetProvider class extends AppWidgetProvider, not Activity.
So, I guess it is impossible to regist SMS Receiver like this,
rcvIncoming = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("telephony", "SMS received");
Bundle data = intent.getExtras();
if (data != null) {
// SMS uses a data format known as a PDU
Object pdus[] = (Object[]) data.get("pdus");
String message = "New message:\n";
String sender = null;
for (Object pdu : pdus) {
SmsMessage part = SmsMessage.createFromPdu((byte[])pdu);
message += part.getDisplayMessageBody();
if (sender == null) {
sender = part.getDisplayOriginatingAddress();
}
}
Log.i(sender, message);
}
}
};
registerReceiver(rcvIncoming, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
My goal is to receive SMS message on my custom appWidget.
Any help would be appreciated!!
AppWidgetProvider is a derived class of BroadcastReceiver. Therefore, you can override onReceive() to handle SMS message. Of course, you have to setup intent filter for SMS_RECEIVED in the manifest correctly first.

Categories

Resources