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. Android. JNI. how to get username !?
Forum Updated to NodeBB v4.3 + New Features

Android. JNI. how to get username !?

Scheduled Pinned Locked Moved Mobile and Embedded
jni android use
5 Posts 1 Posters 2.0k Views 1 Watching
  • 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.
  • O Offline
    O Offline
    oleJa
    wrote on last edited by
    #1

    Good day. tell me please. How do I know login or e-mail user Android device?

    I tried to write on Java(JNI). but the code is not running.


    package org.qtproject.example.notification;

    import android.app.Notification;
    import android.app.NotificationManager;
    import android.content.Context;

    import android.accounts.AccountManager;
    import android.accounts.Account;
    import java.util.List;
    import java.util.LinkedList;

    public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity
    {
    static NotificationManager m_notificationManager;
    static Notification.Builder m_builder;
    static NotificationClient m_instance;

     static AccountManager manager;
     static Account[] accounts;
     static List<String> possibleEmails;
    
    public NotificationClient()
    {
        m_instance = this;
    }
    
    public static void notify(String s)
    {
        if (m_notificationManager == null) {
            m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
            m_builder = new Notification.Builder(m_instance);
            m_builder.setSmallIcon(R.drawable.icon);
            m_builder.setContentTitle( getUsername() );
        }
    
        m_builder.setContentText(s);
        m_notificationManager.notify(1, m_builder.build());
    }
    
    public static String getUsername() {
    
         manager = AccountManager.get(this);
         accounts = manager.getAccountsByType("com.google");
         possibleEmails = new LinkedList<String>();
    
        for (Account account : accounts) {
          possibleEmails.add(account.name);
        }
    
        if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
            String email = possibleEmails.get(0);
            String[] parts = email.split("@");
    
            if (parts.length > 1)
                return parts[0];
        }
    
        return "null";
    
    }
    

    }

    1 Reply Last reply
    0
    • O Offline
      O Offline
      oleJa
      wrote on last edited by
      #2

      i try this too.
      but i have: JNI DETECTED ERROR IN APPLICATION: JNI GetStaticMethodID called with pending exception 'java.lang.IllegalArgumentException' thrown in unknown throw location

      package org.qtproject.example;

      import android.accounts.AccountManager;
      import android.accounts.Account;

      public class Android_info extends org.qtproject.qt5.android.bindings.QtActivity{

      static Android_info m_instance;
      
      public Android_info(){
          m_instance = this;
      }
      
      public static String return_mail(){
          AccountManager am = AccountManager.get(m_instance);
          Account[] accounts = am.getAccountsByType("com.google");
          return accounts[0].name;
      }
      

      }

      1 Reply Last reply
      0
      • O Offline
        O Offline
        oleJa
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • O Offline
          O Offline
          oleJa
          wrote on last edited by oleJa
          #4

          this code workink fine on java android. but JNI+Qt its not working.

          package org.qtproject.example;

          import android.app.Notification;
          import android.app.NotificationManager;
          import android.content.Context;

          import android.accounts.AccountManager;
          import android.accounts.Account;

          public class Android_info extends org.qtproject.qt5.android.bindings.QtActivity{

          static Android_info m_instance;
          
          public Android_info(){
              m_instance = this;
          }
          
          public static String return_mail(){
              AccountManager am = AccountManager.get(m_instance);
              Account[] accounts = am.getAccountsByType("com.google");
              return accounts[0].name;
          }
          

          }

          1 Reply Last reply
          0
          • O Offline
            O Offline
            oleJa
            wrote on last edited by
            #5

            intellij idea output:
            java.lang.IllegalStateException: System services not available to Activities before onCreate()

            :(

            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