Why won't the BroadcastReceiver fail to start? - android

I have an app with the following manifest entries.
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" android:windowSoftInputMode="adjustPan" package="com.example.app" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17" />
<application android:allowBackup="true" android:hardwareAccelerated="true" android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="#string/app_name" android:name="TestSmsApp" android:theme="#android:style/Theme.Black.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.applegrew.cordova.android.plugin.SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
When my app is stopped and a new SMS is received, I see the following in logcat.
I/ActivityManager( 540): Start proc com.example.app for broadcast com.example.app/com.applegrew.cordova.android.plugin.SmsReceiver: pid=25457 uid=10095 gids={50095, 1028}
E/Trace (25457): error opening trace file: No such file or directory (2)
After that I see no more activity from my receiver. I also have a logger to print messages at the start of onReceive() but I guess that was never invoked. My receiver's code is as below.
package com.applegrew.cordova.android.plugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.LOG;
import org.apache.cordova.PluginResult;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class SmsReceiver extends BroadcastReceiver {
public static final String SMS_EXTRA_NAME = "pdus";
private CallbackContext callback_receive;
private boolean isReceiving = true;
// This broadcast boolean is used to continue or not the message broadcast
// to the other BroadcastReceivers waiting for an incoming SMS (like the native SMS app)
private boolean broadcast = true;
#Override
public void onReceive(Context ctx, Intent intent) {
LOG.v("com.example.app", ">>>>>>>>>>>>>>>>>onReceive called!!!!");
// Get the SMS map from Intent
Bundle extras = intent.getExtras();
if (extras != null)
{
// Get received SMS Array
Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);
for (int i=0; i < smsExtra.length; i++)
{
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
if(this.isReceiving && this.callback_receive != null) {
JSONObject obj = new JSONObject();
try {
obj.put(SMS.ADDRESS, sms.getOriginatingAddress());
obj.put(SMS.BODY, sms.getMessageBody());
LOG.v("com.example.app", ">>>>>>>>>>>>>>>>>with value" + sms.getOriginatingAddress() + ";; " + sms.getMessageBody());
PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
result.setKeepCallback(true);
callback_receive.sendPluginResult(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
// If the plugin is active and we don't want to broadcast to other receivers
// Also we cannot abort a broadcast which is not ordered.
if (this.isReceiving && !broadcast && isOrderedBroadcast()) {
this.abortBroadcast();
}
}
}
public void broadcast(boolean v) {
this.broadcast = v;
}
public void startReceiving(CallbackContext ctx) {
this.callback_receive = ctx;
this.isReceiving = true;
}
public void stopReceiving() {
this.callback_receive = null;
this.isReceiving = false;
}
}

I have just tried your code. It works well. onReceive() gets called, but the Application column is not filled in the LogCat. If you have filter set, you won't see the message. Please replace application filter with "tag:pdus" (or completely remove the filter) and you will see the output. Also use "Stop process" button in "Devices" view of Eclipse to stop the app.

Related

How to receive incoming SMS on Android KitKat or above?

I know from KitKat onward we need to make out app as default SMS app to be able to modify the SMS database.
But I don't want that. I only need to get notification of incoming SMS and display as toasts, and also send SMS.
Do I need to make my app default for that?
Edit 1:
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rtrgroup.sms_to_neta_server"
android:installLocation="preferExternal">
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginScreen" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"/>
<activity android:name=".MainScreen" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
</activity>
<service android:name=".MainService"
android:exported="true">
</service>
<receiver android:name=".MainReceiver"
android:exported="true"
android:enabled="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is my BroadcastReceiver:
package com.rtrgroup.sms_to_neta_server;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.widget.Toast;
import android.telephony.*;
public class MainReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage;
if (Build.VERSION.SDK_INT >= 19) { //For ver. >= KitKat
SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);
currentMessage = msgs[0];
} else {
Object pdus[] = (Object[]) bundle.get("pdus");
currentMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
}
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String messageB1 = currentMessage.getDisplayMessageBody();
String msg = currentMessage.getMessageBody();
String address = currentMessage.getOriginatingAddress();
Toast.makeText(context, senderNum + ": " + msg, Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
}
}
}
}
You don't need to be the default SMS app to send SMS. SmsManager.sendTextMessage() still works as long as you have the android.permission.SEND_SMS permission.
To receive SMSs, you can monitor the android.provider.Telephony.SMS_RECEIVED broadcast, with android.permission.RECEIVE_SMS permission.
Ref. http://android-developers.blogspot.fi/2013/10/getting-your-sms-apps-ready-for-kitkat.html

BroadcastReceiver onReceive event doesn't fire

I'm trying to listen for received SMS messages, but onReceive method is never called.
The program is built for 1.6 version of android, and it runs without errors with 2.3.7 version, but the event I said before doesn't fire.
Any tip?
// SMSListener CODE
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSListener extends BroadcastReceiver {
protected String smsBody = "";
public static final String SMS_EXTRA_NAME = "pdus";
protected SQLEventDataSource events;
#Override
public void onReceive(Context context, Intent intent) {
// Get SMS map from Intent
Bundle extras = intent.getExtras();
Toast.makeText( context, "O evento onReceive foi lançado", Toast.LENGTH_SHORT ).show();
String messages = "";
if ( extras != null )
{
// Get received SMS array
Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );
for ( int i = 0; i < smsExtra.length; ++i )
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
String body = sms.getMessageBody().toString();
// check if it's a configuration message
if (body.substring(0, 5) == "SMED#") {
String[] configs = body.split("#");
events.addEventConfiguration(configs[1], configs[2], configs[3]);
}
}
// Display SMS message
Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
}
}
private void setSmsBody(String smsBody) {
this.smsBody = smsBody;
}
public String getSMSBody() {
// TODO Auto-generated method stub
return this.smsBody;
}
}
// AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pt.ipbeja.estig.smed"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="1" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.provider.Telephony.SMS_RECEIVED" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name=".SMEDGPSActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SMSListener" android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Assuming you literally pasted your manifest, it contains a typo in the required permissions. It currently incorrectly lists the action filter string (android.provider.Telephony.SMS_RECEIVED) as opposed to the actual permission:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

Monitoring incoming SMS messages in Android

I have this program that will try to monitor incoming SMS messages. But when I tried to run my program in the emulator and I tried to send a SMS message, it works. But when I installed my program in my phone, its not working.
What is the problem?
And I want to run the program at the backend as well. How to do that?
BTW, below are the whole codes for this sample app.
Thanks
RJ
import java.io.BufferedWriter;
import java.io.FileWriter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
public class MySMSMonitor 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]);
}
Log.d("MySMSMonitor","SMS Message Received.");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="spy.Frandy.com"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="6" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".TelephonyDemo"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MySMSMonitor"> <intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter>
</receiver>
</application>
</manifest>
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class TelephonyDemo extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button sendBtn = (Button)findViewById(R.id.sendSmsBtn);
sendBtn.setOnClickListener(new OnClickListener(){
#Override public void onClick(View view) {
EditText addrTxt = (EditText)TelephonyDemo.this.findViewById(R.id.addrEditText);
EditText msgTxt = (EditText)TelephonyDemo.this.findViewById(R.id.msgEditText);
try {
sendSmsMessage(
addrTxt.getText().toString(),msgTxt.getText().toString());
Toast.makeText(TelephonyDemo.this, "SMS Sent",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(TelephonyDemo.this, "Failed to send SMS", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}});
}
#Override
protected void onDestroy() {
super.onDestroy();
}
private void sendSmsMessage(String address,String message)throws Exception {
SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendTextMessage(address, null, message, null, null);
}
}
I would simply insist you to use ContentObserver and register content://sms Uri and fetch the type of the SMS using
cusor.getString(cusor.getColumnIndex("type")); // 1 = SMS Received
// 2 = SMS Sent
Further you can easily get the required data from the cursor that you fetched. Here is a blog for the same.
I'm going to assume with the little detail you have provided that the reason it works on a emulator and not your actual phone; may be another conflicting app intercepting the messages before they are broadcasted to your app.
try adding android:priority="1000" to your intent-filter of your receiver in your apps manifest
(a thousand being whatever number you feel necessary, could be higher if needed)
<intent-filter android:priority="1000" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
more info on - intent-filter priority
Try This:
<receiver android:name=".MySMSMonitor"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="2">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

SMS Broadcast Receiver works in debug only with emulator

I have an app that tries to catch SMS messsages from a specific sender and then starts a new activity. When running in Eclipse using Debug As and using emulator my BroadcastReceiver it works great, it hits the breakpoints as expected. When I use Run As in Eclipse to launch in the emulator the SMS messages never get caught. It appears the BroadcastReceiver is never called based on the lack of Log outputs. The SMSes also do not get caught when running on my phone. Any ideas why it works one way but not another?
I have very few apps on my phone and the only one I know of that catches text messages is the built-in messenger one.
<receiver android:name=".sms.ConfirmationResponder">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
...
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
The receiver:
public class ConfirmationResponder extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.i("SMS", "############################ Confirmation being read");
// More stuff after this but I don't even get the log message
}
}
Here is my program:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
public class SMSBroadcastReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "SMSBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Intent recieved: " + intent.getAction());
if (intent.getAction() == SMS_RECEIVED) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
if (messages.length > -1) {
Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
}
}
}
}
}
And the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Technicaljar.SMSBroadcastReceiver"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true" >
<receiver android:name=".SMSBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
Hope it Helps
I think it works but eclipse is not attached to device when "Run as" mode and do not show logcat info.

Detecting phone activity in a class

So in my Android app, I've been testing how to detect an incoming and outgoing call. I got it to work by building a class that extends a BroadcastReceiver, but in that class if I call another class it crashes. For example MyClass mc = new MyClass(); mc.functionname();
My actual app is running a looping sound clip. I want to pause that sound clip when an incoming or outgoing call is made. The soundclip is played in a class that extends Activity. How do I do this?
This is what I have for the BroadcastReceiver class and my manifest.
package com.anthony.phone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class inComingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
Log.i("MYTAG",bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.i("MYTAG","State: "+ state);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("MYTAG",":) Incomng Number: " + phonenumber);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}
}
and my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.anthony.phone"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".CallTActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.anthony.phone.inComingReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
i had also this kind of problem. i solved by calling activity from broadcast receiver And writing my class code in particular Activity. As mentioned below
Intent i = new Intent(context,newActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
i hope it will help you to solve your query.
ask me if you further have any doubt.

Categories

Resources