Android: 初始化 Intent 时找不到 Class 的符号

Android: Cannot find symbol of Class when initializing Intent

我正在编写一个简单的 Android 程序,它使用扩展 Android 的 BroadcastReceiver 的 class 来初始化 Intent。但是,当我 运行 我的程序时,我收到以下错误:

C:\Users\windows\AndroidStudioProjects\NotificationTest\app\src\main\java\com\example\notificationtest\MainActivity.java:60: error: cannot find symbol
        Intent notificationIntent = new Intent(this, MatchNotification.class);
                                                     ^
  symbol:   class MatchNotification
  location: class MainActivity
1 error

考虑到我已经在 MainActivity 相同的文件夹 中明确定义了 MatchNotification,这似乎很奇怪。下面是我的 MainActivity class:

package com.example.notificationtest;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;

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

        Intent notificationIntent = new Intent(this, MatchNotification.class);
    }
}

下面是我的 MatchNotification class:

package com.example.notificationtest

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.media.MediaPlayer
import android.media.RingtoneManager
import android.net.Uri

class MatchNotification : BroadcastReceiver() {
    private lateinit var player: MediaPlayer;
    private lateinit var context: Context;

    override fun onReceive(context: Context, intent: Intent) {
        this.context = context

        // Retrieve the URI of the alarm the user has set
        var ringtoneUri:Uri? = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
        player = MediaPlayer.create(context, ringtoneUri)
        player.start()
    }
}

下面是我的AndroidManifest.xml:

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

    <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=".MatchNotification"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

发送 broadcast Intent 宁愿看起来像这样:

Intent intent = new Intent();
intent.setAction("com.example.notificationtest.ON_MATCH");
intent.putExtra("data", "Nothing to see here, move along.");
sendBroadcast(intent);

其中需要将对应的intent-filter加入到receiver中:

<receiver
    android:name="com.example.notificationtest.MatchNotification"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        ...
        <action android:name="com.example.notificationtest.ON_MATCH" />
    </intent-filter>
</receiver>

另外 运行 :lint 然后修复它可能抱怨的所有 Kotlin 互操作性问题。