Adam Lee
Adam Lee

Reputation: 576

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. 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. Below is my 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:

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:

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

Upvotes: 0

Views: 1038

Answers (1)

Martin Zeitler
Martin Zeitler

Reputation: 76639

Sending a broadcast Intent would rather look alike this:

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:

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

Upvotes: 1

Related Questions