How to make a J2ME application run in Background? - java

I have a written a J2ME application which uses Bluetooth and search a file within the peer mobile and download it. I would like to make my application run in background , whenever I get a call , or message and later resume after few seconds , Has anybody worked on this please share your experience . Is there any way to run a Midlet in background ?

to set a j2me app to the background use the following in your midlet class:
Display.getDisplay (this).setCurrent (null);
to get the screen back use the following:
Display.getDisplay (this).setCurrent (myCanvas);
Where myCanvas is your canvas instantiation
R
p.s. You can still use a thread or timer to do things in the background while your midlet is hidden.
p.s.2: this does not work on all models. (Works on Nokia s60, SonyEricsson, but not on Nokia s40, Samsung and some others.

A device's ability to run an application in the background depends on its ability to multitask. Therefore, more expensive, PDA-type devices are more likely to support background execution than lower-cost devices.
For in background :-
private Display display = Display.getDisplay(this);
private Displayable previousDisplayable;
public void toBack() {
previousDisplayable = display.getCurrent();
display.setCurrent(null);
}
And to come in Fore ground :-
public void toFront() {
display.setCurrent(previousDisplayable);
}
But be aware that it not supports every device.(Works on Nokia s60, SonyEricsson, but not on Nokia s40, Samsung and some others).

Related

Android SDK, Check if device is Amazon-FireTV

I am trying to write a simple piece of code that will execute some other code if true. What I want to do is check if my app is running on the 'Amazon Fire-TV (BOX, not the Fire-TV stick)' I think it would not be that hard to do but I am guessing it would be something like this?
String osName = android.getSystemOS();
if(!osName.equals("AMAZON FIRE-TV")){
Toast.makeText(MainActivity.class, "This app may not be compatible with your device..., Toast.LENGTH_LONG").show();
...
}
You can check any device name specifically using:
boolean isFireTV = Build.MODEL.equalsIgnoreCase("AFTB");
(see this page for FireTV model strings, and this one for Fire Tablets)
I'd also check out this answer for a more generic test to help you determine if your app is running on an Amazond device, or installed via the Amazon AppStore (eg on a Blackberry device)
the following function:
public static boolean isTV() {
return android.os.Build.MODEL.contains("AFT");
}
should detect either firetv or fire tv stick
see
https://developer.amazon.com/public/solutions/devices/fire-tv/docs/amazon-fire-tv-sdk-frequently-asked-questions
for details

Sharing animated GIF to Twitter makes it static (iOS)

So I'm using LibGDX and RoboVM to convert my Android project to an iOS app. I'm trying to share an animated GIF that the user has just created and saved to their device, here's what I have so far:
public void shareGif(String path)
{
NSData data = new NSData(Gdx.files.absolute(path).readBytes());
NSArray<NSData> nsObjectsToShare = new NSArray<NSData>(data);
ArrayList<String> excludedActivities = new ArrayList<String>();
excludedActivities.add(UIActivityType.Print());
excludedActivities.add(UIActivityType.AddToReadingList());
excludedActivities.add(UIActivityType.PostToVimeo());
excludedActivities.add(UIActivityType.PostToTencentWeibo());
UIActivityViewController share = new UIActivityViewController(nsObjectsToShare, null);
share.setExcludedActivityTypes(excludedActivities);
((IOSApplication)Gdx.app).getUIViewController().presentViewController(share, true, null);
}
Now, this does work. It successfully opens the share window and sends an animated GIF via Mail and Message. Twitter, however, only seems to share a static image (it appears to actually be a JPG when I examine it on my Twitter timeline).
Anyone have any ideas how I can get Twitter to share the ANIMATED version of the GIF? I know I'm not using Objective-C like with typical iOS apps but I'm hoping you can still make out what's going on in the above code.
Was apparently fixed in iOS 8. Just updated my device and it works now. Cool.

lwuit wizard and deploy

I am developing J2ME application with lwuit and Codenameone and created it in lots of time. After I created it I wanted to deploy it in some devices like Nokia, Samsung, LG and etc. that they supported MIDP. So I figure out Nokia devices run it with no error and Samsung and other companies devices have some problems that I can't understand why?!
So I tried different way of creating this application. I used Codenameone wizard with blank theme and manual template and then I tried to deploy it. Well, that right. I got success and it ran in Samsung devices too.
After that I tried to add some forms to "theme" in this appliaction and run it in simulator. I changed some code in my main class like this:
public class Main extends UIBuilder {
private Form current;
public void init(Object context) {
try{
Resources theme = Resources.openLayered("/theme");
UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
}catch(IOException e){
e.printStackTrace();
}
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = findMain();//new Form("Hi World");
//hi.addComponent(new Label("Hi World"));
hi.show();
}
public void stop() {
current = Display.getInstance().getCurrent();
}
public void destroy() {
}
public com.codename1.ui.Form findMain() {
return (com.codename1.ui.Form)findByName("Main", Display.getInstance().getCurrent());
}}
I got error when I ran it.
So this is my questions:
I created one application in Visual Mode of Codenameone.
How can I run it in Samsung and some other devices like Samsung (without opertaing system, just support java or MIDP)?
How can I change my Visual Mode application to Manual Mode in Codenameone?
Thanks in advance.
You are deriving from UIBuilder within the lifecycle class and trying to use a finder method on something that isn't showing yet. There is absolutely no way that your code works in the simulator and it is not the code that was generated by the wizard.
Thanks Shai. For creating I did hard coding and create all of forms one by one.
I implement base Form that derive from com.codenameone.ui.Form and every forms derive from base Form.

I want to run the application in background of mobile without any GUI in mobile using J2ME?

I am developing the network application in which I want to run my J2ME MIDP application in background without any GUI so that is any way to construct the application is such manner.
try this
set your current Display to null. so there will not be any form or alert running on the screen. But however your code will be running in the background.
Display display = Display.getDisplay(this); // here 'this' points to Midlet
display.setCurrent(null);
it easy just have a code of line on any event for example in the click of button
Display.getDisplay (this).setCurrent (null);
and return back the control via
Display.getDisplay (this).setCurrent (mycanvas);
Yes this code works Good,
display = Display.getDisplay(this);
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void hide()
{
Display.getDisplay (this).setCurrent (null);
}
This is will work like, make a button can after clicking it call hide Function, or you call this hide function in constructor so it will hide itself when app start, can you keep unHide statement in appStart() so if you Tab the program then it will unHide app again.
NOTE: you said you are working on Network app, but some mobile will turn off the Internet Connection, when the Mobile screen Turn Off. please check this. and If you found any solution It will be Good to share here.

How to embed jar in HTML

There are a lot of resources on this already but I just can't seem to get it to work. What am I doing wrong? The jar file is at:
http://www.alexandertechniqueatlantic.ca/multimedia/AT-web-presentation-imp.jar
And the code I am using to embed is:
<APPLET ARCHIVE="multimedia/AT-web-presentation-imp.jar"
CODE="ImpViewer.class"
WIDTH=100%
HEIGHT=100%>
</APPLET>
The test page I am using is at:
http://www.alexandertechniqueatlantic.ca/test.php
When I download the jar it runs fine, so I am certain the problem is only with the html embedding. Pleas help!
Also, I get the following error:
java.lang.ClassCastException: ImpViewer cannot be cast to
java.applet.Applet
java.lang.ClassCastException: ImpViewer cannot be cast to java.applet.Applet
The 'applet' is not an applet.
BTW - nice UI. Like the way the red splash fades in to the 'Welcome Introductory Workshop' page. Very smooth.
Launch it from a link using Java Web Start (& please don't try and cram such a beautiful UI into a web page).
If the client insists on the GUI being crammed into a web site then (slap them for me &) try this hack.
/*
<APPLET
ARCHIVE="AT-web-presentation-imp.jar"
CODE="ImpViewerApplet"
WIDTH=720
HEIGHT=564>
</APPLET>
*/
import java.awt.*;
import java.applet.*;
import java.util.*;
public class ImpViewerApplet extends Applet {
public void init() {
setLayout(new BorderLayout());
Window[] all = Window.getWindows();
ArrayList<Window> allList = new ArrayList<Window>();
for (Window window : all) {
allList.add(window);
}
String[] args = {};
ImpViewer iv = new ImpViewer();
iv.main(args);
all = Window.getWindows();
for (Window window : all) {
if (!allList.contains(window) && window.isVisible()) {
if (window instanceof Frame) {
Frame f = (Frame)window;
Component[] allComp = f.getComponents();
Component c = f.getComponents()[0];
f.remove(c);
f.setVisible(false);
add(c);
validate();
}
}
}
}
}
The emphasis is on the word 'hack'.
The Frame will flash onto screen before disappearing.
It will only work at 720x564 px, unlike the java.awt.Frame which was resizable to any size. But then, your '100%' width/height was being a bit optimistic anyway. Some browsers will honour those constraints, others will not.
It took a bit of effort, but your ImpViewer class has the following definition:
public class ImpViewer extends ImWindow
implements Printable, Runnable
{
[...]
ImpViewer is NOT an Applet like it needs to be, but is instead an ImWindow. It should inherit from either Applet or perhaps ImApplet.
At either rate, Andrews idea of using Java Web Start is legit. The app you have looks more like a desktop app.
An Applet is a Java component which handles the right calls to show up embedded in a web page. The product you have (the JAR file) contains everything necessary to run the program; however, it does not have the correct interface (the applet) for running that program embedded in a web page.
Talk to the author of the product (of if that author is not available, look for documentation) and see if a applet interface is available. Perhaps it is only a matter of using a different class name. If it looks like such an interface is not available, then no one has done the necessary work to make it "embeddable" in a web page. Without knowing your product in more detail, it's not easy to determine if the effort to create an Applet interface into the product is easy or not.
If you don't have the source code, then the amount of effort to develop an Applet interface to what you have is even greater than the unknown amount of effort it would have been with the source code.
There are a few products that do allow applications to be viewed and controlled from a web browser, even when the application in question wasn't designed to be embedded in a web page. These products tend to be expensive and proprietary; but, if it is truly mission-critical (and if it makes enough money) then the expense and effort might be bearable. With such a solution, the web browser actually opens a window into a configured "application server" which launches the application in full screen mode every time the connection is established. Yes, it is an odd architecture; however, such an odd architecture exists purposefully as that's really the only way possible to do some things when the application can't run in other environments.
Look to Citrix for such a solution in the event that you can afford it (remember there's extra windows licenses involved) and you can tolerate it's performance and quirks.

Categories