My Apps need to make a call several time according to the user need.
public class callDelay implements Runnable {
#Override
public void run() {
// TODO Auto-generated method stub
for(int count = 0 ; count < (Integer.parseInt(myGate.getNumberOfAttempts().toString())); count++){
try {
Intent openGateNow = new Intent(Intent.ACTION_CALL);
openGateNow.setData(Uri.parse("tel:" + myGate.getGatePhoneNumber().toString()));//to call a phone number must use "tel:"+ the number
startActivity(openGateNow);
} catch (Exception e) {
// TODO: handle exception
}
try {
Thread.sleep(Integer.parseInt(myGate.getCallDelay().toString())*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
callNowFlag = false;
speedVerification = false;
}
}
The problem is that when i call this thread, it ignore the counter and calls over and over again without delays and with no counter limits (i have to stop it my self).
I would like it to call just X number of times and with delay between the calls.
what is wrong with my code?
what reset the counters and delay?
Related
Today I write a program.It works successfully and is almost finished.But I found when I close the Socket,BufferedReaderandPrintWriter,the program always shows ANR.I feel confused about that.Is there any sequence to close the program??
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
try {
mPrintWriter.println("192.168.2.131;"+mSocket.getLocalAddress().toString().replace("/", "")+";byebye");
mPrintWriter.close();
mBufferedReader.close();
mSocket.close();
net_.interrupt(); //This is the thread to send and receive data.
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is my question and I want to eliminate this bug to keep my program perfect.Thanks
Read this Android - how do I investigate an ANR?
And maybe try something like that
#Override
public void onClick(DialogInterface dialog, int which) {
thread = new Thread(new Runnable() {
#Override
public void run() {
try {
mPrintWriter.println("192.168.2.131;"+mSocket.getLocalAddress().toString().replace("/", "")+";byebye");
} catch (IOException e) {
e.printStackTrace();
} finally{
mPrintWriter.close();
mBufferedReader.close();
}
try{
net_.interrupt();
}catch (IOException e) {
e.printStackTrace();
}finally{
mSocket.close();
}
}
});
thread .start();
}
And at the end of your app you should join that thread
thread.join();
I have a problem, I have a for loop and a ProgressDialog and would like to see something like (10/5) where 10 is the total items to be processed by and for the 5 elements are currently being developed. I have this code.
new Thread(new Runnable() {
public void run() {
for(int i=0; i<adapter.getTotalItems().size(); i++) {
try {
index = i;
progressDialog = ProgressDialog.show(MyClass.this,adapter.getTotalItems().size()+"/"+index, );
MyClass.this.runOnUiThread(new Runnable() {
public void run() {
progressDialog.cancel();
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(1*2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
MyClass.this.runOnUiThread(new Runnable() {
public void run() {
progressDialog.dismiss();
}
});
}
}).start();
Don´t cancle the ProgressDialog every Time, just Change the Title like:
mProgressDialog.setTile(adapter.getTotalItems().size()+"/"+index);
That´s it.
I'm trying to create a speech recognition app where the app recieves voice and sends out stuff. I'd like everything that the onEndOfSpeech method to be called to wait a second and then do the entire voice recognition intent to start over again.
public void onEndOfSpeech() {
Log.d("Speech", "onEndOfSpeech");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
Not sure that I am doing this correctly.
Thanks!
This is how it should be
try {
Thread.sleep(3000);
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
} catch (InterruptedException e) {
// it depends on your app logic what to do with InterruptedException, you can process it or rethrow or restore interrupted flag
}
Try this code
protected boolean _active = true;
// time to display the splash screen in ms
protected int _splashTime = 1000;
Thread splashThread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
e.printStackTrace();
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
I have a naive problem, but I confused: I have made application which uses Facebook SDK, and it works good on my device and emulator, and it doesn't work on customer's device. He doesn't get any error or exceptions - when he press button for authorize he will see "loading" message, but progress bar will be closed, and authorization will be canceled. What problem is it? Thank you for anything hints
private void submitExec() {
/* if (SQLiteDbWrapper.getInstance().getBookCount()==0) {
Toast.makeText(this, "A list of books is empty", Toast.LENGTH_LONG).show();
return;
}*/
SQLiteDbWrapper.getInstance().makeFacebook(this, this.getApplicationContext());
if (SQLiteDbWrapper.getInstance().getConnector().getFacebook().isSessionValid()) {
//new SubmitClass().execute();
}
else {
SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {
#Override
public void onAuthSucceed() {
//MyBookDroidActivity.this.executeSubmitClass();
}
#Override
public void onAuthFail(String error) {
}
};
SessionEvents.addAuthListener(listener);
SQLiteDbWrapper.getInstance().getConnector().login();
}
}
It is function for authorizating.
public void makeFacebook(Activity activity, Context context) {
if (mConnector==null||!mConnector.getFacebook().isSessionValid()) {
mConnector=new FacebookConnector(FACEBOOK_APPID, activity, context,
new String[] {"publish_stream", "read_stream", "email"});
}
}
It is function for making FacebookConnector.
Try adding logging:
public void appendLog(String text)
{
File logFile = new File("sdcard/log.file");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
If you can't run some kind of LogCat or collect the stacktrace on the device yourself, you may want to look into:
http://code.google.com/p/microlog4android/
I have 2 buttons that both play an mp3. When the first song is playing I want a click of the second song to stop the first song, and start the song that was clicked. also if the first button is pressed twice the second press resets the song.
basically I don't want the songs to play on top of each other when multiple buttons are pressed.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.placeyouknow:
String url = "http://www.katastro.com/audio/facts/01%20That%20Place%20You%20Know.mp3";
mp = new MediaPlayer();
try {
mp.setDataSource(url);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
case R.id.fallen:
String url1 = "http://www.katastro.com/audio/fallen/01%20Fallen.mp3";
mp = new MediaPlayer();
try {
mp.setDataSource(url1);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Seems like you need to implement a state machine to manage your player (start-stop-restart) instead of creating a new player every time a button is pressed
Here is a sample sound helper class I use for simple apps. I use it to play at most one resource at a time within my app, but you could modify it to play audio from some url instead.
public class Sound
{
private static MediaPlayer mp = null;
/** Stop old sound and start new one */
public static void play(Context context, int resource)
{
stop(context);
mp = MediaPlayer.create(context, resource);
mp.setLooping(false);
mp.start();
}
/** Stop the music */
public static void stop(Context context)
{
if (mp != null)
{
mp.stop();
mp.release();
mp = null;
}
}
}
Whoooaaaaa, losing handles on MediaPlayers left and right.
First of all, for this particular activity, you should only put
mp = new MediaPlayer();
in the onCreate() method of your activity. Delete the two you call in the onClick() method and move it to the onCreate() method.
From there, all you have to do is include a call to mp.reset() right before you call mp.setDataSource() and it should work just like you want it to!
Cheers