Código para programadores de apps Android

A continuación tienen una lista de trucos que seguro les serán de utilidad para aquellos que se dediquen a la programación de aplicaciones para móviles Android:


ÍNDICE:

 -OBTENER EL IMEI
 -CERRAR UNA APP
 -CÓMO UTILIZAR LA VIBRACIÓN 
-OBTENER EL ACTUAL VOLUMEN DE AUDIO
 -OBTENER UNA LISTA DE LAS LLAMADAS
 -OBTENER LA RELACIÓN DE SENSORES
 -LISTAR LOS SENSORES DE SISTEMA
 -OBTENER EL TIPO-MIME DE UN FICHERO


CÓDIGO FUENTE:


 - OBTENER EL IMEI

uses
  FMX.Helpers.Android, Androidapi.JNI.JavaTypes,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Telephony, Androidapi.JNIBridge;
 
var
  TelephonyObj: JObject;
  TelephonyManager: JTelephonyManager;
begin
  TelephonyObj := SharedActivityContext.getSystemService(TJContext.JavaClass.TELEPHONY_SERVICE);
  TelephonyManager := TJTelephonyManager.Wrap((TelephonyObj as ILocalObject).GetObjectID);
  Label1.Text := JStringToString(TelephonyManager.getDeviceId);


- CERRAR UNA APP

uses
  FMX.Platform.Android;
  
procedure TForm1.Button4Click(Sender: TObject);
begin
  MainActivity.finish;
end;


- CÓMO UTILIZAR LA VIBRACIÓN


uses
  FMX.Helpers.Android, Androidapi.JNI.JavaTypes, Androidapi.JNI.Os,
  Androidapi.JNI.App, Androidapi.JNIBridge, Androidapi.JNI.GraphicsContentViewText;
  
function IntArrayToJArray(const OrigArray: array of integer): TJavaArray;
var
  i: integer;
begin
  Result := TJavaArray.Create(Length(OrigArray));
  for i := Low(OrigArray) to High(OrigArray) do
    Result.Items[i] := OrigArray[i];
end;
  
procedure TForm1.Button1Click(Sender: TObject);
var
  VibratorObj: JObject;
  Vibrator: JVibrator;
begin
  VibratorObj := SharedActivity.getSystemService(TJActivity.JavaClass.VIBRATOR_SERVICE);
  Vibrator := TJVibrator.Wrap((VibratorObj as ILocalObject).GetObjectID);
   
    Vibrator.vibrate(StrToInt(ClearingEdit1.Text));
  //or
  //Vibrator.vibrate(IntArrayToJArray([1000, 5000, 3000, 1000]), -1);
  //or
  //Vibrator.cancel();
  //or
  //if Vibrator.hasVibrator() then 
  //... 
  //else 
  //...
end;


- OBTENER EL ACTUAL VOLUMEN DE AUDIO

uses
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.Media,
  Androidapi.Helpers,
  Androidapi.JNI.App,
  Androidapi.JNIBridge;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  AudioObj: JObject;
  Audio: JAudioManager;
  CurrentVolume: Integer;
begin
  AudioObj = SharedActivity.getSystemService(TJActivity.JavaClass.AUDIO_SERVICE);
  Audio := TJAudioManager.Wrap((AudioObj as ILocalObject).GetObjectID);
  CurrentVolume = Audio.getStreamVolume(TJAudioManager.JavaClass.STREAM_MUSIC);
end;


- OBTENER UNA LISTA DE LAS LLAMADAS DEL MÓVIL


uses
  FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Provider, Androidapi.JNI.JavaTypes, System.DateUtils;

procedure TForm1.Button1Click(Sender: TObject);
var
  cursor: JCursor;
  CallTypeInt: Integer;
  CallDuration, CallDate: Int64;
  CallName, CallTypeStr: String;
  ListBoxItem: TListBoxItem;
begin

  cursor := SharedActivity.getContentResolver.query(
   TJCallLog_Calls.JavaClass.CONTENT_URI,
    nil,
     nil,
      nil,
       nil);

  ListBox1.Clear;
  ListBox1.BeginUpdate;

  if (cursor.getCount > 0) then
  begin

    while (cursor.moveToNext) do
    begin

      CallName := JStringToString(cursor.getString(
        cursor.getColumnIndex(TJCallLog_Calls.JavaClass.CACHED_NAME)));

      if CallName = '' then begin
        CallName := JStringToString(cursor.getString(
        cursor.getColumnIndex(TJCallLog_Calls.JavaClass.NUMBER)));
      end;

      CallTypeInt := StrToInt(JStringToString(cursor.getString(
        cursor.getColumnIndex(TJCallLog_Calls.JavaClass.&TYPE))));

      case CallTypeInt of
        1: CallTypeStr := 'Bandeja de entrada';
        2: CallTypeStr := 'Bandeja de salida';
        3: CallTypeStr := 'Desconocido';
        else CallTypeStr := 'Cancelado';
      end;

      CallDuration := StrToInt64(JStringToString(cursor.getString(
        cursor.getColumnIndex(TJCallLog_Calls.JavaClass.DURATION))));

      CallDate := StrToInt64(JStringToString(cursor.getString(
        cursor.getColumnIndex(TJCallLog_Calls.JavaClass.DATE))));

      ListBoxItem := TListBoxItem.Create(ListBox1);

      ListBoxItem.ItemData.Text := CallTypeStr + ': ' + CallName;

      ListBoxItem.ItemData.Detail := DateTimeToStr(TTimeZone.Local.ToLocalTime(
        UnixToDateTime(CallDate div 1000))) + '   Duración: '
         + Format('%2.2d:%2.2d', [CallDuration div 60, CallDuration mod 60]);

      ListBox1.AddObject(ListBoxItem);

      Label1.Text := 'Всего: ' + IntToStr(ListBox1.Count);

    end;
  end;

  cursor.close;

  ListBox1.EndUpdate;

end;


- OBTENER LA RELACIÓN DE SENSORES


uses
  android.hardware.SensorManager, android.hardware.Sensor,
  Androidapi.JNI.JavaTypes, FMX.Helpers.android, Androidapi.JNIBridge,
  Androidapi.JNI.GraphicsContentViewText, Androidapi.Helpers;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  SensorsManagerObj: JObject;
  SensorsManager: JSensorManager;
  Sensors: JList;
  Sensor: JSensor;
  i: Integer;
  StringType: String;
begin
  Memo1.Lines.Clear;
 
  SensorsManagerObj := SharedActivityContext.getSystemService
    (TJContext.JavaClass.SENSOR_SERVICE);
  SensorsManager := TJSensorManager.Wrap((SensorsManagerObj as ILocalObject)
    .GetObjectID);
 
  Sensors := SensorsManager.getSensorList(TJSensor.JavaClass.TYPE_ALL);
 
  Label1.Text := 'Found: ' + IntToStr(Sensors.size) + ' sensors';
 
  for i := 0 to Sensors.size - 1 do
  begin
    Sensor := TJSensor.Wrap((Sensors.get(i) as ILocalObject).GetObjectID);
 
    Memo1.Lines.Add('Name: ' + JStringToString(Sensor.getName));
 
    case Sensor.getType of
      TJSensorTYPE_ACCELEROMETER:
        StringType := 'Acelerometro';
      TJSensorTYPE_GRAVITY:
        StringType := 'Gravedad';
      TJSensorTYPE_GYROSCOPE:
        StringType := 'Giroscopio';
      TJSensorTYPE_LIGHT:
        StringType := 'Iluminación';
      TJSensorTYPE_LINEAR_ACCELERATION:
        StringType := 'Aceleración';
      TJSensorTYPE_MAGNETIC_FIELD:
        StringType := 'Campo magnético';
      TJSensorTYPE_ORIENTATION:
        StringType := 'Orientación';
      TJSensorTYPE_PRESSURE:
        StringType := 'Presión';
      TJSensorTYPE_PROXIMITY:
        StringType := 'Proximidad';
      TJSensorTYPE_ROTATION_VECTOR:
        StringType := 'Vector de rotación';
      TJSensorTYPE_TEMPERATURE:
        StringType := 'Temperatura';
    else
      StringType := 'Undefined';
    end;
 
    Memo1.Lines.Add('Type: ' + StringType);
    Memo1.Lines.Add('Vendor: ' + JStringToString(Sensor.getVendor));
    Memo1.Lines.Add('Version: ' + IntToStr(Sensor.getVersion));
    Memo1.Lines.Add('Resolution: ' + FloatToStr(Sensor.getResolution));
    Memo1.Lines.Add('Max Range: ' + FloatToStr(Sensor.getMaximumRange));
    Memo1.Lines.Add('Power: ' + FloatToStr(Sensor.getPower) + 'mA');
    Memo1.Lines.Add('MinDelay: ' + IntToStr(Sensor.getMinDelay));
    Memo1.Lines.Add('----------------');
 
  end;
end;


- LISTAR LOS SENSORES DE SISTEMA

uses
  System.Sensors, System.TypInfo;
 
procedure TForm1.Button2Click(Sender: TObject);
var
  SensorsManager: TSensorManager;
  i: Integer;
  Sensor: TCustomSensor;
begin
  SensorsManager := TSensorManager.Current;
  if SensorsManager.CanActivate then
  begin
    SensorsManager.Activate;
 
    Memo2.Lines.Clear;
    Label2.Text := 'Found: ' + IntToStr(SensorsManager.Count) + ' sensors';
 
    for i := 0 to SensorsManager.Count - 1 do
    begin
      Sensor := SensorsManager.Sensors[i];
      Memo2.Lines.Add('Name: ' + Sensor.Name);
      Memo2.Lines.Add('Category: ' + GetEnumName(TypeInfo(TSensorCategory),
        Ord(Sensor.Category)));
      Memo2.Lines.Add('Manufacturer: ' + Sensor.Manufacturer);
      Memo2.Lines.Add('Model: ' + Sensor.Model);
      Memo2.Lines.Add('State: ' + GetEnumName(TypeInfo(TSensorState),
        Ord(Sensor.State)));
      Memo2.Lines.Add('SerialNo: ' + Sensor.SerialNo);
      Memo2.Lines.Add('Description: ' + Sensor.Description);
      Memo2.Lines.Add('UniqueID: ' + Sensor.UniqueID);
      Memo2.Lines.Add('----------------');
    end;
  end;
  SensorsManager.Current.Deactivate;
end;


- OBTENER EL TIPO-MIME DE UN FICHERO

uses
  Androidapi.JNI.JavaTypes, Androidapi.JNI.Webkit;
  
var
  ExtFile: string;
  mime: JMimeTypeMap;
  ExtToMime: JString;
begin
  //Determinar la extensión de archivo y el tipo MIME
  ExtFile := AnsiLowerCase(StringReplace(TPath.GetExtension(path), '.', '',[]));
  mime := TJMimeTypeMap.JavaClass.getSingleton();
  ExtToMime := mime.getMimeTypeFromExtension(StringToJString(ExtFile));

No hay comentarios:

Publicar un comentario