Number picker start changing value for few seconds - android

I would like a numberpicker to start changing the values automaticly for few seconds .
private void changeValueByOne(final NumberPicker higherPicker, final boolean increment) {
Method method;
try {
// refelction call for
// higherPicker.changeValueByOne(true);
method = higherPicker.getClass().getDeclaredMethod("changeValueByOne", boolean.class);
method.setAccessible(true);
method.invoke(higherPicker, increment);
} catch (final NoSuchMethodException e) {
e.printStackTrace();
} catch (final IllegalArgumentException e) {
e.printStackTrace();
} catch (final IllegalAccessException e) {
e.printStackTrace();
} catch (final InvocationTargetException e) {
e.printStackTrace();
}
}
I ve implemented this method and tried
Thread thread = new Thread() {
#Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
changeValueByOne(wp,true);
}
});
}
};
thread.start();
But it is not ok .
I would like to have an effect as a slot machine .

Why can't you simply set the value to an incremented value? Something like this:
private void changeValueByOne(final NumberPicker higherPicker) {
int oldValue = higherPicker.getValue();
higherPicker.setValue(oldValue++);
}
EDIT
Here is the source of the method you're reflecting:
private void changeValueByOne(boolean increment) {
if (mHasSelectorWheel) {
mInputText.setVisibility(View.INVISIBLE);
if (!moveToFinalScrollerPosition(mFlingScroller)) {
moveToFinalScrollerPosition(mAdjustScroller);
}
mPreviousScrollerY = 0;
if (increment) {
mFlingScroller.startScroll(0, 0, 0, -mSelectorElementHeight, SNAP_SCROLL_DURATION);
} else {
mFlingScroller.startScroll(0, 0, 0, mSelectorElementHeight, SNAP_SCROLL_DURATION);
}
invalidate();
} else {
if (increment) {
setValueInternal(mValue + 1, true);
} else {
setValueInternal(mValue - 1, true);
}
}
}
You can see that if mHasSelectorWheel = true, it calls the startScroll method. In other words, it sounds like your NumberPicker has mHasSelectorWheel = false, in which case you're out of luck given your current implementation. It is highly advisable that you look into a custom scrollable number picker widget.

Related

Android: How to wait IntentService for BroadcastReceiver onReceive method

I have IntentService that need to wait in method a() for results of onReceive() of BroadcastReceiver().
For now i use lmao wait(5000)... so it's not too elegant
IntentService:
private boolean methodA() {
try {
synchronized (mLocalBroadcastReceiver) {
mLocalBroadcastReceiver.wait(3000);
}
} catch (InterruptedException e) {
Log.e(TAG,"error, thread interrupted");
e.printStackTrace();
}
if(CONSTANT == true){
return true;
else
return false;
}
BroadCastRecievier:
#Override
public void onReceive(Context context, Intent intent) {
CONSTANT = true //changes somehow between true/false
}
In other words: return value of methodA depends on results of onReceive(). How to synchronize two threads?
Finally i used thread like that:
private void waitForResponse() {
//wait for response
thread = new WaitForStatus();
thread.run();
try {
synchronized (thread) {
thread.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private class WaitForAppStatus implements Runnable {
#Override
public void run() {
while (true) {
if (CONSTANT != -1) {
break;
} else {
try {
wait(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

Get Video Play Time android from videoplayer

I write this function to get video time . This functions works fine but when I backpress then it throws exception .
public void videoTimer ()
{
try
{
running = true;
final int duration = mVideoView.getDuration();
thread = new Thread(new Runnable()
{
public void run()
{
do
{
tvVideoTimer.post(new Runnable()
{
public void run()
{
int time = (duration - mVideoView.getCurrentPosition()) / 1000;
tvVideoTimer.setText(getTimeString(mVideoView.getCurrentPosition()));
}
});
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if (!running) break;
}
while (mVideoView.getCurrentPosition() <= duration);
}
});
thread.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}

How to implement cross process lock in android?

I'm writing a library project for multiple APPs to use. And for some reason, I must make a function mutual exclusion for different APPs, so I need a cross-process lock. But as far as I know, in android APPs can only write to it's own file's directory in internal storage, and external storage is unreliable because some device don't have one. So file lock seems not applicable for me, so is there any other way to implement cross-process lock?
thanks~
If you do not want to (or you can not) use flock or fcntl, maybe you can use LocalServerSocket to implement a spinlock.
For example:
public class SocketLock {
public SocketLock(String name) {
mName = name;
}
public final synchronized void tryLock() throws IOException {
if (mServer == null) {
mServer = new LocalServerSocket(mName);
} else {
throw new IllegalStateException("tryLock but has locked");
}
}
public final synchronized boolean timedLock(int ms) {
long expiredTime = System.currentTimeMillis() + ms;
while (true) {
if (System.currentTimeMillis() > expiredTime) {
return false;
}
try {
try {
tryLock();
return true;
} catch (IOException e) {
// ignore the exception
}
Thread.sleep(10, 0);
} catch (InterruptedException e) {
continue;
}
}
}
public final synchronized void lock() {
while (true) {
try {
try {
tryLock();
return;
} catch (IOException e) {
// ignore the exception
}
Thread.sleep(10, 0);
} catch (InterruptedException e) {
continue;
}
}
}
public final synchronized void release() {
if (mServer != null) {
try {
mServer.close();
} catch (IOException e) {
// ignore the exception
}
}
}
private final String mName;
private LocalServerSocket mServer;
}

Redraw view with intervals from thread

I need animate menu in custom view. It must be redrawed with intervals some times(about 10), but it redraws after thread stopped.
public void menuShift() {
Runnable runnable = new Runnable() {
public void run() {
while (TablesActivity.this.view.menuShifting) {
try {
Thread.sleep(100) ;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TablesActivity.this.view.timerRefresh() ;
TablesActivity.this.view.postInvalidate() ;
}
}
} ;
this.menuShiftThread = new Thread(runnable) ;
this.menuShiftThread.run() ;
}
this.menuShiftThread.run(); is the problem, you need
this.menuShiftThread.start()
to actualy start a new thread.

Delay super.onbackpressed in android

I made a script which detects if there's internet connection:
public static boolean isOnline() {
try {
InetAddress.getByName("google.hu").isReachable(3);
return true;
} catch (UnknownHostException e){
return false;
} catch (IOException e){
return false;
}
}
If there's no internet the app will warn the user and the quit! But how to delay super.onBackPressed for 20 seconds? :)
this.toast1 = new Toast(getApplicationContext());
toast1.setGravity(Gravity.CENTER, 0, 0);
toast1.setDuration(20000);
toast1.setView(layout);
toast1.show();
super.onBackPressed();
Thread delayThread= new Thread(new Runnable()
{
#Override
public void run()
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
activityInstance.this.runOnUiThread(new Runnable()
{
#Override
public void run()
{
super.onBackPressed();
}
});
}
});
delayThread.start();
The easiest is to create a Handler in onCreate()and use postDelayed() with a Runnable that calls super.onBackPressed()

Categories

Resources