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. Intents with Qt and Android

Intents with Qt and Android

Scheduled Pinned Locked Moved Mobile and Embedded
2 Posts 1 Posters 2.4k 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.
  • P Offline
    P Offline
    Phataas
    wrote on last edited by
    #1

    I am making an Android app and using Intents are very common. I have struggled a lot trying to get this to work.

    I need an example and/or good explanation on how to do this. As far as I have seen there exists no proper documentation/example on how to do this.

    I have run the AndroidExtras/Notifier example and written some additional code as shown below.

    I have several questions:

    1. QtActivity I assume is the activity responsibly for the GUI showing on my Android device?
    2. Shall I pass my intent to QtActivity? If yes, how?
    3. How can one use startActivityForResult() and get the result in onActivityResult() like I have tried below?
    4. Is it possible to write code in QtActivity and call code directly from QtActivity? So I could implement this in QtActivity direcly.

    @package org.qtproject.example.notification;

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

    import android.bluetooth.BluetoothAdapter;
    import android.content.Intent;

    public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity
    {
    //Bluetooth
    private static BluetoothAdapter bluetoothAdapter;
    private static final int ENABLE_BLUETOOTH_REQUEST = 1;

    private static NotificationManager m_notificationManager;
    private static Notification.Builder m_builder;
    private static NotificationClient m_instance;
    
    public NotificationClient()
    {
        m_instance = this;
    }
    
    public 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("A message from Qt!");
        }
    
        m_builder.setContentText(s);
        m_notificationManager.notify(1, m_builder.build());
    }
    
    
    public void connect()
    {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(bluetoothAdapter == null)
        {
            notify("Bluetooth Adapter not found or supported");
            return;
        }
    
        if(!bluetoothAdapter.isEnabled())
        {
            Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetoothIntent, ENABLE_BLUETOOTH_REQUEST);
        }
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        notify("onActivityResult yay!");
    
        if(requestCode == 1)
        {
            if(resultCode == RESULT_OK)
            {
                //User accepted and bluetooth enabled
                    notify("User accepted to enable bluetooth");
            }
            else if(resultCode == RESULT_CANCELED)
            {
                //User declined or bluetooth enable error
                    notify("User declined to enable bluetooth");
            }
        }
    }
    

    }@

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Phataas
      wrote on last edited by
      #2

      Most of the troubles was solved reading "here":http://blog.qt.digia.com/blog/2013/12/12/implementing-in-app-purchase-on-android/. And some fixing in my manifest file.

      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