I need my J2ME app to run in the background and still allow the user to use his mobile without problem.
the app still needs to process some events in the background.
I would also like to allow the user to stop the app if he wants to.
How can I accomplish this?
Running a midlet in the background but still processing is not specified in the j2me standard i think. Normaly at the moment your midlet is moved to background the paused method should be called.
But not every vendor implements it that way. Symbian keeps your program running as if there was no change when minimized. At least on the N80 and N90.
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 foreground :-
public void toFront() {
display.setCurrent(previousDisplayable);
}
But Be aware that every device not supports that features.(Works on Nokia s60, SonyEricsson, but not on Nokia s40, Samsung and some others).
This is not always supported, but on the handsets that do, the command is:
Display.getDisplay(theMidlet).setCurrent(null);
If you app is in background then it doesn't receive any events. So I don't know how it can process any event in the background. If its a preload J2ME app then you can work with the handset manufacturer and obtain certain jad attributes to put your midlet in background. You can thus not allow the user to exit the app. You may want to think this use case through.
On the other hand you can do all these in Blackberry apps.
MIDlet.notifyPaused()
Related
I am new to Java programming and I know basic syntax of Java and can write programs.
I want to make desktop application in java that never lets the system sleep. I want that application to run in background and should not disturb the user flow.
I figured out some keyboard keys can be pressed internally which does not affect the flow like F13 F14 not shown to user but can be used internally.
Also I came with this java program that moves the mouse to its same position after some seconds so that system does not sleep.
import java.awt.*;
import java.util.*;
public class Mal{
public static void main(String[] args) throws Exception{
Robot mal = new Robot();
while(true){
mal.delay(1000 * 60);
mal.mouseMove(mouseLoc.x, mouseLoc.y);
}
}
}
I am curious to know how to make desktop app for windows using Java.
Like when user clicks on the app it get activated and keep running in the background until close by the user and it should never let the PC sleep either my moving mouse or by clicking special keys.
Useful links, code and path for development is required.
Thank You!
You could try using java.awt.Robot: https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html
As the documentation notes, this will not work in all environments, because allowing user-space programs to emulate user input is a bit of a security issue.
I am migrating my app target SDK Version to android oreo.
One of the changes which might affect the app is that if the Display size of the device is changed then the app targeting Android Nougat and above will be notified as if there is a change in orientation of the device. Below is the source.
When the device density changes, the system notifies running apps in
the following ways:
If an app targets API level 23 or lower, the system automatically
kills all its background processes. This means that if a user switches
away from such an app to open the Settings screen and changes the
Display size setting, the system kills the app in the same manner that
it would in a low-memory situation. If the app has any foreground
processes, the system notifies those processes of the configuration
change as described in Handling Runtime Changes, just as if the
device's orientation had changed.
If an app targets Android 7.0, all
of its processes (foreground and background) are notified of the
configuration change as described in Handling Runtime Changes.
Now my app has a longrunning process which runs in an Async TSak in an Android Activity. Which means that the App has an AsyncTask which is inside the Activity Code.
I create a Dialog box on the start of the Async Task and hide it when the Async Task has done its work.
Now suppose the users start the task and then goes to the setting and changes the Display Size then return back to my app then the dialog box of the app is gone by the Async Task is still performed till the end which mean that the user might think that the app has finished the task but whereas the app would be actually performing the task. But my Activity is also responding as if restarted except that Async Task is running.
My app's orientation locked to portrait.
How should I handle such a scenario? Any help is appreciated. Thanks!
EDIT: This is the scenario
1) App Start
2) Main Screen
3) Users Press A Button
4) AsyncTask inside Main Screen Started
5) A Dialog Box is shown with the progress of task
6) User Minimizes the App and Goes to setting
7) Changes the Display Size/Density or changes the Font Size of the device
8) My App is called by the OS in such a way as if the device rotation is changed
9) Then the user returns back to the app
10) But the dialog box shown is no more being shown
11) But the async Task is running in the background and is still performing its task
12) The task is actually done but the user thinks that the task is still not done.
In your on_resume() method verify the screen size and the font size have not changed. If they have, adjust, remove and recreate, your dialog box appropriately. This is similar to handling orientation changes while suspended (adding font size as a condition).
EDIT:
Add android:configChanges="fontScale|density" to your manifest file.
static int fontScale = 1;
static int densityDpi = 0;
public void onConfigurationChanged(Configuration newConfig) {
if (newConfig.fontScale != fontScale) {
// destroy and recreate dialog adjusting for font size.
fontScale = newConfig.fontScale;
}
if (newConfig.densityDpi != densityDpi) {
// destroy and recreate dialog adjusting for new screen size.
densityDpi = newConfig.densityDpi;
}
}
It would probably be better to utilize notifications for the async task, since they are unaffected by config changes and are not terminated in low memory conditions.
When you rotate the phone, your activity is actually destroyed and recreated. This includes your dialog. There are two ways to fix it:
1) Turn off this "helpful" functionality. If you don't have different layouts for landscape and portrait its what I'd suggest. Add android:configChange="orientation|resize" to your activity in the manifest.
2) Implement onSaveInstanceState/onRestoreInstanceState and have a variable that says whether or not you need to recreate and relaunch the dialog.
Suggestions:
Try using WorkManager to schedule the task and also ViewModel from the android architecture components which helps to retain the state even after the configuration changes
Sidebar
In order to access the sidebar, the user has the press the long back button.
I was wondering if there are anyways to prevent the user from accessing the Sidebar?
Thanks in advance.
The special behaviour of the back button is a Samsung OS specific, meaning they added it to their flavour of Android.
Thus, i fail to see how you can disable it from user code.
One option (although it would be a bit obtrusive) is to disable the back action completely by overriding and see if it stops Samsungs OS from picking up on the button (probably not as it would be OS level call).
This could be done like this:
#Override
public void onBackPressed() {
}
Remember, like i said, this is a bit intrusive, as you alter the behaviour of a common usage method. And it is also not guaranteed to stop Samsung devices to bring up the menu, as that is likely a call on OS not user-level.
I want to execute a piece of code (say, for example, display a Toast) every time that the app is opened. So far I have managed to do this every time the app is launched by putting the code into my MyApp.java file that extends Application.
However, if I press the homescreen or back out of the app and then go into it, the message doesn't reappear. It only does when I relaunch the app. Any idea how to do this?
EDIT:
basically im asking how to execute code everytime the whole APP is brought to foreground (this can be first time open, after another app was used, after user backed out of app, etc). Where would I place onResume code? It wouldn't be in a particular activity, would it, since I want it to apply when entire app appears in foreground, not just particular activity.
You can try writing that code in your activity's #Override-d onResume() method.
The only way to do this is,
Determine which app is currently in the foreground
.Follow this discussion for getting an idea for the best way to do it.
[Determining the current foreground application from a background task or service
Suppose, if the function name is 'getCurrentForgroundApp()',
You need a service to execute getCurrentForgroundApp(); every one second
(1-second interval is depending on your purpose, can be lower or higher).
Now, you can identify which app is running foreground in every second.
So, check if your app is the one running foreground. If true, then execute the toast or code you need.
This is how app-locker apps showing lock screen over selected apps, whenever they come to the foreground.
You have to use the onResume callback:
Android API
Example of use from previous SO question
In activity class:
#Override
protected void onResume() {
super.onResume();
//your code here
}
In my android app, I want to detect the user's device if the device has keyboard(like motorola milestone) and then show corresponding user interface. How do I do that?
http://developer.android.com/reference/android/content/res/Configuration.html
public int keyboard
The kind of keyboard attached to the device. One of: KEYBOARD_NOKEYS, KEYBOARD_QWERTY, KEYBOARD_12KEY.
If you're asking if there is a mechanism to allow detection of physical keyboard?
yes, Android does have the capability to notify applications of a keyboard open/close event.
You'll have to add detection of configuration changes to your activities. I believe there is a notification that is posted to the activity currently displayed that will indicate the keyboard state (or device configuration change).
http://developer.android.com/guide/topics/manifest/activity-element.html might give more insight in to what you're looking for.