Android Process Function Not Working On Background - java

As you can see in the pictures, toast line is working properly but process function not working another activity.
This code line does not workking on background, only work on Activity.
Process p = Runtime.getRuntime().exec("input touchscreen tap 500 500");
My purpose is touch the different screen, EX:WhatsApp or any app.
btndene.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Thread(new Runnable() {
#Override
public void run() {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.teamviewer.quicksupport.market");
startActivity(launchIntent);
for(int i = 0; i<10; i++) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
update(i);
}
}
private void update(final int i) {
runOnUiThread(new Runnable() {
#Override
public void run() {
//txt.setText("Hello"+i);
// komut = "input keyevent 30";
//calistir(komut);
Toast.makeText(MainActivity.this,"Bastin",Toast.LENGTH_SHORT).show();
try {
p = Runtime.getRuntime().exec("input touchscreen tap 500 500");
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}).start();

Due to security reasons it wont work on other apps...You can use any UI automation frameworks like Robotium,Expresso to do this..

Try using monkeyrunner instead. It works with any app.

Related

SwipeRefreshLayout animation frozen

I am trying to use swipe refresh layout to update some data.
All works fine but a small bug that I can't seem to solve
When I pull to refresh, the animation gets frozen.
I have implemented it this way:
myRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// Do some stuff
// Sleep as a demonstrator of the issue
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
myRefreshLayout.setRefreshing(false);
}
});
and also tried this:
myRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
myRefreshLayout.post(new Runnable() {
#Override
public void run() {
// Do some stuff
// Sleep as a demonstrator of the issue
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
myRefreshLayout.setRefreshing(false);
}
});
}
});
}
});
but the animation remains frozen until the everything is done, and then it just vanishes (because it hits the setRefreshing(false))
After searching a little, I thought it could be the UI that is waiting for the stuff to finish, so I tried to implement it this way:
myRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
new Thread(new Runnable() {
#Override
public void run() {
// Do some stuff
// Sleep as a demonstrator of the issue
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
myRefreshLayout.setRefreshing(false);
}
});
}
});
}
});
}
What happens in this case is that the refresh indicator never vanishes, so I guess it is not calling the runOnUiThread
I tried the same thing with the main looper (handler) instead of the runOnUiThread with the same effect.
Is there any neat way of implementing this?
Am I missing some detail?
I tried the steps described above and I also looked on Stack Overflow for similar issues to no avail.
Thanks!
It's right to do refreshing on background thread,and setRefreshing(false) on main thread. The problem is you forgot to call start() after new thread.
myRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
new Thread(new Runnable() {
#Override
public void run() {
// Do some stuff
// Sleep as a demonstrator of the issue
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
myRefreshLayout.setRefreshing(false);
}
});
}
}).start();// start the thread here
}
});
}

How to set a rectangular focus area on Google Vision Camera Source?

I'm currently having problems with Google vision. There is nothing wrong with the library actually, its works great. All what I'm trying to accomplish now is set a rectangular area where the CameraSource will focus only on. The aim is try to capture text within that particular rectangular box only. I've tried many examples on StackOverflow but they all didn't seem to work. My current working code with the Google Vision ( 11.8.0 ) is
cameraView = findViewById(R.id.surfaceview);
output = findViewById(R.id.output);
TextRecognizer textRecognizer = new TextRecognizer.Builder(ScanVoucher.this).build();
if(!textRecognizer.isOperational()) {
show_alert("Text Recognition not supported on this device");
} else {
cameraSource = new CameraSource.Builder(ScanVoucher.this, textRecognizer)
.setAutoFocusEnabled(true)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedFps(2.0f)
.setRequestedPreviewSize(300, 300)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
cameraSource.start(cameraView.getHolder());
} catch (IOException e) {
show_alert("Unable to access camera");
} catch (SecurityException e) {
finish();
} catch(Exception e) {
//Kill
finish();
}
}
#Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
cameraSource.stop();
}
});
textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
#Override
public void release() {
}
#Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
final SparseArray<TextBlock> items = detections.getDetectedItems();
if(items.size() != 0) {
output.post(new Runnable() {
#Override
public void run() {
StringBuilder builder = new StringBuilder();
for(int i = 0; i < items.size(); i++) {
TextBlock item = items.valueAt(i);
builder.append(item.getValue());
}
try {
output.setText(builder.toString());
} catch (Exception e) {
output.setText(e.getMessage());
}
}
});
}
}
});
}
As I said, everything works great. Wondering how I could set the rectangular box like preview area so that the camera only captures text with the box just like the QR or bar code scanner apps. Thanks in advance.
Please dont abandon project.. You can Reset your .setRequestedPreviewSize(80, 24) to fit.
for me, this only captures the first text line. i hope you see this

New activity after 100% in ProgressDialog

I've got this code:
#Override
public void onClick(View v) {
progressDoalog = new ProgressDialog(Hack.this);
progressDoalog.setMax(100);
progressDoalog.setMessage("Its loading....");
progressDoalog.setTitle("ProgressDialog bar example");
progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDoalog.show();
new Thread(new Runnable() {
#Override
public void run() {
try {
while (progressDoalog.getProgress() <= progressDoalog
.getMax()) {
Thread.sleep(200);
handle.sendMessage(handle.obtainMessage());
if (progressDoalog.getProgress() == progressDoalog
.getMax()) {
progressDoalog.dismiss();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
Handler handle = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
progressDoalog.incrementProgressBy(1);
}
};
});
}
}
Where can I add a code to open new activity when the ProgressDialog will be at 100%? Which and where exactly? Thanks for your help!
You can't start an Activity from a Dialog, but what you can do is start the Activity from the old one using a OnDismissListener.
Take a look at the documemtation :
https://developer.android.com/reference/android/content/DialogInterface.OnDismissListener.html
I haven't noticed but you can check the progress in your Handler, check if it's 100%, dismiss the dialog and start the new Activity, remember that you gotta do this on the UI thread

Starting a thread with a button

I'm programming a small android app in Java/eclipse.
In one part of my app i need a thread, as i build in the following way:
protected void onResume() {
super.onResume();
// we're going to simulate real time with thread that append data to the graph
new Thread(new Runnable() {
#Override
public void run() {
// we add 100 new entries
for (int i = 0; i < 100; i++) {
runOnUiThread(new Runnable() {
#Override
public void run() {
addEntry();
}
});
// sleep to slow down the add of entries
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// manage error ...
}
}
}
}).start();
}
Evertything works fine so far. But now i want to start that thread not automatically. I want to handle ".start()" with a button.
How can i realize it?
I'm very new to Java and Android.
Thanks in Advance!
You can use Handler with Runnable instead of your Thread idea, Check out the following code, it server your purpose,
private Handler broadcastHandler;
private Runnable broadcastRunnable;
protected void onResume() {
super.onResume();
broadcastRunnable = new Runnable() {
#Override
public void run() {
// Your UI related operations
runOnUiThread(new Runnable() {
#Override
public void run() {
addEntry();
}
});
// Add some delay
broadcastHandler.postDelayed(broadcastRunnable, 1000);
}
}
public void onButtonClick(View view) {
broadcastHandler.postDelayed(broadcastRunnable, 1000);
}

Using Sleep(int ms) in onClick(View arg0)

I am working on this android application:
A Button and a textField, the button click change the text of the textField.
So i want to add some animation to the button click by changing the text character by character and wait 100 millisecondes before adding each character.
But when I run the application and press the button, all the text appear after the sleep time without any animation :p
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for(int i=0 ; i<str.length() ; i++)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
txt.append(String.valueOf(str.charAt(i)));
}
}
});
you do not want to sleep on the main-thread. Better use postDelayed
you block ui thread now and you only see the result of onClick. you should not call sleep in ui thread. this is not best, but it should work
#Override
public void onClick(View arg0) {
final Handler handler = new Handler();
new Thread() {
public void run() {
for (int i = 0; i < str.length(); i++) {
final int _i = i;
try {
Thread.sleep(100);
} catch (Exception e) {
}
handler.post(new Runnable() {
public void run() {
txt.append(String.valueOf(str.charAt(_i)));
}
});
}
}
}.start();
}

Categories

Resources