i saw this link before i asked but no result ,i start service via intent here is the code :
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.prefs: startActivity(new Intent(MarkanaActivity.this, Prefs.class)); break; case R.id.startservices: startService(new Intent(MarkanaActivity.this, UpdaterServices.class)); break; case R.id.stopservices: startService(new Intent(MarkanaActivity.this, UpdaterServices.class)); break; } return true; } On Thu, Feb 9, 2012 at 3:04 PM, Chris Mawata <chris.maw...@gmail.com> wrote: > Take a look at > http://developer.android.com/reference/android/app/Service.html > It depends on how you are starting the service. > > On Feb 9, 5:25 am, AndroidGirl8 <walaamahmou...@gmail.com> wrote: > > Hi all, > > i'm trying to make service getting twitter update by a thread every > > 5secs > > start service just fine and getting new twittes and inserting to > > database but i cant close service and it continue working untill force > > close > > > > here is my code : > > > > public class UpdaterServices extends Service { > > > > private static final String TAG = > > UpdaterServices.class.getSimpleName(); > > updater up; > > > > @Override > > public IBinder onBind(Intent intent) { > > // TODO Auto-generated method stub > > return null; > > } > > > > @Override > > public void onCreate() { > > super.onCreate(); > > up = new updater(); > > Log.d(TAG, "check on create"); > > } > > > > @Override > > public synchronized void onStart(Intent intent, int startId) { > > super.onStart(intent, startId); > > > > if (up.isRunning() == false) { > > up.start(); > > Log.d(TAG, "check onstart"); > > } > > } > > > > @Override > > public synchronized void onDestroy() { > > > > super.onDestroy(); > > > > if (up.isRunning() == true) { > > > > up.interrupt(); > > } > > up = null; > > Log.d(TAG, "check ondestroy"); > > > > } > > > > class updater extends Thread { > > > > final static int Delay = 5000; > > private boolean running = false; > > > > public void run() { > > > > running = true; > > while (running) { > > try { > > > > // ///open database and > inserting what we get from twitter > > // into our sqlite new schema > > DBHelper dhhelper = new > DBHelper(UpdaterServices.this); > > SQLiteDatabase db = > dhhelper.getWritableDatabase(); > > > > Twitter twitter = > ((MarkanaApplication) getApplication()) > > .gettwitter(); > > List<Status> s = > twitter.getFriendsTimeline(); > > ContentValues value = new > ContentValues(); > > for (Status w : s) { > > value.put(DBHelper.C_ID, > w.id); > > > value.put(DBHelper.C_CreatedAt, w.createdAt.getTime()); > > > value.put(DBHelper.C_User, w.user.name); > > > value.put(DBHelper.C_Text, w.text); > > > > try { > > > db.insertWithOnConflict(DBHelper.Table, null, > > > value, SQLiteDatabase.CONFLICT_REPLACE); > > } catch (Exception e) { > > > > } > > > > Log.d(TAG, > String.format("%s %s", w.user.name, w.text)); > > > > } > > > > // ///close databases which we > opened > > > > db.close(); > > dhhelper.close(); > > > > Log.d(TAG, "running thread"); > > Thread.sleep(Delay); > > > > } catch (InterruptedException e) { > > running = false; > > > > } > > > > } > > > > } > > > > public boolean isRunning() { > > > > return running; > > > > } > > > > } > > > > } > > > > what is wrong in my code or how can stop service when user click on > > stop ?? > > another question what better using Asynctask instead of service > > cause i can control in it better than service or service just fine ?? > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to android-developers@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en