jsoup in a service android - android

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

Related

Android Nougat receive via socket error

I have made an application that send and receive data via socket it works in Android os < 7.0.0 but when i launche this application in android nougat(7.0.0) it display an error when receiving replay it display android.os.NetworkOnMainThreadException error at lign (dataOutputStream.writeUTF(msgReply);) so how can i fix this error thanks for yours attention.
Code:
String msgReply = "&sim1$extr€"+getIpAddressonly()+"?8080.wifi/";
try
{
dataOutputStream.writeUTF(msgReply); // error at this line
}
catch (IOException e) {
e.printStackTrace();
}
You need to get your network I/O off the main (UI) thread. Start a background thread to do network I/O.

Using IBM mobile first Android sdk in Background service when the app is killed.

We are trying to make a sync adapter service in Android which will run in the background when the app is killed.
This service will fetch some data from JsonStore and will sync up with the server.
Code:
try {
URI adapterPath = new URI("/dummy/adapter");
WLResourceRequest request = new WLResourceRequest(adapterPath,WLResourceRequest.POST);
request.send(new AdapterListener(new CallbackAdapter() {
#Override
public void onFetch(String response) {
// TODO Auto-generated method stub
}
#Override
public void onError(String error) {
// TODO Auto-generated method stub
}
}));
} catch (URISyntaxException e) {
e.printStackTrace();
}
Problems:
When we try to run service in a different process, we get an error at line ( WLResourceRequest request = new WLResourceRequest(adapterPath,WLResourceRequest.POST);) that WL.getInstance should be called after WL.createInstance but we can not create WL instance in service because it needs an instance of ACTIVITY.
When we try to run service in the same process, in which the app is currently running, everything works fine untile app is running but if we kill the app same things happen which are happening at point 1.
Questions:
Is there a way we can create WL instance in service.
Is there a way we can let WL instance initialized forever, even though user kills the app.
Is there a way we can let our app run forever with WL instance initialized forever.
I got it working, all you need to add
WL.App.setKeepAliveInBackground(true);
into the js file and WL instance will work with sync adapters and services in Android.
Running the MobileFirst Android SDK in an Android Service is currently not supported. There is an open feature request for this ability, so please feel free to add your vote if you can want to make this happen. Search here: https://mobilefirstplatform.ibmcloud.com/help/

Android, internet connection and no answer for ordinary calling

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.

log file for errors?

I am developing an Android-App with "Aide".Aide is an app for developing android apps with android devices. When i start the app, i have created, i get an error like "the app has aborted unfortunately". how can i resolve what happened wrong ? is there a log-file where i can see the stack trace ? is ist possible that everytime an error happens a dialog apperas with the stack trace instead of the message "the app has aborted" ? thanks for everybody who can help me.
Greets
Arne
If you want to observe the stack trace, all you need is a LogCat reader, like CatLog, for instance. Note that if your device is Jelly Bean of higher, you'll need root permissions to read the logs.
EDIT:
Further research indicates that there is a LogCat reader built into AIDE. The root permission issue still applies.
I have never used Aide, but the concept will be the same. You need to be able to debug your app on your phone via your IDE. As an example in Eclipse I would connect my phone via usb and in Eclipse it then shows up as an Android Device in AVD. I then run my App in Debug mode on my phone and all your error output will be in Logcat. Otherwise you will have to code debug logic into your app so that it writes it's own logging onto your fs on you phone.
If you have the Android SDK installed (I guess it's the case), then you can use the adb utility to access the log :
adb logcat
This will show you stacktrace in case of error, and many very other useful informations.
You got 3 options:
Upgrade into a stable Pro version to use the working LogCat on AIDE
Use USB debugging as mentioned by apesa
Use following function to log to local file:
public void appendLog(String text)
// https://stackoverflow.com/a/6209739/8800831
{
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. flush();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Use it like this:
try{
// your code goes here
}catch (Exception e){
appendLog(e);
}
You need to add permission for writing_external_storage in Manifest.

How to programatically hide Caller ID on Android

on Android phones, under Call -> Additional settings -> Caller ID
it is possible to hide your caller ID. I want to do that programatically from my code, but was not able to find a way to do that.
I searched through
android.provider
android.telephony
for 2.1 release and was not able to find it.
Has anybody successfully solved this issue?
Thanks in advance. Best regards.
Here I will describe two approaches I tried.
1.) It is possible to display Additional Call Settings screen from your application. Although it looks like it is part of the Settings application, that is not true. This Activity is part of the Native Phone Application, and it may be approached with the following intent:
Intent additionalCallSettingsIntent = new Intent("android.intent.action.MAIN");
ComponentName distantActivity = new ComponentName("com.android.phone", "com.android.phone.GsmUmtsAdditionalCallOptions");
additionalCallSettingsIntent.setComponent(distantActivity);
startActivity(additionalCallSettingsIntent);
Then user has to manually press on the CallerID preference and gets radio button with 3 options.
This was not actually what I wanted to achieve when I asked this question. I wanted to avoid step where user has to select any further options.
2.) When approach described under 1.) is executed in the Native Phone Application, function setOutgoingCallerIdDisplay() from com.android.internal.telephony.Phone has been used.
This was the basis for the next approach: use Java Reflection on this class and try to invoke the function with appropriate parameters:
try
{
Class <?> phoneFactoryClass = Class.forName("com.android.internal.telephony.PhoneFactory");
try
{
Method getDefaultPhoneMethod = phoneFactoryClass.getDeclaredMethod("getDefaultPhone");
Method makeDefaultPhoneMethod = phoneFactoryClass.getMethod("makeDefaultPhone" , Context.class);
try
{
makeDefaultPhoneMethod.invoke(null, this);
Object defaultPhone = getDefaultPhoneMethod.invoke(null);
Class <?> phoneInterface = Class.forName("com.android.internal.telephony.Phone");
Method getPhoneServiceMethod = phoneInterface.getMethod("setOutgoingCallerIdDisplay", int.class, Message.class);
getPhoneServiceMethod.invoke(defaultPhone, 1, null);
}
catch (InvocationTargetException ex)
{
ex.printStackTrace();
}
catch (IllegalAccessException ex)
{
ex.printStackTrace();
}
}
catch (NoSuchMethodException ex)
{
ex.printStackTrace();
}
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
Firstly I tried just to use getDefaultPhone(), but I get RuntimeException
"PhoneFactory.getDefaultPhone must be called from Looper thread"
Obviously, issue lies in the fact that I tried to call this method from the Message Loop that was not the Native Phone App one.
Tried to avoid this by making own default phone, but this was a security violation:
ERROR/AndroidRuntime(2338): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.provider.Telephony.SPN_STRINGS_UPDATED from pid=2338, uid=10048
The only way to overcome (both of) this would be to sign your app with the same key as the core systems app, as described under
Run secure API calls as root, android
I'm not sure if this is a global feature, but Australian phones can hide their number by prefixing the caller's number with #31# or 1831. This may not be the perfect solution, but a prefix like this could possibly work for your requirements during coding.

Categories

Resources