0

I'm using Unity to set alarms for a project. (Usually wouldn't use Unity but it is a requirement for different reasons). I've written an alarm like this, and this method is called from Unity with a parameter of 5:

public void SetAlarm (float time)
    {
        Intent intent = new Intent(context, AlarmClockPlugin.class);
        intent.setAction("ALARM_INTENT");
        PendingIntent pending = PendingIntent.getBroadcast(context,0,intent,0);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (long) (1000 * time), pending);
}

This alarm class also inherits from BroadcastReceiver and overrides the OnReceive method like this:

@Override
    public void onReceive(Context context, Intent intent) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        // Write toast text for now
        Toast.makeText(context,"Broadcast received",Toast.LENGTH_LONG).show();

        wl.release();
    }

I register the broadcast receiver in the manifest like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tombuild.myapp">
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:supportsRtl="true">
    <receiver android:process =":remote" android:name="com.tombuild.myapp.AlarmClockPlugin">
        <intent-filter>
            <action android:name="ALARM_INTENT"/>
        </intent-filter>
        </receiver>
</application>

</manifest>

But unfortunately the onReceive is not been called - no toast notification pops up at all after the 5 second duration, why is this?

Tom Whitcombe
  • 13
  • 1
  • 5

1 Answers1

0

I'm not familiar with Unity, but there obvious differences with Unity and Android Studio from the code you have posted, for example no Activity in the manifest. Why not try a simpler example in your setAlarm method.

    ...
    Intent intent = new Intent();
    intent.setAction("com.tombuild.myapp.ALARM_INTENT");
    sendBroadcast(intent);
    ...

And just put a Toast in the onReceive method.

There is a very simple tutorial on BroadcastReceivers on TutorialsPoint, which got me started.

G O'Rilla
  • 268
  • 3
  • 5