21

What I am trying to do is that the numbers to which my application sends messages to, are passed to the BraodcastReceiver...but so far either I am getting null or BroadcastReceiver just simply crashes..

here is my intent for BroadcastReceiver from CreateMessage class...

        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("phN", phoneNo);
        intent.putExtras(bundle);
        startActivity(intent);

And in BroadcastReceiver (SmsReceiver class) I am trying to catch intent like this..

public void onReceive(Context context, Intent intent) {
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();
    try{
    //receiveNumbers = intent.getExtras().get("phN").toString();
        String  receiveNumbers = intent.getStringExtra("phN");
        Toast.makeText(context, receiveNumbers, Toast.LENGTH_LONG).show();
    }catch(Exception e){
        e.printStackTrace();
    }

My Manifest File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.AEM_n"
      android:versionCode="2" android:versionName="2.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>    
    <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" android:debuggable="true">
        <activity android:name=".new_menu"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="@string/app_name"
            android:name=".SetEvent" >
            <intent-filter>
                <action android:name="com.AEM_n.SETEVENT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            </activity>
            <activity
            android:label="@string/app_name"
            android:name=".AddParticipants" />
            <activity
            android:label="@string/app_name"
            android:name=".CreateMessage" />
            <activity
            android:label="@string/app_name"
            android:name=".DataBaseClass" />
            <activity 
            android:label="@string/app_name"
            android:name=".IntentReceiver"/>
            <activity 
            android:label="@string/app_name"
            android:name=".SmsReceiver"
            />
            <receiver android:name=".SmsReceiver" android:exported="true">
            <intent-filter android:priority="999">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
            </receiver>
    </application>

</manifest>

I have already tried all the answers given Stackoverflow...But no use...

Please guys tell me where I am going wrong...Thanks!

Error Log:

04-07 02:34:02.770: ERROR/AndroidRuntime(25593): FATAL EXCEPTION: main
04-07 02:34:02.770: ERROR/AndroidRuntime(25593): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.myapp.myaction (has extras) } in com.AEM_n.SmsReceiver@405a07c8
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.os.Handler.handleCallback(Handler.java:587)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.os.Looper.loop(Looper.java:123)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.app.ActivityThread.main(ActivityThread.java:3701)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at java.lang.reflect.Method.invoke(Method.java:507)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at dalvik.system.NativeStart.main(Native Method)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593): Caused by: java.lang.NullPointerException
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at com.AEM_n.SmsReceiver.onReceive(SmsReceiver.java:37)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     ... 9 more
Ahmed S. Durrani
  • 1,535
  • 2
  • 17
  • 41

6 Answers6

49

Following @Jason 's example...I did this...

In MainActivity or any activity from where you want to send intent from

Intent intent = new Intent("my.action.string");
intent.putExtra("extra", phoneNo); \\ phoneNo is the sent Number
sendBroadcast(intent);

and then in my SmsReceiver Class I did this

public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();

  Log.i("Receiver", "Broadcast received: " + action);

  if(action.equals("my.action.string")){
     String state = intent.getExtras().getString("extra");

  }
}

And in manifest.xml I added "my.action.string" though it was an option..

<receiver android:name=".SmsReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        <action android:name="my.action.string" />
        <!-- and some more actions if you want -->
    </intent-filter>
</receiver>

worked like charm!!

Ahmed S. Durrani
  • 1,535
  • 2
  • 17
  • 41
  • I am working on a pretty similar conception that my app needs to handle. IN my case I have to pass the data through an inter-device mechanism , which I have defined to be SMS, from one device to another. Is it possible to send the data as you did, by assigning to the intent the values? How to make my app the default app to treat when the sms is received? – JPerk May 15 '17 at 22:49
4

You starting an Activity instead of broadcasting Intent. Try to change

startActivity(intent);

to

sendBroadcast(intent);

UPDATE:

You set no action and no component name to the Intent. Try create intent as following:

Intent intent = new Intent(context, YourReceiver.class);
intent.putExtra("phN", phoneNo);
sendBroadcast(intent);
pepyakin
  • 2,217
  • 19
  • 31
0

You would send a broadcast like so:

Intent intent = new Intent(action);
intent.putExtra("phN", phoneNo);
sendBroadcast(intent);

The action parameter is a String that correlates to the action you registered the BroadcastReceiver with. So if you registered your receiver like so:

MyBroadcastReceiver receiver = new MyBroadcastReceiver();
registerReceiver( receiver, new IntentFilter( "com.myapp.myaction" ) );

then action would be "com.myapp.myaction"

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
  • where do I register my BroadcastReceiver ? In my activity class? – Ahmed S. Durrani Apr 05 '12 at 18:06
  • Yes, typically in the `onCreate()` method. Be sure also to call `unregisterReceiver(receiver)` in the `onDestroy()` method (meaning you'll need to have a class reference to the receiver). – Jason Robinson Apr 05 '12 at 18:12
  • Please see my Manifest file...I tried your code...but application keep crashing. – Ahmed S. Durrani Apr 06 '12 at 16:00
  • post the stack trace for your crash please – Jason Robinson Apr 06 '12 at 20:07
  • I'm really confused on what you're trying to accomplish. Are you wanting to use the same `BroadcastReceiver` to receive SMS message and to receive broadcasted phone numbers? That's a bad idea. Your app is crashing because the SMS intent doesn't have a value with key "phN". – Jason Robinson Apr 06 '12 at 23:59
  • Yes, I was trying to do that...as I have to compare outgoing numbers with incoming numbers so that I could perform some action...I do not know any other way..PLease tell me one..for key I changed it "extra" on both places... – Ahmed S. Durrani Apr 07 '12 at 08:30
  • Will the object we send to BroadcastReceiver receiver persist? Even when the app closes? –  Jan 14 '16 at 11:53
0

Your problem is actually very simple. It's enough that change onReceive() code just like it :

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    Log.i("Receiver", "Broadcast received: " + action);

   if(action.equals("my.action.string")){
       String state = bundle.getString("phN");

   }
}
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
BinMan1
  • 95
  • 1
  • 10
0

in activity

Intent intent = new Intent(context, destinationActivity);
// Send String
intent.putExtra(Intent.EXTRA_TEXT, mNameEntryString);
// Send int
intent.putExtra("intVariableName", intValue);
startActivity(intent);

in receiver

Intent intent = getIntent();
// get string
String recievedString = intent.getStringExtra(Intent.EXTRA_TEXT);
// get int
int intValue = intent.getIntExtra("intVariableName", 0); // 0 je default
Josef Vancura
  • 1,053
  • 10
  • 18
-3

use sendbroadcast instead of startactivity.it will work..!!

saksham agarwal
  • 250
  • 3
  • 14