3

i am new on android . I am trying register the BroadcastReceiver in code at my activity. this is my code:

MyReciever class:

public class MyReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Log.i("===>", "onReceive: "+  intent.getAction());
    Toast.makeText(context, "I got it "+ intent.getIntExtra("MyValue",0), Toast.LENGTH_SHORT).show();

}
}

myActivity:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myReciever = new MyReciever();
    intentFilter = new IntentFilter();
    intentFilter.addAction("test");
}

@Override
protected void onResume() {
    registerReceiver(myReciever, intentFilter);
    super.onResume();
}
}

Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.safarayaneh.mybroadcastreciever">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

when i run my app, nothing happen and toast not comes up! I've read this and this article and i don't understand that where is my problem.

sayres kabir
  • 362
  • 4
  • 22
  • Which broadcast are you expecting to receive? I don't see anything that's sending a broadcast with the `"test"` action. – Mike M. Jun 13 '17 at 06:30
  • @MikeM. you mean is that i have to use broadcast intent? – sayres kabir Jun 13 '17 at 06:32
  • Yeah, something has to broadcast an `Intent` your Receiver is registered for. Otherwise, nothing is going to happen. – Mike M. Jun 13 '17 at 06:35
  • @MikeM. but some tutorial did not use of broadcast intent? like this:http://code4reference.com/2012/09/register-unregiste-broadcast-receiver-part/ – sayres kabir Jun 13 '17 at 06:39
  • 1
    That example is registering for the `"android.intent.action.TIME_TICK"` broadcast, which is sent by the system every minute. Nothing is sending a broadcast with your custom action `"test"`. – Mike M. Jun 13 '17 at 06:42
  • @MikeM. Then i have to add reciver tag to manifest? – sayres kabir Jun 13 '17 at 06:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146492/discussion-between-sayres-kabir-and-mike-m). – sayres kabir Jun 13 '17 at 06:56
  • I have changed all thing that you said , but still does not work: http://codepad.org/W9Nl2TXD – sayres kabir Jun 13 '17 at 06:59
  • The `onCreate()` method runs before `onResume()`, so you're sending the broadcast before you've registered the Receiver. Beyond that, if you mean to do this all dynamically in your code, you don't need the manifest element for your Receiver. – Mike M. Jun 13 '17 at 07:03
  • 1
    @MikeM. thank you ,finally it is work . – sayres kabir Jun 13 '17 at 07:11

2 Answers2

1
public class MainActivity extends AppCompatActivity {

    private IntentFilter intentFilter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //this line makes the broadcastreceiver
        BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Toast.makeText(context, "I got it "+ intent.getIntExtra("MyValue",0), Toast.LENGTH_SHORT).show();
            }
        };

        //this line register broadcastreceiver
        LocalBroadcastManager.getInstance(getContext()).registerReceiver(mBroadcastReceiver, new IntentFilter("test"));

        //this line calls the broadcastreceiver
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("test"));
    }    

}
Mike
  • 8,137
  • 6
  • 28
  • 46
Meikiem
  • 1,876
  • 2
  • 11
  • 19
0

Register your broadCast receiver in OnResume and unregister it in OnPause like this

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myReciever = new MyReciever();
    intentFilter = new IntentFilter();
    intentFilter.addAction("test");
}

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unregisterReceiver(reMyreceive);
     }
     @Override
     protected void onResume() {
        // TODO Auto-generated method stub
         super.onResume();

          registerReceiver(reMyreceive, filter);
     } 
Sandeep dhiman
  • 1,863
  • 2
  • 17
  • 22