Looper.prepare() ?

Put your problem here if it does not fit any of the other categories.

Looper.prepare() ?

Postby Illuminat23 » Tue Feb 02, 2010 11:12 pm

I i'm coding a programm with a backroudn Service that runs at the Startup.
Now the Problem is that i found this in the Log:
Can't create handler inside thread that has not called Looper.prepare()

Normally the Programm will send GPS Information to a http Server.

Any Ideas?
Illuminat23
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 17, 2009 3:26 pm

Top

Looper.prepare() should be in run()

Postby bingxuewangzi427 » Wed Feb 03, 2010 7:33 am

can you show your codes?
bingxuewangzi427
Developer
Developer
 
Posts: 38
Joined: Tue Jan 26, 2010 11:33 am

Postby Illuminat23 » Wed Feb 03, 2010 8:04 am

Yes!
Here is the Code for my Service.
Who will be startet at the beginning:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package hackbard.Myservice;
  2.  
  3.  
  4.  
  5. import java.io.IOException;
  6.  
  7. import java.util.HashMap;
  8.  
  9. import java.util.Map;
  10.  
  11. import java.util.Timer;
  12.  
  13. import java.util.TimerTask;
  14.  
  15.  
  16.  
  17. import org.apache.http.HttpResponse;
  18.  
  19. import org.apache.http.client.ClientProtocolException;
  20.  
  21.  
  22.  
  23. import android.app.Service;
  24.  
  25.  
  26.  
  27. import android.content.Context;
  28.  
  29. import android.content.Intent;
  30.  
  31. import android.content.SharedPreferences;
  32.  
  33. import android.location.Location;
  34.  
  35. import android.location.LocationListener;
  36.  
  37. import android.location.LocationManager;
  38.  
  39. import android.os.Bundle;
  40.  
  41. import android.os.IBinder;
  42.  
  43. import android.os.Looper;
  44.  
  45. import android.util.Log;
  46.  
  47. import android.widget.Toast;
  48.  
  49.  
  50.  
  51. public class MonitorService extends Service {
  52.  
  53.  
  54.  
  55.         private Timer timer = new Timer();
  56.  
  57.         public static final String PREFS_NAME = "Myservice";
  58.  
  59.         private LocationManager lm;
  60.  
  61.         private LocationListener locationListener;
  62.  
  63.         private Location location = null;
  64.  
  65.  
  66.  
  67.         @Override
  68.  
  69.         public IBinder onBind(Intent arg0) {
  70.  
  71.                 return null;
  72.  
  73.         }
  74.  
  75.  
  76.  
  77.         @Override
  78.  
  79.         public void onCreate() {
  80.  
  81.                 super.onCreate();
  82.  
  83.                 initService();
  84.  
  85.         }
  86.  
  87.  
  88.  
  89.         private void initService() {
  90.  
  91.                 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  92.  
  93.                 if(settings.getBoolean("gpsautoupdater", true)){
  94.  
  95.                         timer.scheduleAtFixedRate(new TimerTask() {
  96.  
  97.                                 public void run() {
  98.  
  99.                                         updateNotification();
  100.  
  101.                                 }
  102.  
  103.                         }, 0, settings.getLong("gpsupdate", 5000));
  104.  
  105.                 }
  106.  
  107.         }
  108.  
  109.  
  110.  
  111.         protected void updateNotification() {
  112.  
  113.                 refresh();
  114.  
  115.         }
  116.  
  117.  
  118.  
  119.         public void refresh() {
  120.  
  121.                 try {
  122.  
  123.                         lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  124.  
  125.                         locationListener = new MyLocationlistener();
  126.  
  127.                         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
  128.  
  129.                                         locationListener);
  130.  
  131.                         location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  132.  
  133.                 } catch (Exception e) {
  134.  
  135.                         Log.w("INFO", "1: " + e.getMessage());
  136.  
  137.                 }
  138.  
  139.                 String url = "http://test.de/output.php";
  140.  
  141.                 Map<String, String> kvPairs = new HashMap<String, String>();
  142.  
  143.                 kvPairs.put("lon", String.valueOf(location.getLongitude()));
  144.  
  145.                 kvPairs.put("lat", String.valueOf(location.getLatitude()));
  146.  
  147.                 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  148.  
  149.                 kvPairs.put("nick", settings.getString("nick", "anonymus"));
  150.  
  151.                 try {
  152.  
  153.                         HTTPPoster.doPost(url, kvPairs);
  154.  
  155.                 } catch (ClientProtocolException e) {
  156.  
  157.                         Log.w("INFO", "2: " + e.getMessage());
  158.  
  159.  
  160.  
  161.                 } catch (IOException e) {
  162.  
  163.                         Log.w("INFO", "3: " + e.getMessage());
  164.  
  165.                 }
  166.  
  167.  
  168.  
  169.         }
  170.  
  171.  
  172.  
  173.         private class MyLocationlistener implements LocationListener {
  174.  
  175.                 public void onLocationChanged(Location loc) {
  176.  
  177.                 }
  178.  
  179.  
  180.  
  181.                 public void onProviderDisabled(String provider) {
  182.  
  183.                 }
  184.  
  185.  
  186.  
  187.                 public void onProviderEnabled(String provider) {
  188.  
  189.                         refresh();
  190.  
  191.                 }
  192.  
  193.  
  194.  
  195.                 public void onStatusChanged(String provider, int status, Bundle extras) {
  196.  
  197.                 }
  198.  
  199.         }
  200.  
  201. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
Illuminat23
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 17, 2009 3:26 pm

try this codes..

Postby bingxuewangzi427 » Wed Feb 03, 2010 8:12 am

// Edit your codes to the following ,tell me the result~~ :)

private void initService() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if(settings.getBoolean("gpsautoupdater", true)){
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
Looper.prepare(); //////////////////////////new add
updateNotification();
Looper.loop(); ///////////////////////////new add
}
}, 0, settings.getLong("gpsupdate", 5000));
}
}
bingxuewangzi427
Developer
Developer
 
Posts: 38
Joined: Tue Jan 26, 2010 11:33 am

Postby Illuminat23 » Wed Feb 03, 2010 8:49 am

Cool it works!
But why does the Service ends when i close the app?
Illuminat23
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 17, 2009 3:26 pm

hi

Postby bingxuewangzi427 » Wed Feb 03, 2010 9:18 am

Illuminat23 wrote:Cool it works!
But why does the Service ends when i close the app?


It's because the service live.If you want to keep the service running,you should call onBind() first, are you sure you don't call stopService() when close the app?
bingxuewangzi427
Developer
Developer
 
Posts: 38
Joined: Tue Jan 26, 2010 11:33 am

Top

Postby Illuminat23 » Wed Feb 03, 2010 9:22 am

Yes thats important ;)
The User can check in the Config if he want that or not

So put onBind in the Create function of the service?
Illuminat23
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 17, 2009 3:26 pm

hi

Postby bingxuewangzi427 » Wed Feb 03, 2010 9:37 am

Illuminat23 wrote:Yes thats important ;)
The User can check in the Config if he want that or not

So put onBind in the Create function of the service?


Yes,if you use onBind() to start a service ,it means that the stopService is useless,you must call UnbindService() to stop it.
Use onBind() is the best way,because you don't worry about some unexpected reasons to stop the service.....

My English is not so well,I hope you can unstantand what I mean! :D
bingxuewangzi427
Developer
Developer
 
Posts: 38
Joined: Tue Jan 26, 2010 11:33 am

Postby Illuminat23 » Wed Feb 03, 2010 9:42 am

i use:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if (settings.getBoolean("gpsautoupdater", true)) {
  2.  
  3.                         intent = new Intent(this, MonitorService.class);
  4.  
  5.                         startService(intent);
  6.  
  7.                 }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

in my Main App.
So i have there to recode that i use onBind?
Or i do onBind in MyService fot itself?

My english is bad too ;)
I'm from Germany
Illuminat23
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 17, 2009 3:26 pm

Postby bingxuewangzi427 » Wed Feb 03, 2010 10:06 am

Illuminat23 wrote:i use:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if (settings.getBoolean("gpsautoupdater", true)) {
  2.                         intent = new Intent(this, MonitorService.class);
  3.                         startService(intent);
  4.                 }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

in my Main App.
So i have there to recode that i use onBind?
Or i do onBind in MyService fot itself?

My english is bad too ;)
I'm from Germany

/// try the following codes
private ServiceConnection _connection = new ServiceConnection ()
if (settings.getBoolean("gpsautoupdater", true)) {
intent = new Intent(this, MonitorService.class);
bindService(intent,_connection,this.BIND_AUTO_CREATE); }

you can call unbindService(_connection) to stop it...

PS :I'm from China ,glad to meet you.Do you use MSN? Maybe we can share experience in the further :)
bingxuewangzi427
Developer
Developer
 
Posts: 38
Joined: Tue Jan 26, 2010 11:33 am

Postby Illuminat23 » Wed Feb 03, 2010 10:46 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private ServiceConnection _connection = new ServiceConnection ()
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

There is the Problem:
Cannot instantiate the type ServiceConnection

And i can't find a good fix on Google...
Illuminat23
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 17, 2009 3:26 pm

oh,sorroy

Postby bingxuewangzi427 » Wed Feb 03, 2010 10:55 am

Illuminat23 wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private ServiceConnection _connection = new ServiceConnection ()
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

There is the Problem:
Cannot instantiate the type ServiceConnection

And i can't find a good fix on Google...


sorry,give you the whole codes


private ServiceConnection _connection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
_boundService = ((TestService.LocalBinder)service).getService();

Toast.makeText(TestServiceHolder.this, "Service connected",
Toast.LENGTH_SHORT).show();
}

public void onServiceDisconnected(ComponentName className) {
// unexpectedly disconnected,we should never see this happen.
_boundService = null;
Toast.makeText(TestServiceHolder.this, "Service connected",
Toast.LENGTH_SHORT).show();
}
};

private void initButtons() {
Button buttonStart = (Button) findViewById(R.id.start_service);
buttonStart.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
startService();
}
});

Button buttonStop = (Button) findViewById(R.id.stop_service);
buttonStop.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
stopService();
}
});

Button buttonBind = (Button) findViewById(R.id.bind_service);
buttonBind.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
bindService();
}
});

Button buttonUnbind = (Button) findViewById(R.id.unbind_service);
buttonUnbind.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
unbindService();
}
});
}

private void startService() {
Intent i = new Intent(this, TestService.class);
this.startService(i);
}

private void stopService() {
Intent i = new Intent(this, TestService.class);
this.stopService(i);
}

private void bindService() {
Intent i = new Intent(this, TestService.class);
bindService(i, _connection, Context.BIND_AUTO_CREATE);
_isBound = true;
}

private void unbindService() {
if (_isBound) {
unbindService(_connection);
_isBound = false;
}
}
bingxuewangzi427
Developer
Developer
 
Posts: 38
Joined: Tue Jan 26, 2010 11:33 am

Postby Illuminat23 » Wed Feb 03, 2010 12:49 pm

Thanks i will check that tongiht!
And bingxuewangzi427 you got a PM
Illuminat23
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 17, 2009 3:26 pm

Postby Illuminat23 » Wed Feb 03, 2010 7:16 pm

Oke where do you get TestService.LocalBinder and _boundService
dont have LocalBinder in MyServer

Got the binder an bound and so on.

But he only start it once time...
Illuminat23
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 17, 2009 3:26 pm

hi

Postby bingxuewangzi427 » Thu Feb 04, 2010 1:44 am

Illuminat23 wrote:Oke where do you get TestService.LocalBinder and _boundService
dont have LocalBinder in MyServer

Got the binder an bound and so on.

But he only start it once time...


what do you mean? If you bind the service and don't unbind it,it will always running,maybe other mistake?


PS, 8.46 AM in China
bingxuewangzi427
Developer
Developer
 
Posts: 38
Joined: Tue Jan 26, 2010 11:33 am

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests