【问题标题】:Broadcast Receiver won't receive intent广播接收器不会收到意图
【发布时间】:2018-01-13 23:28:52
【问题描述】:

我正在尝试组合一个基本的 IntentService/BroadcastReceiver 组合。 是的,我知道目前没有什么用处,但我只是想让广播运行。 服务接收到我的初始意图,但接收器没有得到返回的广播(调试器)。其他答案对我没有帮助,我觉得我已经尝试了一切。它一定是微不足道的,但我就是不能指望它。

显示活动:

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

    }
}

服务和动作常量:

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"
    }
}

清单:

<?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>

【问题讨论】:

    标签: android android-intent gradle kotlin broadcastreceiver


    【解决方案1】:

    试试看

    首先将您的 BroadcastReceiver 设为静态或它自己的类,我不确定 Android 是否可以使用非静态内部类。

    其次,我不确定为什么你的接收者意图过滤器有一个

    filter.addDataScheme("http")
    

    但是您从服务中触发的 Intent 中丢失了它,如果您不需要它,请将其删除。

    【讨论】:

    • First make your BroadcastReceiver static or it's own class, I am not sure Android can work with non static inner classes. 只要它是动态注册的,它来自哪里都没有关系。
    • 更有趣的事实是,接收者期望数据 uri 以“http”开头的意图,但没有outgoingIntent.setData 调用。
    • 这个结果是可行的,但是我之前尝试过,我的应用程序刚刚崩溃,所以我认为过滤器可能需要一个数据方案。看起来有些事情只有在您请求某人帮助并且他们看一下时才起作用:)无论如何,谢谢,很抱歉将您的时间用于这么小的事情:)
    【解决方案2】:
    1. 尝试从过滤器中删除数据方案 http。 2. 尝试将类别默认添加到过滤器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多