:::: MENU ::::
Browsing posts in: BlackBerry

BlackBerry How to download a mp3 file and save it into SDCard Memory?

package com.mx.josesaid;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
public class Mp3FileUIApplication extends UiApplication {
 public static void main(String[] args) {
 new Mp3FileUIApplication().enterEventDispatcher();
 }
 public Mp3FileUIApplication() {
 this.pushScreen(new Mp3FileMainScreen());
 }
}
final class Mp3FileMainScreen extends MainScreen {
 private InfoScreen _infoScreen;
 public Mp3FileMainScreen() {
 super();
 LabelField title = new LabelField("Descargar archivo mp3 app", LabelField.ELLIPSIS
 | LabelField.USE_ALL_WIDTH);
 this.setTitle(title);
 }
 public boolean onClose() {
 Dialog.alert("Goodbye!");
 System.exit(0);
 return true;
 }
 private final MenuItem _viewItem = new MenuItem("Descargar Archivo", 110, 10) {
 public void run() {
 Mp3FileMainScreen.this._infoScreen = new InfoScreen();
 UiApplication.getUiApplication().pushScreen(Mp3FileMainScreen.this._infoScreen);
 }
 };
 private final MenuItem _closeItem = new MenuItem("Close", 200000, 10) {
 public void run() {
 Mp3FileMainScreen.this.onClose();
 }
 };
 protected void makeMenu(Menu menu, int instance) {
 menu.add(this._viewItem);
 menu.add(this._closeItem);
 }
 private class InfoScreen extends MainScreen {
 public InfoScreen() {
 super();
 LabelField mensaje = new LabelField();
 this.add(mensaje);
 final String remoteName = "http://178.211.63.20/ringtones/fulltrackbig/Juanes_Yerbatero.mp3";
 final String localName = "file:///SDCard/BlackBerry/music/juanes_Yerbatero.mp3";
 // UiApplication.getUiApplication().invokeAndWait(new Runnable() {
 // public void run() {
 DownloadCombiner d = new DownloadCombiner(remoteName, localName);
 d.start();
 // }
 // });
 }
 }
}
----------------------------------------------
package com.mx.josesaid;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
class DownloadCombiner extends Thread {
 private final String remoteName;
 private final String localName;
 private int connection;
 public DownloadCombiner(String remoteName, String localName) {
 this.remoteName = remoteName;
 this.localName = localName;
 }
 public void run() {
 try {
 int chunkIndex = 0;
 int totalSize = 0;
 FileConnection file = (FileConnection) Connector.open(this.localName);
 if (!file.exists()) {
 file.create();
 }
 file.setWritable(true);
 OutputStream out = file.openOutputStream();
 HttpConnection conn;
 InputStream in;
 while (true) {
 System.out.println("Opening Chunk: " + chunkIndex);
 // String currentFile = this.remoteName + "." + chunkIndex++;
 // conn = (HttpConnection) Connector.open(currentFile);
 String currentFile = this.remoteName + ";deviceside=true";
 conn = (HttpConnection) Connector.open(currentFile);
 if (conn.getResponseCode() != 200) {
 System.out.println("Response Code = " + conn.getResponseCode());
 break;
 }
 in = conn.openInputStream();
 int length = -1;
 byte[] readBlock = new byte[256];
 int fileSize = 0;
 while ((length = in.read(readBlock)) != -1) {
 out.write(readBlock, 0, length);
 fileSize += length;
 }
 totalSize += fileSize;
 System.out.println("Chunk Downloaded: " + fileSize + " Bytes");
 System.out.println("total Downloaded : " + totalSize + " Bytes");
 in.close();
 conn.close();
 in = null;
 conn = null;
 Thread.yield(); // allow other threads time
 }
 System.out.println("Full file downloaded: " + totalSize + " Bytes");
 out.close();
 file.close();
 System.out.println("Wrote file to local storage");
 } catch (IOException e) {
 this.mensaje(e.getMessage());
 } catch (Exception e) {
 this.mensaje(e.getMessage());
 }
 }
 public void mensaje(final String msg) {
 UiApplication.getUiApplication().invokeAndWait(new Runnable() {
 public void run() {
 Dialog.alert(msg);
 }
 });
 }
}





Save Persistent data in blackberry


package com.mx.controltrack.movil;

import java.util.Hashtable;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;


/**
 * Clase que permite el almacenamiento persistente en la Blackberry
 * 
 * @author José Said
 * 
 */
public class DataContext {
 //
 private final PersistentObject persistentObject;
 private Hashtable settingsTable;


 /**
  * 
  */
 public DataContext() {
  // Hash of examples.persistentstore.
  this.persistentObject = PersistentStore.getPersistentObject(0xc8027082ac5f496cL);
  synchronized (this.persistentObject) {


   this.settingsTable = (Hashtable) this.persistentObject.getContents();
   if (null == this.settingsTable) {
    this.settingsTable = new Hashtable();
    this.persistentObject.setContents(this.settingsTable);
    this.persistentObject.commit();
   }
  }
 }


 /**
  * @param key
  * @return objeto
  */
 public Object get(String key) {
  return this.settingsTable.get(key);
 }


 /**
  * @param key
  * @param value
  */
 public void set(String key, Object value) {
  this.settingsTable.put(key, value);
 }


 /**
  * 
  */
 public void commit() {
  this.persistentObject.commit();
 }


}

--------------------------
AnyWhere class we write something like this:


DataContetx objeto  = new DataContetx();
objeto.set(1, new Empleado());
objeto.set(2, new Empleado());
objeto.set(3, new Empleado());
objeto.commit();// It saves our record at the blackberry device, so easy no?


Pages:123