Tareq Sulaiman
Tareq Sulaiman

Reputation: 71

Android BroadCastReceiver not working with custom actions

I am trying to implement a simple BroadCastReceiver with a custom action that pops a toast in the onReceive() method of the receiver but it is not working for some mysterious reason!!

The main activity only has a button that broadcasts an intent when clicked. I'm not sure if it is a problem with naming the action. Any help would be appreciated.

MainActivity :

   package com.example.tsupt.bcr;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    public void broadcastIntent(View view){
        Intent intent = new Intent();
        intent.setAction("com.example.tsupt.bcr.CUSTOM_INTENT");
        sendBroadcast(intent);
    }

}

The broadcast receiver :

public class BR extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"It worked",Toast.LENGTH_LONG).show();
        System.out.println("It worked");

}}

The manifest:

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tsupt.bcr">

    <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>
            <receiver android:name="com.example.tsupt.bcr.BR">
                <intent-filter>
                    <action android:name="com.example.tsupt.bcr.CUSTOM_INTENT">
                    </action>
                </intent-filter>

            </receiver>
        </activity>

    </application>

</manifest>

The button from the layout file:

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="broadcastIntent"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="180dp"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent" />

Upvotes: 1

Views: 3354

Answers (2)

sdabet
sdabet

Reputation: 18670

In your manifest, the <receiver> element should be a sibling of the <activity> element instead of a child:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tsupt.bcr">

<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>
    <receiver android:name="com.example.tsupt.bcr.BR">
        <intent-filter>
            <action android:name="com.example.tsupt.bcr.CUSTOM_INTENT">
            </action>
        </intent-filter>

    </receiver>

</application>

</manifest>

Upvotes: 0

Tareq Sulaiman
Tareq Sulaiman

Reputation: 71

Actually it worked after all. The problem seems to be with my huawei honor 5x that I was testing my app on. There is this battery saving ways huawei phones use and that looks like the source of the problem.

Upvotes: 0

Related Questions