package gr.mybook.mylocalservice;

import gr.mybook.mylocalservice.myLocalService.MyLocalBinder;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    myLocalService myService;
    boolean isConnected = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
    private ServiceConnection myConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            MyLocalBinder binder = (MyLocalBinder) service;
            myService = binder.getService();
            isConnected = true;
            String currentDateTime = myService.getDateTime();
            TextView myTextView = (TextView) findViewById(R.id.textView1);
            myTextView.setText(currentDateTime);
        }

        public void onServiceDisconnected(ComponentName arg0) {
            isConnected = false;
        }
     };
	
    public void onClick(View view){
    	Intent intent = new Intent(this, myLocalService.class);
    	bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
