views:

110

answers:

2

I am currently working on an app that uses a broadcastreceiver to check for incoming text messages, but all of the sudden it seems to have stopped working, I even wrote a small test application that, to me at least, seems syntactically correct, but also is not functioning.

Here is the code from the test project:

The manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.AGApplications.test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".test"
                  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=".MsgMon"> 
            <intent-filter> 
                <action android:name=
                    "android.provider.Telephony.SMS_RECEIVED" /> 
            </intent-filter> 
        </receiver>

    </application>

    <uses-permission android:name="android.permission.RECEIVE_SMS">
    </uses-permission>

    <uses-sdk android:minSdkVersion="8" />
</manifest> 

The BroadcastReceiver:

package com.AGApplications.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MsgMon extends BroadcastReceiver{

   @Override
   //Called when new message is received
   public void onReceive(Context context, Intent intent) {
      Log.d("PHONE", "Message Received");
   }
}

And the main activity:

package com.AGApplications.test;

import android.app.Activity;
import android.os.Bundle;

public class test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

As far as I can tell I'm not doing anything wrong, but obviously I am!

A: 

All seems to ok for me i have done same example

here is my code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.infostretch.broadcastex"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">


    <receiver android:name="broadcastex" android:label="@string/app_name"><intent-filter><action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>



<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</manifest> 


public class broadcastex extends BroadcastReceiver {
    /** Called when the activity is first created. */
    private static final String TAG = "smsfwd";
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

public void onReceive(Context context, Intent intent) {

    Log.i(TAG, "Intent recieved: " + intent.getAction());

            Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
            if (messages.length > -1) {
                Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());

            }
        }

this code is working for me in android 2.1 have a try i think all is same as you

ud_an
The key here is the READ_PHONE_STATE permission. Also additional permission READ_SMS might be needed.
Marcin Gil
this permission in this case i think don't needed i have used that for my another functionality. i have run without this permission and working fine.
ud_an
A: 

So I discovered the issue. You cannot use "PHONE" as a debug tag. Don't ask me why, as I cannot seem to figure it out, but after using multiple different tags, "PHONE" was the only one that wouldn't register.

My bug seems to lie elsewhere and I am hot on the trail again! Thank you all for your help!

Andrew Guenther