Question

I am developing an app. In which i am using a worker thread inside an activity. I have some problems with my app and try to figure out what's the matter using debugging tags. but logcat doesn't display tag which is inside the thread. i am newbie so don't know much about that..

Here is the code..

package com.example.forgetmenot;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class BirthdayService extends Service{

    SQLiteDatabase db;
    BirthdayDatabase myDatabase = new BirthdayDatabase(this, null, null, 0);
    private ArrayList<String> data = new ArrayList<String>();
    Handler handler = new Handler();
    String form[] ={"_id","name","contact","day","month","year"};
    String TAG = "Debug";
    @Override
    public IBinder onBind(Intent arg0) {

        return null;
    }

    @Override
    public void onCreate() {
//      Toast.makeText(this,"birthday Service started", Toast.LENGTH_SHORT).show();
        super.onCreate();
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Calendar cal = Calendar.getInstance();

        new Thread(new Runnable() {

            @Override
            public void run() {
                Calendar cal = Calendar.getInstance();
                int mDay = cal.get(Calendar.DAY_OF_MONTH);
                int mMonth = cal.get(Calendar.MONTH);
                Log.d(TAG, "Current day: "+mDay);
                Log.d(TAG,"Current month: "+mMonth);
                int birthDay = 0;
                int birthMonth = 0;
                final Cursor mycursor = db.query("birthdays", form, null, null, null, null, null, null);
                while(mycursor.moveToNext()){
                    birthDay = mycursor.getInt(3);
                    birthMonth = mycursor.getInt(4);
                    Log.d(TAG, "birhDay:"+birthDay);
                    Log.d(TAG, "birthMonth:"+birthMonth);
                    if(mDay == birthDay && mMonth == birthMonth)
                    {
                        handler.post(new Runnable() {

                            @Override
                            public void run() {

                                Toast.makeText(getBaseContext(), "Happy Birthday "+mycursor.getString(1), Toast.LENGTH_SHORT).show();
                            }

                        });

                        db.close();
                        break;
                    }
                }
                db.close();

            }

        });

        Toast.makeText(this, "BirthdayService", Toast.LENGTH_SHORT).show();
        return 0;
    }

}
Was it helpful?

Solution

start the Thread you created in onStartCommand

OTHER TIPS

you have to ensure that which emulator or device selected in device info tab, if still you did't find it, then please reset adb, and then please select your emulator, and try again,

still get same problem then restart your eclipse or adb.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top