How to create and register a BroadcastReceiver in code

I am new to android. I am trying to register a BroadcastReceiver in code in 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 happens and no toast appears! I read this and this article and I don't understand where my problem is.

+3


source to share


2 answers


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"));

      



}

+1


source


Register the broadCast receiver on OnResume and unregister on 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);
     } 

      

0


source







All Articles