向Android应用程序添加基本的BroadcastReceiver

时间:2019-01-22 17:13:11

标签: android android-intent android-broadcastreceiver

谁能告诉我为什么我没有从此BroadcastReceiver得到任何结果?

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

<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"/>
            <action android:name="android.intent.action.VIEW"/>

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

    <receiver
            android:name=".TestReceiver"
            android:enabled="true"
            android:exported="false">
        <intent-filter>
            <action android:name="test.intent"/>
        </intent-filter>
    </receiver>
</application>

class TestReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
    // This method is called when the BroadcastReceiver is receiving an Intent broadcast.
    Toast.makeText(context, "SUCCESS", Toast.LENGTH_LONG).show()
    }
}

class MainActivity : AppCompatActivity() {

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

    val intent = Intent()
    intent.action = "test.intent"
    this.sendBroadcast(intent)

    }
}

我已经阅读了多篇SO帖子和有关如何设置基本BroadcastReceiver的多个教程,但看不到它是如何工作的。

我正在为吐司设置一个断点,希望它会触发并在没有运气的情况下监视吐司。

任何帮助将不胜感激。

0 个答案:

没有答案