I have created 2 threads in my android app but they don't work :(.
final Thread t1 = new Thread() {
// #Override
public void run() {
camera.takePicture(null, null, photoCallback);
Log.e("mediafile", mediaFile+"");
}
};
t1.start();
final Thread t2 = new Thread() {
// #Override
public void run() {
Intent myIntent = new Intent(CameraActivity.this, ModificationActivity.class);
Log.e("le chemin de la photo", "" + mediaFile);
myIntent.putExtra("imagePath", ""+mediaFile);
startActivity(myIntent);
}
};
try {
t1.join();
t2.start();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
My variable is always at null when I start my intent because the first thread is not finished.
I hope you can help me :D
Thanks in advance to all
This is the log :
11-05 14:29:59.936 18216-18216/com.mcm.citadium I/Choreographer﹕ Skipped 72 frames! The application may be doing too much work on its main thread. 11-05 14:30:05.140 18216-18387/com.mcm.citadium E/mediafile﹕ null 11-05 14:30:05.148 18216-18389/com.mcm.citadium E/le chemin de la photo﹕ null 11-05 14:30:05.652 18216-18216/com.mcm.citadium E/fin de photoCallback﹕ /storage/sdcard0/Pictures/MyCameraApp/IMG_20131105_143005.jpg
Well, first of all you don't need to create any threads to capture image - you can run camera.takePicture on main thread safely - it will handle its job in another thread anyway (see documentation).
So, for example you could trigger picture-taking activity in your button onClick handler :
shootButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCamera.takePicture(null, null, photoCallback);
}
});
Your photoCallback would then look something like :
Camera.PictureCallback photoCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// generate file path for new picture
String fileName = "myPicture.jpg";
String mediafile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
.getCanonicalPath() + "/mydir/" + fileName;
// launching new activity to modify picture
Intent myIntent = new Intent(CameraActivity.this,
ModificationActivity.class);
Log.e("le chemin de la photo", "" + mediaFile);
myIntent.putExtra("imagePath", ""+mediaFile);
startActivity(myIntent);
}
};
I think you can use [Handler]: http://developer.android.com/reference/android/os/Handler.html
to schedule the two thread.
Some examples :
http://androidexample.com/Thread_With_Handlers_-_Android_Example/index.php?view=article_discription&aid=58&aaid=83
http://examples.javacodegeeks.com/android/core/os/handler/android-handler-example/
Or [AsyncTask]: http://developer.android.com/reference/android/os/AsyncTask.html which is almost equal to " Handler + Thread + Message + ThreadPool "
Some examples :
http://examples.javacodegeeks.com/android/core/os/asynctask/android-asynctask-example/
AsyncTask Android example
Why is the starting of t2 inside the try-catch? I think if you simply place both starts outside of the try-catch, and then join both threads in it, this should work.
t1.start();
t2.start();
try {
t1.join();
t2.join();
}
catch (Exception e) { e.printStackTrace(); }
Related
I'm trying to connect to a TCP/IP socket in a thread the moment my app is opened, and would like to show a progress dialog while connecting. The problem is that the thread is always executed before the progress dialog is shown, which means that it is shown only after the socket connection has actually happened (or failed).
public static boolean startConnection (Context c) {
boolean[] ret = new boolean[1];
ProgressDialog progressDialog = new ProgressDialog(c);
progressDialog.setMessage("Connecting...");
progressDialog.show();
Thread t = new Thread() {
#Override
public void run() {
try {
clientSocket = new Socket();
clientSocket.connect(new InetSocketAddress(SERVER_IP, SERVER_PORT), 5000);
ret[0] = true;
}
catch (IOException e) {
e.printStackTrace();
ret[0] = false;
}
((Activity)c).runOnUiThread(progressDialog::dismiss);
}
};
t.start();
try {
t.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
return ret[0];
}
And I'm invoking startConnection in the onStart() method:
protected void onStart() {
super.onStart();
if (ConnectionHandler.startConnection(this)) {
goToMainActivity();
}
else {
Toast.makeText(this, "Connection error.", Toast.LENGTH_LONG).show();
}
}
How can I make it so that the progress dialog is shown before the thread actually starts?
t.join(); waits on the UI main thread until second thread t finishes, that's the reason your code is blocking the UI thread and the dialog doesn't appear.
In addition you can't call progressDialog.dismiss(); on the t thread, all code that interacts with the UI must be called on the main thread, you can try tom call it using runOnUIThread() activity method
I have an app that writes to its local storage depending on user actions; said contents need to
be forwarded to another app.
My approach:
create a worker thread with a file observer pointed to local storage
start worker from the apps main activity
worker thread creates and sends intents with updated contents to separate app
I'm not sure (maybe need to open a separate question), but everything created in an activity gets destroyed when the activity is stopped, right? meaning that adding workers, file observers have the same life span as the activity they're defined in, right?
Code:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final String FILE_OBSERVER_WORK_NAME = "file_observer_work";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "Creating file observer worker");
WorkManager workManager = WorkManager.getInstance(getApplication());
WorkContinuation continuation = workManager
.beginUniqueWork(FILE_OBSERVER_WORK_NAME,
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequest.from(APIWorker.class));
Log.i(TAG, "Starting worker");
continuation.enqueue();
final Button button = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i(TAG, "Button clicked!");
String stuffToWriteToFile = getStuff();
String cwd = getApplicationInfo().dataDir;
String stuffFilePath= cwd + File.separator + "stuff.json";
PrintWriter stuffFile= null;
try {
stuffFile = new PrintWriter(stuffFilePath, "UTF-8");
stuffFile.println(stuffToWriteToFile);
stuffFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});
}
#Override
public void onResume(){
super.onResume();
// start worker here?
}
#Override
public void onStart() {
super.onStart();
// start worker here?
}
}
APIWorker.java:
public class APIWorker extends Worker {
public APIWorker(#NonNull Context context, #NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
private static final String TAG = APIWorker.class.getSimpleName();
#NonNull
#Override
public Result doWork() {
Context applicationContext = getApplicationContext();
Log.d(TAG, "Observing stuff file");
FileObserver fileObserver = new FileObserver(cwd) {
#Override
public void onEvent(int event, #Nullable String path) {
if(event == FileObserver.CREATE ||
event == FileObserver.MODIFY) {
String cwd = applicationContext.getApplicationInfo().dataDir;
String stuffFilePath = cwd + File.separator + "stuff.json";
String fileContents;
File observedFile = new File(stuffFilePath);
long length = observedFile.length();
if (length < 1 || length > Integer.MAX_VALUE) {
fileContents = "";
Log.w(TAG, "Empty file: " + observedFile);
} else {
try (FileReader in = new FileReader(observedFile)) {
char[] content = new char[(int)length];
int numRead = in.read(content);
if (numRead != length) {
Log.e(TAG, "Incomplete read of " + observedFile +
". Read chars " + numRead + " of " + length);
}
fileContents = new String(content, 0, numRead);
Log.d(TAG, "Sending intent ");
String packageName = "com.cam.differentapp";
Intent sendIntent = applicationContext.getPackageManager().
getLaunchIntentForPackage(packageName);
if (sendIntent == null) {
// Bring user to the market or let them choose an app?
sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("market://details?id=" + packageName));
}
// sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, fileContents);
sendIntent.setType("application/json");
applicationContext.startActivity(sendIntent);
Log.d(TAG, "Intent sent ");
}
catch (Exception ex) {
Log.e(TAG, "Failed to read file " + path, ex);
fileContents = "";
}
}
}
}
};
fileObserver.startWatching();
return null;
}
}
Looking at the docs:
https://developer.android.com/guide/components/activities/background-starts
there are restrictions as to when activities can be started from the background but also exceptions, namely:
The app has a visible window, such as an activity in the foreground.
meaning (I think?) that as long as the user interacts with the app (MainActivity) the background worker should run, correct? It's stopped if the activity is paused/destroyed, right?
Usually you would use a Service if you have background processing to do that doesn't need user interaction (display or user input). If your app is in the foreground then your Service can launch other activities using startActivity().
Your architecture seems very strange to me. You are using a Worker, which has a maximum 10 minute lifetime. You are starting the Worker which then creates a FileObserver to detect creation/modification of files. It then reads the file and starts another Activity. This is a very complicated and roundabout way of doing things. I have doubts that you can get this working reliably.
Your Activity is writing the data to the file system. It could just call a method (on a background thread) after it has written the file that then forwards the data to another Activity. This would be much more straightforward and has a lot less moving parts.
I don't know exactly how the lifecycle of the Activity effects the Workers. I would assume that they are not directly linked to the Activity and therefore would not stop when the Activity is paused or destroyed.
I also notice that you are writing to a file on the main (UI) thread (in your OnClickListener). This is not OK and you should do file I/O in a background thread, because file I/O can block and you don't want to block the main (UI) thread.
I am making an alarm clock which asks user to do a particular work in order to close the alarm when it rings. It is working fine but the problem is that if the user closes the alarm app from the recent activities while the alarm is ringing, the alarm stops ringing. I want that even if the user clears the app while its ringing, it should not stop ringing. It should only stop once the task given is completed. How can I implement this?
Edit #1: Activity that is called when alarm rings
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(LOG_TAG, "in AlarmAlert");
unlockScreen();
setContentView(R.layout.activity_alarm_alert);
Bundle bundle = this.getIntent().getExtras();
alarm = (Alarm) bundle.getSerializable("alarm");
alarmDatabase = new AlarmDatabase(this);
//Uri uri = alarm.getRingtonePath();
question = (TextView)findViewById(R.id.question);
answer = (TextView) findViewById(R.id.answer);
oldColors = answer.getTextColors();
diff = alarm.getDifficulty().toString();
questionString = GenerateMathsQuestion.generateQuestion(diff);
question.setText(questionString);
actualAnswer = EvaluateString.evaluate(questionString);
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int result = am.requestAudioFocus(focusChangeListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.setLooping(true);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
try {
mediaPlayer.setDataSource(this, Uri.parse(alarm.getRingtonePath()));
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
if(alarm.getIsVibrate()) {
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
long[] pattern = {1000, 200, 200, 200};
vibrator.vibrate(pattern, 0);
}
}
public void closeAlarm(){
Log.v(LOG_TAG, "will now stop");
mediaPlayer.stop();
if(vibrator!=null)
vibrator.cancel();
Log.v(LOG_TAG, "will now release");
mediaPlayer.release();
Log.v(LOG_TAG, "id of ringing alarm: " + alarm.getAlarmId());
alarm.setIsActive(false);
alarmDatabase.updateData(alarm);
cursor = alarmDatabase.sortQuery();
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex(AlarmDatabase.COLUMN_UID));
currentAlarm = alarmDatabase.getAlarm(id);
Log.v(LOG_TAG, "id of next alarm " + id);
if(currentAlarm != null) {
if (currentAlarm.getIsActive() == true) {
currentAlarm.scheduleAlarm(this, true);
break;
}
}
}
this.finish();
}
You should use Services. Take a look at it, that is what you want it. Generally you can make it to run an operation, and a service wont return any result. But it runs indefinitely even when you kill the app from task manager or free RAM.
I suggest this tutorial for reading about services.
UPDATE
Implement your activity with the service in the following way so it can talk with the layout and stops the alarm when required.
public class HelloService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
// Handler that receives messages from the thread
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
#Override
public void handleMessage(Message msg) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// Restore interrupt status.
Thread.currentThread().interrupt();
}
// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job
stopSelf(msg.arg1);
}
}
#Override
public void onCreate() {
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
// If we get killed, after returning from here, restart
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
#Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
}
I am trying to run a section of code every X seconds in an AsyncTask Activity and it is crashing before getting into any print statements. I am not sure why it is crashing, but I also posted the error I am getting. Anyone have any ideas? Thank you so much!
public class AppListener extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... uri) {
final Handler h = new Handler();
final int delay = 5000; //milliseconds
h.postDelayed(new Runnable() {
public void run() {
try {
String msg_received = null;
System.out.println("LISTENING FOR LAST INSTALLED APP");
System.out.println("TRY");
Socket socket = new Socket("85.190.178.23", 5050);
// Get data sent through socket
DataInputStream DIS = new DataInputStream(socket.getInputStream());
System.out.println("DataInputStream Started");
// read data that got sent
msg_received = DIS.readUTF();
System.out.println("Message from server" + msg_received);
// Might not want to close socket, or only the first string will be sent and none after
socket.close();
}
catch (Exception e) {
System.out.println("Did not receive string");
}
h.postDelayed(this, delay);
}
}, delay);
String msg_received = null;
return msg_received;
}
}
MY ERROR
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Here is how I got my loop to execute every X seconds without interfering with my GUI if it is helpful to anyone else: instead of having a separate class, I just posted my code in my main activity right when my app starts
public class MainActivity extends FragmentActivity{
private Thread repeatTaskThread;
// called when the activity is first created
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Go into loop that is repeated every X seconds
RepeatTask();
}
private void RepeatTask()
{
repeatTaskThread = new Thread()
{
public void run()
{
while (true)
{
//Post your code here that you want repeated every X seconds
// My "try" and "catch" statements from above got inserted here
try
{
// Sleep for 5 seconds
Thread.sleep(5000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
};
repeatTaskThread.start();
}
}
Updated: When you're launching AsyncTasks doInBackground, you're launching a background thread. If you want to post AsyncTask's doInBackground with a delay, then you should not use an AsyncTask at all. You should only need to use a Handler with postDelayed, which will create a background thread for you. It looks like in your code, you tried launch a new Thread with a Handler while in AsyncTask's background thread.
Get rid of the AsyncTask altogether, and include this code in your Activity:
final Handler h = new Handler();
final int delay = 5000; //milliseconds
h.postDelayed(new Runnable() {
public void run() {
try {
String msg_received = null;
System.out.println("LISTENING FOR LAST INSTALLED APP");
System.out.println("TRY");
Socket socket = new Socket("85.190.178.23", 5050);
// Get data sent through socket
DataInputStream DIS = new DataInputStream(socket.getInputStream());
System.out.println("DataInputStream Started");
// read data that got sent
msg_received = DIS.readUTF();
System.out.println("Message from server" + msg_received);
// Might not want to close socket, or only the first string will be sent and none after
socket.close();
}
catch (Exception e) {
System.out.println("Did not receive string");
}
h.postDelayed(this, delay);
}
}, delay);
String msg_received = null;
return msg_received;
}
We're working in an android app with the 4 version and we need a mysql connection. We are doing this with a php file which returns the data base result. The following code are working because if we debug the project we can see the correct database result in the the System.out.println. line.
But, if we try to change one text in our app for the result of the database our app can’t run correctly and the error was in the t.setText(txt); line.
Can anybody help me with this problem? Do you have another idea to do the database connection or is it the correct way?
Thanks a lot.
My code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//FUNCIONA NO BORRAR
final httpHandler handler = new httpHandler();
Thread thread = new Thread()
{
#Override
public void run() {
try {
if(true) {
sleep(1000);
String txt = handler.post("http://www.golftipp.com/pruebas_android/app.php");
TextView t = (TextView) findViewById(R.id.textView2);
t.setText(txt);
System.out.println(txt);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
The error
05-30 02:42:50.331 1276-1291/es.softline.connexiodb.app E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-87
Process: es.softline.connexiodb.app, PID: 1276
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:824)
at android.view.View.requestLayout(View.java:16431)
at android.view.View.requestLayout(View.java:16431)
at android.view.View.requestLayout(View.java:16431)
at android.view.View.requestLayout(View.java:16431)
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
at android.view.View.requestLayout(View.java:16431)
at android.widget.TextView.checkForRelayout(TextView.java:6600)
at android.widget.TextView.setText(TextView.java:3813)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.TextView.setText(TextView.java:3646)
at es.softline.connexiodb.app.MainActivity$1.run(MainActivity.java:33)
Non-UI threads cannot access views, use runOnUiThread():
Thread thread = new Thread()
{
#Override
public void run() {
try {
if(true) {
sleep(1000);
String txt = handler.post("http://www.golftipp.com/pruebas_android/app.php");
TextView t = (TextView) findViewById(R.id.textView2);
runOnUiThread(new Runnable() {
#Override
public void run() {
// do UI updates here
t.setText(txt);
}
});
System.out.println(txt);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();