Search code examples
androidandroid-intentgradlekotlinbroadcastreceiver

Broadcast Receiver won't receive intent


I am trying to put together just a basic IntentService/BroadcastReceiver combo. Yes, I know that what is currently there isn't really useful, but I just wanted to get the broadcast running. The Service receives my initial intent, but the Receiver doesn't get the return Broadcast (debugger). Other answers didn't help me, and I feel like I've tried everything. It has to be something trivial, but I just can't put my finger on it.

Displaying Activity:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val filter = IntentFilter()
        filter.addAction(Constants.BROADCAST_ACTION)
        filter.addDataScheme("http")
        val dsl  = DownloadStateReceiver()
        LocalBroadcastManager.getInstance(this).registerReceiver(dsl,filter)

        buttonLogin.setOnClickListener({
            var loginIntent = Intent(this,RawRestJsonPull::class.java)
            loginIntent.putExtra("userName",editLogin.text.toString())
            loginIntent.putExtra("password",editPassword.text.toString())
            this.startService(loginIntent)

        })

    }

     class DownloadStateReceiver : BroadcastReceiver()
    {
        override fun onReceive(currentContext: Context, incomingIntent: Intent) {
            Log.d("?????","d")
        }

    }
}

Service and the action constants:

class RawRestJsonPull : IntentService("rest" ) {

    override fun onHandleIntent(incomingIntent: Intent) {
        var outgoingIntent = Intent(Constants.BROADCAST_ACTION)
        var status = 0
        var userName : String? = incomingIntent.getStringExtra("userName")
        var password : String? = incomingIntent.getStringExtra("password")

        outgoingIntent.putExtra(Constants.EXTENDED_DATA_STATUS,status).putExtra("userName",userName).
                putExtra("password",password)
        outgoingIntent.flags = Intent.FLAG_INCLUDE_STOPPED_PACKAGES
        LocalBroadcastManager.getInstance(this).sendBroadcast(outgoingIntent)
    }



}

class Constants
{
    companion object {
        val BROADCAST_ACTION = "com.example.hubert1224.xxx.BROADCAST"
        val EXTENDED_DATA_STATUS = "com.example.hubert1224.xxx.STATUS"
    }
}

Manifest:

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

    <uses-permission android:name="android.permission.INTERNET" />
    <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"
                android:windowSoftInputMode="stateVisible">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        </activity>
        <service android:name=".RawRestJsonPull" />

        <receiver android:name=".MainActivity$DownloadStateReceiver" />
    </application>

</manifest>

Solution

  • Try that

    First make your BroadcastReceiver static or it's own class, I am not sure Android can work with non static inner classes.

    Second, I am not sure why your receiver intent filter has a

    filter.addDataScheme("http")
    

    But you are missing that from the Intent you are firing from your service, if you don't need it, remove it.