Use C++Builder to get Android current ringtone mode.

Posted by on in Programming

The getRingerMode of JAudioManager. is Returns the current ringtone mode of Android.
JAudioManager::getRingerMode() is also available in C++Builder.

The current ringtone mode(getRingerMode()), one of RINGER_MODE_NORMAL, RINGER_MODE_SILENT, or RINGER_MODE_VIBRATE.

int name value
0 RINGER_MODE_SILENT Ringer mode that will be silent and will not vibrate.
1 RINGER_MODE_VIBRATE Ringer mode that will be silent and will vibrate.
2 RINGER_MODE_NORMAL Ringer mode that may be audible and may vibrate.

include

Several inclusions are necessary to use in C++Builder.

Unit1.h
#include <Androidapi.Helpers.hpp>
#include <Androidapi.JNI.GraphicsContentViewText.hpp>
#include <Androidapi.JNI.Media.hpp>
#include <Androidapi.JNIBridge.hpp>

Design.

2017-08-181122.png

Create variables for _di_JObject and _di_JAudioManager.

Unit1.h
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TComboBox *ComboBox1;
    TRectangle *Rectangle1;
    TLabel *Label1;
    TTimer *Timer1;
    TRectangle *Rectangle2;
    TButton *Button1;
    void __fastcall Timer1Timer(TObject *Sender);
    void __fastcall Button1Click(TObject *Sender);
private:    // User declarations
    _di_JObject f_sound_;
    _di_JAudioManager f_audio_manager_;
public:     // User declarations
    __fastcall TForm1(TComponent* Owner);
};

Create a JAudioManager instance.

Unit1.cpp
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    f_sound_            = TAndroidHelper::Activity->getSystemService(TJContext::JavaClass->AUDIO_SERVICE);
    f_audio_manager_    = TJAudioManager::Wrap((_di_IJavaInstance)f_sound_);
}

Get the current ringtone mode.

Get ringtone mode with TTimer event.
Change the color(Pink,Yellow,Limegreen) for each ringtone mode.

Unit1.cpp
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    int mode_ = f_audio_manager_->getRingerMode();

    Label1->Text = ComboBox1->Items->Strings[mode_];
    ComboBox1->ItemIndex = mode_;
    switch (mode_) {
    case 0:
        Rectangle1->Fill->Color = TAlphaColorRec::Pink;
        break;
    case 1:
        Rectangle1->Fill->Color = TAlphaColorRec::Yellow;
        break;
    case 2:
        Rectangle1->Fill->Color = TAlphaColorRec::Limegreen;
    }
}

Sets the ringer mode.

Each time you press the button on the bottom side, you change the mode.

Unit1.cpp
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    int mode_ = ComboBox1->ItemIndex + 1;
    if (mode_ >= ComboBox1->Items->Count) mode_ = 0;

    f_audio_manager_->setRingerMode(mode_);
}

2017_06_23_2.gif



About
Gold User, No rank,
Delphi / C++Builder blogger

Comments

Check out more tips and tricks in this development video: