Unable to start service Intent u=0 not found: AlarmReceiver

25 Views Asked by At

I'm trying to set a simple alarm on Android. Below I implemented the main activity, manifest, and the alarm receiver as a broadcast receiver. The alarm receiver never received a callback, as the log isn't called.

The error I'm hitting is:

Unable to start service Intent { cmp=com.package/.AlarmReceiver } U=0: not found

It doesn't crash the app. It just shows up and dies silently.

Main Activity:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AlarmPlaygroundTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {

                    val context = LocalContext.current
                    Button(onClick = {
                        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
                        val intent = Intent(context, AlarmReceiver::class.java)
                        val pendingIntent =
                            PendingIntent.getService(context, 1234, intent,
                                PendingIntent.FLAG_IMMUTABLE
                            )
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && alarmManager.canScheduleExactAlarms()) {
                            alarmManager.setExact(
                                AlarmManager.RTC_WAKEUP,
                                System.currentTimeMillis() + 5000,
                                pendingIntent
                            )
                            Log.d("jason", "set")
                        }
                    }) {
                        Text("Click me")
                    }
                }
            }
        }
    }
}

Manifest:

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

    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AlarmPlayground"
        tools:targetApi="31">
        <receiver android:name=".AlarmReceiver"/>
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.AlarmPlayground">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

AlarmReceiver:

class AlarmReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent?) {
        Log.d("jason", "received alarm")
        Toast.makeText(context, "hello", Toast.LENGTH_SHORT).show()
    }
}
0

There are 0 best solutions below