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

Reply via email to