简体   繁体   English

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

[英]Android: Cannot find symbol of Class when initializing Intent

I am writing a simple Android program that initializes an Intent , using a class that extends Android's BroadcastReceiver .我正在编写一个简单的 Android 程序来初始化Intent ,使用 class 扩展 Android 的BroadcastReceiver However, when I run my program, I receive the following error:但是,当我运行我的程序时,我收到以下错误:

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

This seems strange, considering that I have clearly defined MatchNotification in the same folder as MainActivity .这看起来很奇怪,考虑到我已经在与MainActivity相同的文件夹中明确定义了 MatchNotification 。 Below is my MainActivity class:下面是我的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);
    }
}

And below is my 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()
    }
}

Below is my AndroidManifest.xml :下面是我的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>

Sending a broadcast Intent would rather look alike this:发送广播Intent更倾向于如下所示:

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

Which requires the corresponding intent-filter added into the receiver :这需要将相应的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>

Also run :lint and then fix all Kotlin interoperability issues it may complain about.还运行:lint然后修复它可能抱怨的所有 Kotlin 互操作性问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM