Question

I'm trying to implement simple service in android but I could not stat the basic service.

Here is my main class:

 import java.io.IOException;
    import java.net.InetAddress;
    import java.net.UnknownHostException;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    import com.finki.darko.mk.services.SimpleService;

    public class Main extends Activity implements OnClickListener {
        private Button startService;
        private Intent intent;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            if (!isOnline()) {
                Toast.makeText(this, R.string.internetConnection,
                        Toast.LENGTH_SHORT).show();
                finish();
            } else {
                setContentView(R.layout.main_activity);
                startService = (Button) findViewById(R.id.vesti);
                startService.setOnClickListener(this);


            }
        }

        private static boolean isOnline() {
            try {
                InetAddress.getByName("google.com").isReachable(3);
                return true;
            } catch (UnknownHostException e) {
                return false;
            } catch (IOException e) {
                return false;
            }
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.vesti:
                startService(new Intent(Main.this,SimpleService.class));
                break;

            }

        }
    }

and this is my simple server class:
ublic class SimpleService extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
          return null;
    }
    @Override
    public void onCreate() {
          super.onCreate();
          Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
          super.onDestroy();
          Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
    }
}

also I've entered the service in the manifest like this:

<service android:name=".SimpleService" >
        </service>

Does anybody know why I'm not able to start the service?

Was it helpful?

Solution 2

You need to declare the service int the manifest like this:

<service
    android:name="com.finki.darko.mk.services.SimpleService" 
    android:enabled="true" 
/>

OTHER TIPS

Please provide the complete package name + class name in Manifest like:-

<service android:name="<package-name>.Main.SimpleService" 
  android:enabled="true" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top