i'm having some trouble about this operation.
I have a widget that update some things. When arrive an sms , i wanna update it after receiving. I have written a BrodCastReceiver Class, but it update before arrive.
public class SmsReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
ComponentName name = new ComponentName(context, Randomnuberwidget.class);
int [] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(name);
Intent update = new Intent();
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
context.sendBroadcast(update);
}
i think you need to add an if statement checking the action of the intent... as far as i can tell a broadcastreciever is capable of picking up any implicit broadcast intent, so your code will fire anytime there is such a broadcast intent the way you have it set up now...
try adding
if (bundle.getAction().equals("android.provider.Telephony.SMS_RECEIVED"){
//update widget
}
grant i'm not certain about the if statement, but it should be a minor variation on that...
Related
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')){
...
}
}
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..
When I install the app everything works fine. I am able to print Message body every time I send a message until I lock mobile screen. After that, the app stops printing incoming messages. I tried many ways to overcome this problem but with no luck. Please help me...
public class SmsReceiveActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
Object[] pdus = (Object[]) bundle.get("pdus");
final android.telephony.SmsMessage[] messages = new android.telephony.SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = android.telephony.SmsMessage.createFromPdu((byte[]) pdus[i]);
incomingMsgString += messages[i].getMessageBody().toString();
}
// Print Incoming message Body
}
}
}
getApplication().registerReceiver(receiver, new IntentFilter(SMS_RECEIVED));
}
}
Per the BroadcastReceiver docs, when you register a receiver with registerReceiver(), "You won't receive intents when paused". If you want a receiver that is independent of your activity, you should implement it as a named class and publish it in your manifest. For example, create a named class with your anonymous BroadcastReceiver,
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
Object[] pdus = (Object[]) bundle.get("pdus");
final android.telephony.SmsMessage[] messages = new android.telephony.SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = android.telephony.SmsMessage.createFromPdu((byte[]) pdus[i]);
incomingMsgString += messages[i].getMessageBody().toString();
}
// Print Incoming message Body
}
}
}
And in your manifest add the receiver inside your application tag
<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/ >
</intent-filter>
</receiver>
Then when an SMS message is received your onReceive method will be invoked. There you can package up the information you need and launch an intent to your Activity or Service for further processing.
Create your broadcast as static inner class and register it in manifest. Try this link. Receiver as inner class in Android
I want to resend received SMS_RECEIVED broadcast. I've find example here: http://blog.dev001.net/post/14085892020/android-generate-incoming-sms-from-within-your-app and make it by analogy:
boolean received=false;
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
if(!received) {
received=true;
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
abortBroadcast();
if(/*my condition here*/) {
Intent update = new Intent();
update.setClassName( "com.android.mms", "com.android.mms.transaction.SmsReceiverService");
update.setAction("android.provider.Telephony.SMS_RECEIVED");
update.putExtra( "pdus" , new Object[] { pdus });
startService(update);
}
}
}
}
};
But this code crash com.android.mms (I see "I/ActivityManager(71): Process com.android.mms (pid 904) has died." in LogCat) with this:
threadid=8: thread exiting with uncaught exception (group=0x4001d800)
FATAL EXCEPTION: SmsReceiverService
java.lang.ClassCastException: [Ljava.lang.Object;
at android.provider.Telephony$Sms$Intents.getMessagesFromIntent(Telephony.java:617)
at com.android.mms.transaction.SmsReceiverService.handleSmsReceived(SmsReceiverService.java:299)
at com.android.mms.transaction.SmsReceiverService.access$100(SmsReceiverService.java:67)
at com.android.mms.transaction.SmsReceiverService$ServiceHandler.handleMessage(SmsReceiverService.java:172)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.os.HandlerThread.run(HandlerThread.java:60)
How to fix it?
If you want to use the same data( same received sms) it would be better to reuse the same intent and don't tray to build your own. just use intent.putExtras(Bundle extras);
Can you replace "update.putExtra( "pdus" , new Object[] { pdus });" with
"update.putExtra(pudsBundle)". Try it and let me know if it works, i haven't time to reproduce.
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.