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?