I am using handler to get GCM value
I want to update this value in my database
so I call AsyncTask from the handler
but I get this Error
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
I checked other solutions they said I have to put the code in the run() section which I already do..
This is the code,
private void GetGCM(final String UserID) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
GCMHelper gcmRegistrationHelper = new GCMHelper(getApplicationContext());
String gcmRegID = "";
gcmRegID = gcmRegistrationHelper.GCMRegister("123456");
// Update using Web Service
try {
UpdateGCMWSTask updateGCMWSTask = new UpdateGCMWSTask();
updateGCMWSTask.execute(UserID, gcmRegID);
// ************ HERE IS THE ERROR ***********************
}catch (Exception e)
{
e.printStackTrace();
}
} catch (Exception bug) {
bug.printStackTrace();
}
}
});
thread.start();
}
Add Looper.prepare() and Looper.loop() in you code, like this:
private void GetGCM(final String UserID) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
Looper.prepare();
GCMHelper gcmRegistrationHelper = new GCMHelper(getApplicationContext());
String gcmRegID = "";
gcmRegID = gcmRegistrationHelper.GCMRegister("123456");
// Update using Web Service
try {
UpdateGCMWSTask updateGCMWSTask = new UpdateGCMWSTask();
updateGCMWSTask.execute(UserID, gcmRegID);
// ************ HERE IS THE ERROR ***********************
}catch (Exception e)
{
e.printStackTrace();
}
Looper.loop();
} catch (Exception bug) {
bug.printStackTrace();
}
}
});
thread.start();
}
You can't create asynctask inside a thread. There are few ways to handle it:
Create a new handler.
Call function runOnUIThread of activity.
Using broadcast.
Related
I want to use JRuby to run Ruby scripts.
However, I'd like it so that if a script takes longer than t seconds, it will automatically be closed.
Here's my attempt:
ScriptingContainer ruby = new ScriptingContainer();
int t = 3;
new Thread() {
#Override
public void run() {
try {
Thread.sleep(t * 1000); // Timeout
System.out.println("Timeout passed.");
ruby.terminate(); // This has no effect?
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
Object output = ruby.runScriptlet(scriptWithAnInfiniteLoop);
ruby.terminate(); // Terminate without timeout, at the end of the script
Here's an answer using the deprecated Thread.stop():
ScriptingContainer ruby = new ScriptingContainer();
int t = 5;
String script = content;
Thread runner = new Thread() {
#Override
public void run() {
try {
ruby.runScriptlet(script);
ruby.terminate(); // Close normally.
} catch (Exception e) {
ruby.terminate(); // Close if JRuby crashes.
}
}
};
Thread killer = new Thread() {
#Override
public void run() {
try {
Thread.sleep(t * 1000);
runner.stop();
ruby.terminate(); // Close forcefully.
} catch (Exception e) {}
}
};
runner.start();
killer.start();
See this article on why it is deprecated: https://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
Since I am using a deprecated method I won't mark this as an official answer.
The idea is for my runnable to run every minute.
Instead, it runs in roughly about 20 seconds and I have no idea why.
Below is the code:
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
try{
//Post from Queue & update post
if (NetworkUtils.isConnected()) {
//post from queue
try {
postHelper.postFromQueue();
} catch (IOException e) {
e.printStackTrace();
}
//Update posts
postHelper.updateSolicitations();
}
}
catch (Exception e) {
// TODO: handle exception
}
finally{
//also call the same runnable to call it at regular interval
handler.postDelayed(this, 60000);
}
}
};
I don't know if it's relevant but it's onCreate method of MainActivity.
Maybe you consider using ScheduledExecutorService
public static void main(String[] args) {
ScheduledExecutorService execService
= Executors.newScheduledThreadPool(5);
execService.scheduleAtFixedRate(()->{
//The repetitive task, say to update Database
System.out.println("hi there at: "+ new java.util.Date());
}, Delay, Rate, TimeUnit.MILLISECONDS );//TimeUnit.MILLISECONDS is time unit
}
Use a scheduled where you define a quartz cronjob that is then triggered whenever you defined it.
you can do something like every minute or second or every day at 3 o'clock
Simple Quartz/Cron job setup
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
try{
//Post from Queue & update post
if (NetworkUtils.isConnected()) {
//post from queue
try {
postHelper.postFromQueue();
} catch (IOException e) {
e.printStackTrace();
}
//Update posts
postHelper.updateSolicitations();
}
}
catch (Exception e) {
// TODO: handle exception
}
}
}, 60000);
I'm using CloudBoost for Android application and i had some problems when I try to save save a data in a table, I'm not getting any results. This is my code:
CloudApp.init("*****", "*****");
...
new Thread(new Runnable() {
#Override
public void run() {
CloudObject obj = new CloudObject("User", "1uvSDThQ");
Log.e("LOG", "1"); //Already this is not shown
try {
obj.set("color", "#000000");
obj.setAcl(new ACL());
obj.save(new CloudObjectCallback() {
#Override
public void done(final CloudObject x, final CloudException e) {
if(e != null)
//error
Log.e("LOG", "Errore");
if(x!=null)
//cloudObject
Log.e("LOG", "FATTO");
}
});
} catch (CloudException e) {
e.printStackTrace();
}
}
});
Your log isn't shown because you made a Thread without calling start().
Android typically uses Asynctask anyway, but I'm not sure why you really need a Thread with this library... That save method looks asynchronous
CloudApp.init("*****", "*****")
CloudObject obj = new CloudObject("User", "1uvSDThQ");
Log.e("LOG", "1");
try {
obj.set("color", "#000000");
obj.setAcl(new ACL());
obj.save(new CloudObjectCallback() {
#Override
I am using an actionListener to trigger an sequence of events and ultimatley this code is called:
public class ScriptManager {
public static Class currentScript;
private Object ScriptInstance;
public int State = 0;
// 0 = Not Running
// 1 = Running
// 2 = Paused
private Thread thread = new Thread() {
public void run() {
try {
currentScript.getMethod("run").invoke(ScriptInstance);
} catch(Exception e) {
e.printStackTrace();
}
}
};
public void runScript() {
try {
ScriptInstance = currentScript.newInstance();
new Thread(thread).start();
State = 1;
MainFrame.onPause();
} catch (Exception e) {
e.printStackTrace();
}
}
public void pauseScript() {
try {
thread.wait();
System.out.println("paused");
State = 2;
MainFrame.onPause();
} catch (Exception e) {
e.printStackTrace();
}
}
public void resumeScript() {
try {
thread.notify();
System.out.println("resumed");
State = 1;
MainFrame.onResume();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopScript() {
try {
thread.interrupt();
thread.join();
System.out.println("stopped");
State = 0;
MainFrame.onStop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The runnable is created and ran, however, the problem occurs when I try to use the any of the other methods, they lock my UI. (I'm assuming this is because im running this on the EDT) Does anyone know how to fix this?
That's not how you use wait and notify. They need to be executed on the thread that you are trying to pause and resume. Which means you need to send a message to the other thread somehow. There are various ways to do this, but the other thread needs to be listening for this message, or at least check for it occassionally.
...
Thread showWordThread = new Thread() {
public void run() {
try {
sleep(config.delayTime * 1000);
} catch (Exception e) {
System.out.println(e.toString());
}
this.run();
}
};
showWordThread.run();
}
...
It had run for about 5 minutes before error occured:
Exception in thread "Thread-2" java.lang.StackOverflowError.
Why?
I had tried this:
Thread showWordThread = new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(config.delayTime * 1000);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
});
showWordThread.start();
But error still occured.
Others have explained that you should use a while loop instead. You're also trying to call the run method inside your anonymous class declaration. Additionally, you should call start, rather than run - when the new thread has started, it will call run automatically. I'd actually suggest implementing Runnable rather than extending Thread, too. So you want:
Thread showWordThread = new Thread(new Runnable() {
#Override public void run() {
while (someCondition) {
try {
Thread.sleep(config.delayTime * 1000);
// Presumably do something useful here...
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
});
showWordThread.start();
Alternatively, consider using a Timer or ScheduledExecutorService.
You are calling run method as recursively. Java holds call information(such as parameters) in stack memory so when you are calling a method recursively and there isn't any end point, stack memory will consumed and StackOverflow exception throws.
Maybe you want increasing Heap Size of JVM but this solution don't solve your problem and StackOverflow will occurred .
I guess you want run a thread continually. I recommend following code:
Thread showWordThread = new Thread()
{
public void run()
{
try
{
sleep(config.delayTime * 1000);
}
catch (Exception e)
{
System.out.println(e.toString());
}
// this.run(); this snnipet code make error
}
};
showWordThread.run();
}
Don't call run() from within the run() method. That'll definitely produce a stack overflow because you keep reentering the same method with no exit condition. Instead use a while loop.
Thread showWordThread = new Thread() {
public void run() {
while(condition) {
try {
sleep(config.delayTime * 1000);
} catch (Exception e) {
System.out.println(e.toString());
}
}
};
showWordThread.start();
}
Your code have infinity recursive, you should change the code to:
Thread showWordThread = new Thread() {
public void run() {
while (true) {
try {
Thread.sleep(config.delayTime * 1000);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
};
showWordThread.start();
Your function calls itself each time you run it.
That results in a stack overflow.
Maybe because you call run method (this.run()) from itself?