Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Registering Broadcast Receiver on Android
QtWS25 Last Chance

Registering Broadcast Receiver on Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
androidnotificationsjava
4 Posts 3 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hitokage
    wrote on 2 Jun 2020, 08:08 last edited by
    #1

    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 FILES

    public 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" />
    
    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hitokage
      wrote on 9 Jun 2020, 13:08 last edited by Hitokage 6 Sept 2020, 17:10
      #2

      I found a way to register the receiver here. Not sure how I could miss it. There was another mistake with the Intent and IntentFilter which I solved using simple string as here.
      More more useful links here and here dealing with receiver when you close the app or make the phone sleep.

      1 Reply Last reply
      2
      • A Offline
        A Offline
        ashajg
        wrote on 10 Jun 2020, 17:23 last edited by
        #3

        Have u called registerReceiver() method somewhere?

        R 1 Reply Last reply 18 Aug 2020, 10:25
        0
        • A ashajg
          10 Jun 2020, 17:23

          Have u called registerReceiver() method somewhere?

          R Offline
          R Offline
          rcyboom
          wrote on 18 Aug 2020, 10:25 last edited by
          #4

          @ashajg how to registerReceiver in c++?

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved