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
Related
I wan catch SMS in my app and show it o my view. I create BroadcastReceiver:
public class SMSMonitor extends BroadcastReceiver {
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] messages = new SmsMessage[pduArray.length];
for (int i = 0; i < pduArray.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
}
StringBuilder bodyText = new StringBuilder();
for (SmsMessage message : messages) {
bodyText.append(message.getMessageBody());
}
String body = bodyText.toString();
Toast.makeText(context, body, Toast.LENGTH_LONG).show();
abortBroadcast();
}
}
}
And it show Toast wuth SMS body. Now i dont know what should I do
Create Service and pass SMS to service. And get SMS from service
Use Otto observer and send data(SMS) from:
1. BroadcastReceiver
2. Service
and catch in my activity.
Another more correct way
And if I will use Otto (I want it) where better send data - from BroadcastReceiver or pass data to service and send otto message from Service?
Otto or EventBus is for me the most simple solution and the solution that will reflect best your Activity lifecycle.
A great start is to register/unregister your bus from onPause() and onResume like so :
#Override
public void onResume() {
super.onResume();
BusProvider.getInstance().register(this);
}
#Override
public void onPause() {
super.onPause();
BusProvider.getInstance().unregister(this);
}
Next, post an event from your BroadcastReceiver like BusProvider.getInstance().post(new SmsEvent());
Your fragment or Activity will received the event via the #Subscribe method and the SmsEvent parameter type of the method.
It seems you don't need any Service here except if you want to save the data when the activity is not open.
I am trying to turn screen on and off via sms. here is my code below i don't know what has gone wrong as it is not working at all.Do help me in finding error. I am attaching manifest file too.Thank you in advance.
My java file:
public class MyReceiver extends BroadcastReceiver{
String sender;
#Override
public void onReceive(Context context, Intent intent) {
SmsMessage[] sms = null;
Bundle b = intent.getExtras();
String str = " SMS From : ";
if (b != null) {
Object[] pdus = (Object[]) b.get("pdus");
sms = new SmsMessage[pdus.length];
for (int i = 0; i < sms.length; i++) {
sms[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (i == 0) {
str += sms[i].getOriginatingAddress();
str += ":"+sms[i].getMessageBody().toString();
}else if (sms[i].getMessageBody().equals("D")) {
Intent in2= new Intent(Intent.ACTION_SCREEN_OFF);
in2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in2);
}else if (sms[i].getMessageBody().equals("E")) {
Intent in3= new Intent(Intent.ACTION_SCREEN_ON);
in3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in3);
}
}
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Log.d("Receiving", str);
}
}
}
MY manifest file:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<receiver android:name=".MyReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
</application>
Ok, first, regarding the receiving of SMS you might want to see my answer to this: communication between two device using sms
It is the most reliable way of receiving SMS that I have found.
Notice that SMS might not be the best option on newer devices from KitKat upwards if you do not want the message to appear on the device Hide sms notifications with a broadcastreceiver in KitKat
You might want to consider switching to push notifications from the internet instead. This is easily done using for instance parse.com
Regarding turning on and off screen I use the following piece of code.
private PowerManager.WakeLock wl;
private PowerManager pm;
public void screenWakeup(Context context, Activity activity) {
try {
pm = (PowerManager) context
.getSystemService(Activity.POWER_SERVICE);
// if (!pm.isScreenOn()) {
if (wl == null) {
wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "Turios");
}
if (!wl.isHeld()) {
wl.acquire();
}
final Window win = activity.getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// }
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
public void screenRelease(Activity activity) {
if (releaseWakeLock()) {
activity.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
Notice that it needs access to an activity, so it will not work unless your activity is alive. What I would suggest to do is:
Receive the turn on/off message
Send a new broadcast e.g. sendBroadcast(new Intent("INTENT_TURN_SCREEN_ON_OFF"))
Register a listener for the broadcast inside your activity
// in onCreate
wakeScreenReceiver = new WakeScreenReceiver();
registerReceiver(wakeScreenReceiver, new IntentFilter("INTENT_TURN_SCREEN_ON_OFF"));
// in onDestroy
unregisterReceiver(wakeScreenReceiver);
// WakeScreenReceiver defined somewhere inside the Acitivity
public class WakeScreenReceiver extends BroadcastReceiver {
private static final String TAG = "WakeScreenReceiver";
public RefreshModules() {
}
#Override
public void onReceive(Context context, final Intent intent) {
screenWakeup(MainActivity.this, MainActivity.this);
// or
screenRelease(MainActivity.this);
}
}
This way the app will only ever attempt to turn on/off the screen if the Activity is alive since otherwise the broadcast will simply not be received.
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'm trying to intercept the received SMSs by using a broadcast receiver. Here is the code:
<receiver android:name=".receivers.SmsReceiver" android:enabled="true"
android:exported="true" android:priority="999">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
and:
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
if (SMS_RECEIVED.equals(intent.getAction())) {
this.abortBroadcast();
Bundle bundle = intent.getExtras();
if (bundle != null) {
// get sms objects
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus.length == 0) {
return;
}
// large message might be broken into many
SmsMessage[] messages = new SmsMessage[pdus.length];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sb.append(messages[i].getMessageBody());
}
String sender = messages[0].getOriginatingAddress();
String message = sb.toString();
Log.d("sms", sender);
Log.d("sms", message);
}
}
}
}
The SMS is intercepted fine, but the stock Android SMS app is still showing its notifications and I can also find the message inside the stock app sms list.
Is there any way to stop the stock SMS app notifications and to avoid the message from appearing inside its list?
You need to call abortBroadcast();, see my answer to communication between two device using sms
If you are running Android 4.4 KitKat, it seems to be more difficult to do these sorts of things and have not looked into it yet myself.
As you said you are running KitKat, then answer is - you cannot mute default SMS app. You can also receive messages or send (that's why you get messages), but still, you cannot "consume" the message.
If user has set your application as default SMS app, then he/she is not going to get an SMS notification. You have to handle the notification, as well as other feature of the SMS in your app.
For more information read this blog.
Check out other blog and sample app.
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.