I am trying to silence an incoming call and prevent the BlackBerry device from ringing. I tried Alert.setVolume(0) and some EventInjector keys but this didn't work.
So how to silence an incoming call?
I was puzzled by your question and decided to take up the challenge. I tried different thing including
Playing a "silence" audio file hoping to overlap the device's ringing or occupy the media player
Hacking the phone screen via UiApplication.getUiApplication().getActiveScreen()
Injecting keyboard events
Eventually, injecting VOLUME UP key (VOLUME DOWN key works as well) event worked for me and muted the device ringing on incoming call. The drawback with this approach is that sometimes the device did ring for a fraction of second before muting.
import net.rim.blackberry.api.phone.AbstractPhoneListener;
import net.rim.blackberry.api.phone.Phone;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.EventInjector;
import net.rim.device.api.ui.Keypad;
class Muter extends AbstractPhoneListener {
public void callIncoming(int callId) {
Thread muterThread = new Thread(new Runnable() {
public void run() {
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, (char) Keypad.KEY_VOLUME_UP, 0));
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_UP, (char) Keypad.KEY_VOLUME_UP, 0));
}
});
muterThread.setPriority(Thread.MAX_PRIORITY);
muterThread.start();
}
}
public class MuterApp extends Application {
public static void main(String[] args){
Phone.addPhoneListener(new Muter());
new MyApp().enterEventDispatcher();
}
}
The following also works (replace Muter thread in callIncoming() method with the following code).
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, (char) Keypad.KEY_VOLUME_UP, 0));
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_UP, (char) Keypad.KEY_VOLUME_UP, 0));
}
});
You won't be able to disable the sound programmatically (found a couple other sources that said the same thing). The best workaround people have seemed to come up with was to use the EventInjector to change the phone's sound profile to silent.
Some Blackberry phones have a mute key. You may try the following idea:
public void callIncoming(int callId) {
if (KeyPad.hasMuteKey()) {
/* Inject KEY_SPEAKERPHONE event */
}
else {
/* Inject KEY_VOLUME_DOWN event N times, so that you get the mute effect */
}
}
i am quite new to all this...but i thought i might as well put in my 2 cents worth...
i have been trying to find ways to programatically change the profile settings...
i have found that, while we cannot(yet) change the profile settings, we can change the setting that we are using( change the profile thats in use, i think)- this is something i came across searching for info-though i should give credit to alishaik786 for the code.
public final class LoadingScreen extends MainScreen implements FieldChangeListener
{
public LoadingScreen()
{
createGUI();
}
private void createGUI()
{
try
{
ApplicationManager.getApplicationManager().launch("net_rim_bb_profiles_app");
}
catch (Exception e)
{
//Exception
}
}
public void fieldChanged(Field field, int context)
{
}
}
Related
I am building a download manager in javafx
I have added function to download button which initialises new task.More than one download is also being executed properly.
But I need to add pause and resume function. Please tell how to implement it using executor. Through execute function of Executors, task is being started but how do i pause & then resume it??
Below I am showing relevant portions of my code. Please tell if you need more details. thanks.
Main class
public class Controller implements Initializable {
public Button addDownloadButton;
public Button pauseResumeButton;
public TextField urlTextBox;
public TableView<DownloadEntry> downloadsTable;
ExecutorService executor;
#Override
public void initialize(URL location, ResourceBundle resources) {
// here tableview and table columns are initialised and cellValueFactory is set
executor = Executors.newFixedThreadPool(4);
}
public void addDownloadButtonClicked() {
DownloadEntry task = new DownloadEntry(new URL(urlTextBox.getText()));
downloadsTable.getItems().add(task);
executor.execute(task);
}
public void pauseResumeButtonClicked() {
//CODE FOR PAUSE AND RESUME
}
}
DownloadEntry.java
public class DownloadEntry extends Task<Void> {
public URL url;
public int downloaded;
final int MAX_BUFFER_SIZE=50*1024;
private String status;
//Constructor
public DownloadEntry(URL ur) throws Exception{
url = ur;
//other variables are initialised here
this.updateMessage("Downloading");
}
#Override
protected Void call() {
file = new RandomAccessFile(filename, "rw");
file.seek(downloaded);
stream = con.getInputStream();
while (status.equals("Downloading")) {
byte buffer=new byte[MAX_BUFFER_SIZE];
int c=stream.read(buffer);
if (c==-1){
break;
}
file.write(buffer,0,c);
downloaded += c;
status = "Downloading";
}
if (status.equals("Downloading")) {
status = "Complete";
updateMessage("Complete");
}
return null;
}
}
You may be interested in Concurrency in JavaFX.
I guess you should also have a look at pattern Observer.
By the way I think you should not use constant string as a status ("Downloading", etc), creating an enum would be a better approach.
In your loop, around the read/write part, there should be a synchronization mechanism, controlled by your pause/resume buttons (see the two links).
Here is some of the code in my midlet:
the addKeyListener method presents an error as the function is not recognized.
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Keypad;
public class PhraZApp extends javax.microedition.midlet.MIDlet implements ActionListener{
public PhraZApp {
addKeyListener (new KeyPadListener());
}
protected void keyPressed(int key) {
System.out.println(key);
}
public void actionPerformed(ActionEvent evt) {
System.out.println(evt.getKeyEvent());
}
public final class KeyPadListener implements KeyListener {
public boolean keyChar(char key, int status, int time) {
return false;
}
public boolean keyDown(int keycode, int time) {
if (Keypad.KEY_ESCAPE == Keypad.key(keycode)) {
System.out.println("key: " + keycode);
return true;
}
//let the system to pass the event to another listener.
return false;
}
public boolean keyUp(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean keyRepeat(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean keyStatus(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
The keyPressed action is not heard by any of those listeners.
Ive been told to add the keylistner to a GUI component, but none that I try it with accept it.
Furthermore, one of the possible issues is that the addKeyListener method is not declared, but in that case I don't know how to declare it.
If i change extends javax.microedition.midlet.MIDlet to extends UiApplication, the addKeyListener becomes accepted but the entire midlet falls to a RuntimeErrorException.
How can I get my Midlet to hear the escape key? I have searched through many forums and none of the suggestions have worked so far.
Thanks in advance.
You need to create a LWUIT Command and assign it to the parent form using the setBackCommand method. You can handle the command event like you handle every other command in LWUIT. E.g. through a command listener or even just by subclassing it and overriding actionPerformed(ActionEvent).
Thanks to Shai pointing me in the right direction, I solved it.
Here is how I did it.
Command backCommand = new Command("",Keypad.KEY_ESCAPE);
form.setBackCommand(backCommand);
then
public void actionPerformed(ActionEvent evt) {
if (evt.getCommand().getId() ==Keypad.KEY_ESCAPE){
//execution code
}
I didn't try, but if I had included text in the command I imagine it would appear as such when I push the menu button. The important thing is that I finally got the MIDlet to hear out the escape button after MANY hours of trying and searching for solutions.
I am developing one Phone Application. In that my requirement is when the receiver accepts the call then I give an alert(a small sound) at caller(who is calling) side that caller thinks that receiver accepts the call.
For this I am using the following code for an alert method when call is connected:
public void callConnected(int callId)
{
Alert.startAudio(GetTone.TUNE, 80);
try
{
System.out.println("====================Start Audio: "+System.currentTimeMillis());
Thread.sleep(2000);
Alert.stopAudio();
}
catch (Exception e)
{
e.printStackTrace();
}
}
But I am not getting any TUNE sound.
Am I did correct or not?
If not give some solution for this.
I get the sound from this small code:
public static short frequency = 1046;
public static short duration = 200;
public static int volume = 100;
and whenever I want the sound call this method:
public static void beepSound()
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Alert.startAudio(new short[]{frequency, duration}, volume);
}
});
}
Then I got the beep sound.
I am new to BlackBerry App development. I want to be able to listen for keypress events whenever the BlackBerry (8900 in my case) is on and on all screens is this possible?
If so, it would be great for someone to direct me in the right direction. I am already having a look at Interface KeyListener.
import net.rim.device.api.system.*;
Thanks all
Implement a keylistenerClass like:
import model.Profile;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Keypad;
public final class ShortcutHandler implements KeyListener {
public boolean keyChar(char key, int status, int time) {
return false;
}
public boolean keyDown(int keycode, int time) {
if (Keypad.KEY_ESCAPE == Keypad.key(keycode)) {
// Consume the event.
// Here I'm consuming the event for the escape key
return true;
}
//let the system to pass the event to another listener.
return false;
}
public boolean keyRepeat(int keycode, int time) {
return false;
}
public boolean keyStatus(int keycode, int time) {
return false;
}
public boolean keyUp(int keycode, int time) {
return false;
}
}
Then in your application constructor
public Application() {
//Add the listener to the system for this application
addKeyListener(new ShortcutHandler());
}
I confirm that it's working when the application is in the background.
As I understood, you want to listen to all key events in all applications running on device, not only in your application.
I think it's not possible.
UPDATE
How does the volume up and down key work? – Abs 11 hours ago
If you want to say that all applications receive key events from volume keys, thats not true. RIM OS will receive those events and then update all audio components like alert, audio, player etc.
you can easely check it with this sample:
Do following:
run sample
enter some key events
look at events number
go background
enter some key events
go back to sample by menu->switch application
check events number, it still the same
Code:
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
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 KeyListenerApp extends UiApplication implements KeyListener {
Scr mScreen;
public KeyListenerApp() {
mScreen = new Scr();
pushScreen(mScreen);
addKeyListener(this);
}
public static void main(String[] args) {
KeyListenerApp app = new KeyListenerApp();
app.enterEventDispatcher();
}
private void updateScreen(final String text) {
mScreen.addLine(text);
}
public boolean keyChar(char key, int status, int time) {
updateScreen("keyChar " + key);
return true;
}
public boolean keyDown(int keycode, int time) {
updateScreen("keyDown " + keycode);
return true;
}
public boolean keyRepeat(int keycode, int time) {
updateScreen("keyRepeat " + keycode);
return true;
}
public boolean keyStatus(int keycode, int time) {
updateScreen("keyStatus " + keycode);
return true;
}
public boolean keyUp(int keycode, int time) {
updateScreen("keyUp " + keycode);
return true;
}
}
class Scr extends MainScreen {
int mEventsCount = 0;
LabelField mEventsStatistic = new LabelField("events count: "
+ String.valueOf(mEventsCount));
public Scr() {
super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
add(mEventsStatistic);
}
public void addLine(final String text) {
getApplication().invokeLater(new Runnable() {
public void run() {
mEventsStatistic.setText("events count: "
+ String.valueOf(++mEventsCount));
insert(new LabelField(text), 1);
}
});
}
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
menu.add(goBGMenuItem);
}
MenuItem goBGMenuItem = new MenuItem("go backgroun", 0, 0) {
public void run() {
getApplication().requestBackground();
}
};
}
This how I imagine it could work
create application which extends UiApplication or even Application
create an implementation of Keylistener (which could also extend Thread if you want)
add your KeyListener implementation to your application via addKeyListener()
Then do whatever you want.
The code given above certainly works, but there is a catch. You wont be able to trap the key presses on native apps like call handling sms incoming browsing and stuff. As system generates global event to these apps. Its like you are able to define a routine for clicks when your app is in background , but the functionality of that routine is localised to your application only. It wont effect other apps as such.
I have a BlackBerry application that needs to take pictures from the camera and send them to a server. In order to do this i invoke the native camera application and listen to the filesystem. Once an image is captured and saved as a new jpeg file i get notified, resume foreground control and go about my business. The problem starts occurring after the first time this cycle is completed because now when i decide to call the camera application again it is already opened, and now the user is seeing a thumbnail of the last picture that was taken and several buttons allowing him to manipulate/manage it. naturally what i want the user to see is a preview of what the camera is "seeing" before he snaps another photo as he did before.
I have thought of various ways to solve this including killing the camera app each time (I understand this cannot be done programatically?), sending CameraArguments when invoking the app (which appears to be useless), and now i was thinking a solution could be as simple generating a "Back" key event before switching back to my app which would theoretically dismiss the annoying edit screen. Could this really be done? and if not is there any other possible solution you may think of?
A kind of hack...
start Camera App
in TimerTask check if Camera App started and if it need to be closed (some flag)
if yes, invoke it(so it will became active) and push ESC keypress event injection to close it
Take a look at this:
class Scr extends MainScreen {
boolean killCameraApp = false;
final String mCameraModuleName = "net_rim_bb_camera";
final CameraArguments args = new CameraArguments();
public Scr() {
super();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if (isCameraRunning() && killCameraApp) {
getApplication().invokeAndWait(callCamera);
getApplication().invokeAndWait(killCamera);
}
}
}, 0, 100);
}
Runnable callCamera = new Runnable() {
public void run() {
callCamera();
}
};
Runnable killCamera = new Runnable() {
public void run() {
injectKey(Characters.ESCAPE);
killCameraApp = false;
}
};
private boolean isCameraRunning() {
boolean result = false;
ApplicationManager appMan =
ApplicationManager.getApplicationManager();
ApplicationDescriptor[] appDes = appMan.getVisibleApplications();
for (int i = 0; i < appDes.length; i++) {
result = mCameraModuleName.equalsIgnoreCase(appDes[i]
.getModuleName());
if (result)
break;
}
return result;
}
private void callCamera() {
Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA,
new CameraArguments());
}
private void injectKey(char key) {
KeyEvent inject = new KeyEvent(KeyEvent.KEY_DOWN, key, 0);
inject.post();
}
protected void makeMenu(Menu menu, int instance) {
menu.add(new MenuItem("start camera", 0, 0) {
public void run() {
callCamera();
killCameraApp = false;
}
});
menu.add(new MenuItem("kill app", 0, 0) {
public void run() {
killCameraApp = true;
}
});
super.makeMenu(menu, instance);
}
}
EDIT: Don't forget to set permissions for device release:
Options => Advanced Options => Applications => [Your Application] =>Edit Default permissions =>Interactions =>key stroke Injection