I wrote simple application, for my Galaxy SII, for permanent connection with remote server. Ones for 5 second it sends and receives data. While application is working nobody can't call to me. Network answers him - interlocutor is unavailable.
The same happend me when I use mail client like K-9. It doesn't matter I use GPRS or 3G connection.
What is a main rule (if is it?) to construct internet application to avoid this problem (I mean problem with ordinary incomming phone connection)?
My ordinary code for sending data (in remote service) is like this:
While (condition)
{
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
out.write(data + "\n");
out.flush();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Regards,
Artik
First, if you want to wait for 5 seconds you should use Thread.sleep(5000); not Thread.sleep(500); which is half a second.
Second, consider sending your data to the server using Timer instead of Thread.sleep() and while(condition)
After modifying your code with the above suggestions, try to test from the emulator and simulate a phone call.
Related
I have a service where I need that Jsoup connect to an URL, and the service will launched whit a AlarmManager; where the alarmManager launch the service, and the app closes
in my service I got this code inside an AsyncTask
Document document = null;
try {
document = Jsoup.connect(URL).get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Elements paragraph = document.select("p");
String text = paragraph.text();
And in the manifest I got
<service android:name="com.example.program.serviceprogram"
android:process=":remote"></service>
Somebody Knows why the application is closes when Jsoup is executed in the service?
You could enable the developer mode(USB debugging) in your tablet and connect that as a test device instead of virtual device. Then you'll get the exception trace and could find the root cause from that.
You can find more details on
http://www.developer.com/ws/android/connecting-your-android-device-to-eclipse.html
http://developer.android.com/tools/device.html
http://code.tutsplus.com/articles/connecting-physical-android-devices-to-your-development-machine--mobile-12376
I'm working with connecting and retrieving data from IP in android. To check whether IP is online before retrieving data, I'm pinging IP using InetAddress.getByName(hostName).isReachable(20000). It's working fine for LAN, But for WAN, getting timeOut.
Any help is appreciated!!
try {
boolean b = InetAddress.getByName(hostname).isReachable(40000);
}
catch (Exception e)
{
e.printStackTrace();
}
you have to add this 20000 milllisecod to 40000 milliseconds..
so this is paramter if you not reach your network at 20000 then you got timeout..
I hope its useful to you.
I am having an Android-App [1] which I partly want to port to google-glass - this app uses bluetooth rfcomm. Now I am facing the following problem: when I use my connection code I see a pairing dialog on glass - showing me a large number and asks for a tap to confirm. But this is strange - as I usually have to enter my 4 digit pin on the phone - also I am getting auth problems ( smells like it is caused by not letting me enter the PIN )
Anyone using bluetooth-rfcomm on google-glass?
[1] https://github.com/ligi/DUBwise
I was having the exact problem like this! In this post I put my complete solution to this problem.
But basically the pairing is done like this:
In the BroadcastReceiver
if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
BluetoothDevice device = ListDev.get(selectedDevice);
byte[] pinBytes = getStrFromName(device.getName(),7,11).getBytes(); // My devices had their own pin in their name, you can put a constant pin here or ask for one...
try {
Method m = device.getClass().getMethod("setPin", byte[].class);
m.invoke(device, pinBytes);
try {
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
So the pin is set automatically in this example, but you can always ask for a pin to the user.
Hope it helps!
I have an application that shows, plays and delete videos. I want to know that how to pause the current thread or processing while the media scanner is running. it takes about 4 to 7 seconds for media scanner to complete its activity..
any suggestions
If I understood you correctly you just need to implement the OnScanCompletedListener.
If not, add some code to clarify your question.
So if you want to know when the media scanner is done you have to register BroadcastReceiver which will listen for ACTION_MEDIA_SCANNER_FINISHED. Here is good presentation that gives good explanation of Intent, IntentFilter and BroadcastReceiver.
I use this code:)
while(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
System.out.println("wait sdcard mount.");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am trying to toggle the microphone of Android mobile. App. freezes if I run the mute and unmute in a loop, With a delay of say 500ms. Is there a specific reason mobile behaves this way? (Motorola Droid is not even able to mute the phone) however all other mobiles are able to do it.
This runs in a loop for 20 times
audioService.setMicrophoneMute(true);
if(audioService.isMicrophoneMute())
{
Toast.makeText(getBaseContext(), "MUTED", 1).show();}
try {
Thread.sleep(1000,90);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
audioService.setMicrophoneMute(false);
if(!audioService.isMicrophoneMute()){
Toast.makeText(getBaseContext(), "Un MUTED", 1).show();
It was running on main thread, works fine once I spawned a new thread.