Intents with Qt and Android



  • 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");
            }
        }
    }
    

    }@



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.