guys. I am trying to use custom camera and found one problem: i have delay for 1 second between camera.takephoto and callback (onTakePicture). But why, because i havent any delay in default camera, when i take some photos, it works very faster, thanks.
Just use
try{
Thread.sleep(1000);
}catch(Error e){
}
Related
I want to change the device's background each specific interval (30 seconds for example) . I've been searching for days for a similar project or tutorial but I didn't find anything helpful . As I guess the app that I'm going to code will be a service since I want it to run in the background . I have the background images included in the drawable folder . So can any one help ?? and thanks in advance
There is a great application that does this and it's opensource. It's called Muzei - link created by Roman Nurik. Once a day, this application gets wallpapers from Internet and change your background. You can totally get this code and modify the frequency of the background change, and strip all the web calls and redirect to your internal images
You can also code a plugin for Muzei. You will only have a little part of the code to make it work. It will be a lot easier. But, in the other hand, you will have less control on the time between every wallpaper change.
For your code it will be a mix between :
AlarmManager run every hour
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += remainingMilisecondsToTopHour;
long a=c.getTimeInMillis();
// Schedule the alarm!
AlarmManager am = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME,
c.getTimeInMillis(), 1*60*60*1000, sender);
and Changing Wallpaper. Do not forget to add the permission of changing background in your manifest
// to set a background we need to use bitmap
InputStream is = getResources().openRawResource(R.Drawable.myImage);
// we set the phone background to that image.
Bitmap bm = BitmapFactory.decodeStream(is);
try {
getApplicationContext().setWallpaper(bm);
// add permission of background from manifest file
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Using a service in background all the time will use way more battery of your device.
Run one task every hour if you still want to use a service ;)
Finally you should register to the broadcast event Screen On/Off to avoid changing wallpaper while the device is idle and draining the battery for nothing
What I am trying to accomplish is browsing to a page, waiting for something to load and then taking and saving a screenshot.
The code I already have is
WebDriver driver = new FirefoxDriver();
driver.get("http://www.site.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("/home/Desktop/image.png"));
} catch (Exception e) {
e.printStackTrace();
}
driver.close();
The reason I need to wait, even if the page is loaded is because it'll be loaded but on the site the content I'd like to take a picture of loads after a few seconds. For some reason the page is not waiting, is there another method that I can use to get the driver/page to wait for X amount of seconds?
You can locate an element that loads after the initial page loads and then make Selenium wait until that element is found.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ID")));
That wouldnt really be a selenium specific thing. You just want java to sleep for a bit after loading the page but before taking the screenshot.
Thread.sleep(4000);
put that after your driver.get statement.
If you want to delay a certain number of seconds, rather than to respond as soon as possible, here is a function for pause similar to what selenium IDE offers:
public void pause(Integer milliseconds){
try {
TimeUnit.MILLISECONDS.sleep(milliseconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
source
The most simple of all.
Just try this and forget rest! The equivalent of this code can be used in any language.
I am writing this in python.
import time
time.sleep(2)
this will make the compiler go to sleep for 2 seconds.
Just in case it will help somebody, you should always try to avoid implicit waits and especially Thread#sleep as much as you can. If you do Thread.sleep(10), your code will always wait for 10 seconds even in case your page is ready after 1 sec. So this can slow your tests substantially if you use this often.
Better way is to use ExplicitWaits which you means you will wait exactly as long as some action happens or some element gets rendered on the page. So in your case, I would use explicit wait to check whether is everything loaded and then take a screenshot.
const { By, until } = require('selenium-webdriver');
this.wait = async function (amount: number) {
try {
await this.driver.wait(
until.elementLocated(By.css('[data-test-id="does-not-exist"]')),
amount,
'Looking for element'
);
} catch (e) {
console.log('waiting')
}
We look for a css identifier that isn't there for x amount of seconds. This is typescript btw. This would be a method on some relevant class, or a function by itself. Use it like this
const button = await this.findByCSSSelector('[data-test-id="get-quote-button"]')
const actions = this.driver.actions({ bridge: true });
await actions.move({origin: button }).perform();
// Small pause to observe animation is working correctly in all browsers
await this.wait(700)
const carat = await this.findByCSSSelector('[data-test-id="carat"]');
This waits for .7 of a second so that you can see that whatever animation is working in your functional tests.
I'm using a Service that displays a view using WindowManager, and animation occurs every time I change the view's size using
windowManagerLayoutParams.height = newHeight;
((WindowManager) getSystemService(WINDOW_SERVICE)).updateViewLayout(mMainLayout, windowManagerLayoutParams);
If I disable manually the scale animations, no animation occurs.
Scale animation disabled manually like so:
http://www.cultofandroid.com/11143/android-4-0-tip-how-to-find-and-disable-animations-for-a-snappier-experience/
Is there a way to disable the window scale animations for my application programmatically?
I just had this same problem while working on a system overlay in the SystemUI package and decided to dig through the source to see if I could find a solution. WindowManager.LayoutParams has some hidden goodies that can solve this problem. The trick is to use the privateFlags member of WindowManager.LayoutParams like so:
windowManagerLayoutParams.privateFlags |= 0x00000040;
If you look at line 1019 of WindowManager.java you'll see that 0x00000040 is the value for PRIVATE_FLAG_NO_MOVE_ANIMATION. For me this did stop window animations from occurring on my view when I change the size via updateViewLayout()
I had the advantage of working on a system package so I am able to access privateFlags directly in my code but you are going to need to use reflection if you want to access this field.
As #clark stated this can be changed using reflection:
private void disableAnimations() {
try {
int currentFlags = (Integer) mLayoutParams.getClass().getField("privateFlags").get(mLayoutParams);
mLayoutParams.getClass().getField("privateFlags").set(mLayoutParams, currentFlags|0x00000040);
} catch (Exception e) {
//do nothing. Probably using other version of android
}
}
Did you try Activity#overridePendingTransition(0, 0)?
Check out the documentation:
Call immediately after one of the flavors of startActivity(Intent) or finish() to specify an explicit transition animation to perform next.
I am trying to write some Activity tests for an app, and one particular scenario that I want to test is that when I click a certain button, the Activity view updates accordingly. However, clicking the button causes a somewhat long running asynchronous task to start and only after that task is completed does the view change.
How can I test this? I'm currently trying to use the ActivityInstrumentationTestCase2 class to accomplish this, but am having trouble figuring out how to have the test 'wait' until the asynchronous part of the button click task is complete and the view updates.
The most common and simplest solution is to use Thread.sleep():
public void testFoo() {
TextView textView = (TextView) myActivity.findViewById(com.company.app.R.id.text);
assertEquals("text should be empty", "", textView.getText());
// simulate a button click, which start an AsyncTask and update TextView when done.
final Button button = (Button) myActivity.findViewById(com.company.app.R.id.refresh);
myActivity.runOnUiThread(new Runnable() {
public void run() {
button.performClick();
}
});
// assume AsyncTask will be finished in 6 seconds.
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
assertEquals("text should be refreshed", "refreshed", textView.getText());
}
Hope this helps.
If you're using Eclipse, you could use the debugger by setting a breakpoint in the code that updates the view. You could also set some breakpoints in the long running task to watch and ensure that all your code is executing.
An alternative, write some log or console outputs in your long-running task and the view updater code, so you can see the progress without interrupting the thread by a debugger.
As a piece of advise, if its a long-running process, you should be showing a progress bar of some description to the user, so they aren't stuck there thinking "Is something happening?". If you use a progress bar with a maximum value, you can update it in your long-running task as it is running, so the user can see the activity going from 10% to 20%... etc.
Sorry if you were expecting some kind of jUnit-specific answer.
I ended up solving this by using the Robotium library's Solo.waitForText method that takes a string and timeout period and blocks until either the expected text appears or the timeout occurs. Great UI testing library.
Can someone please help me out. I have an application that creates a file to be processed by an external application. I need to somehow delay my code to wait until there is a file created from the external application. But I am having issues finding anything that cause a delay in the Java.
Thanks in advance
Pretty rudimentary and crude but technically, Thread.sleep() induces delay.
As per comment, simple, but crude:
File f = new File("your-file.txt");
for (;;)
{
try
{
if (f.isFile())
{
break;
}
}
catch (Exception e)
{
e.printStackTrace();
// Or some other appropriate
// handling of the exception.
}
try
{
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
}
delay my code to wait until there is a file created
It is better to activate methods in your code when something happens. To do that I would suggest a separate Thread that runs and checks the condition, then calls your apps. method if needed. For an app. with a GUI, this would typically be achieved using a Swing Timer, but I believe it can also be achieved using a java.util.Timer.
you can try
while(!file.exists());
Instead of polling for a file change with a timer, you could just use the Java file API and wait for a file modification event. Here are the docs:
http://docs.oracle.com/javase/tutorial/essential/io/notification.html
and here is a small example:
http://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java