0

I've looking for my issues but not yet find out.

I've create a service in android, and I want whenever I call the service I can operate CRUD

but I dunno how to do that and the tutorial/reffer is so scrimpy.

Here's my code:

DatabaseHandler.java and Contact.java getting from here

myService.java

public class myService extends Service {
    public Runnable mRunnable = null;
    public myService() {

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final Handler mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {
                Log.d("Service jalan", "beneran dah");
                mHandler.postDelayed(mRunnable, 30 * 1000);
            }
        };
        mHandler.postDelayed(mRunnable, 30 * 1000);

        return super.onStartCommand(intent, flags, startId);
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
        startService(new Intent(this, myService.class));

        DatabaseHandler db = new DatabaseHandler(this);

        /**
         * CRUD Operations
         * */
        // Deleted Contacts
        db.deleteAll();

        // Inserting Contacts
        Log.d("Insert: ", "Inserting ..");
        db.addContact(new Contact("Ravi"));
        db.addContact(new Contact("Srinivas"));
        db.addContact(new Contact("Tommy"));
        db.addContact(new Contact("Karthik"));

        // Reading all contacts
        Log.d("Reading: ", "Reading all contacts..");
        List<Contact> contacts = db.getAllContacts();
        //db.deleteContact(new Contact(1));


        for (Contact cn : contacts) {
            String log = "Id: "+cn.getID()+" ,Name: " + cn.getName();
            // Writing Contacts to log
            Log.d("Name: ", log);
        }

    }
}

there's a way I can use code insert ("Ravi"); insert ("Srinivas"); insert ("Tommy"); in my MainActivity?

EDIT

I've edit myService.java class like this :

public class myService extends Service {
    public Runnable mRunnable = null;
    IBinder mBinder = new LocalBinder();
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public class LocalBinder extends Binder {
        public myService getServerInstance() {
            return myService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final Handler mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {
                Log.d("Service jalan", "beneran dah");
                mHandler.postDelayed(mRunnable, 30 * 1000);
            }
        };
        mHandler.postDelayed(mRunnable, 30 * 1000);
        return super.onStartCommand(intent, flags, startId);
    }
    protected void insert(final String name){
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
        db.addContact(new Contact(name));

    }
}

but I've an error of insert in MainActivity.java when I add code below:

Error : non-static method 'insert(java.lang.string)' cannot be referenced from a static context

myService.insert(this, "Coba lagi ah");
myService.insert(this, "Ini yang kedua");

already find out the error on this site but still cant understand how to fix it

flix
  • 1,688
  • 3
  • 34
  • 64

1 Answers1

0

use your method calling in onStartCommand(Intent intent, int flags, int startId) cause every time you call a service it'll call this method.

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {

            insert("flix");

            return super.onStartCommand(intent, flags, startId);
        }
        protected void insert(final String name){
            DatabaseHandler db = new DatabaseHandler(this);
            db.addContact(new Contact(name));

        }