Registering Broadcast Receiver on Android
-
Hi, I used this scheme but it seems like the receiver also needs to be registered in order to make it work. (Is that right?)
The second step I got from here but the callMethod() function cannot find my java function for registration.Java.lang.NoSuchMethodError: no non-static method "Lorg/qtproject/qt5/android/bindings/QtActivity;.registerReceiver()V"
So I tried to call it like this but no luck either.
Attempt to invoke virtual method 'android.content.Intent android.content.Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)' on a null object reference
I am kinda out of options. What is the right way to simply create an alarm, receive and raise a notification?
Thanks and have a nice day!Here is the code:
JAVA FILESpublic class RecieverRegistrator extends QtActivity { public void registerReceiver() { NotificationReceiver nr = new NotificationReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("intent_myaction_alarm"); registerReceiver(nr, intentFilter); } }
public class SetNotificationAlarm { public SetNotificationAlarm() {} public static void setAlarm(Context context) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 9); calendar.set(Calendar.MINUTE, 55); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Calendar cur = Calendar.getInstance(); if (cur.after(calendar)) { calendar.add(Calendar.DATE, 1); } Intent myIntent = new Intent(context, SetNotificationAlarm.class); int ALARM1_ID = 10000; PendingIntent pendingIntent = PendingIntent.getBroadcast( context, ALARM1_ID, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); } }
Public class NotificationReceiver extends BroadcastReceiver { private static NotificationManager m_notificationManager; private static Notification.Builder m_builder; @Override public void onReceive(Context context, Intent intent) { Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); try { m_notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel notificationChannel = new NotificationChannel("Qt", "Qt Notifier", importance); m_notificationManager.createNotificationChannel(notificationChannel); m_builder = new Notification.Builder(context, notificationChannel.getId()); } else { m_builder = new Notification.Builder(context); } m_builder.setSmallIcon(R.drawable.icon) .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon)) .setContentTitle("A message from Qt!") .setContentText("message").setSound(alarmSound) .setDefaults(Notification.DEFAULT_SOUND) .setColor(Color.GREEN) .setAutoCancel(true); m_notificationManager.notify(0, m_builder.build()); } catch (Exception e) { e.printStackTrace(); } } }
WHAT I CALL FROM main.cpp
QAndroidJniObject setNotificationAlarm = QAndroidJniObject::fromString("SetNotificationAlarm"); QAndroidJniObject::callStaticMethod<void>( "org/myapp/SetNotificationAlarm", "setAlarm", "(Landroid/content/Context;)V", QtAndroid::androidContext().object(), setNotificationAlarm.object<jstring>()); QtAndroid::androidActivity().callMethod<void>("registerReceiver");
IN MANIFEST
<receiver android:name="org.myapp.NotificationReceiver" android:enabled="true" />
-