0

Here is my service class

    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;

    public class Communicationservice extends Service {

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            Log.d("I AM Service","Service Created");
        }

        @Override
        public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub
            super.onStart(intent, startId);
        }

    }

Here is my Main Activity


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

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


        Log.d("flow","1");
        Intent in = new Intent("com.yal.Communication.Communicationservice");
        this.startService(in);
        //startService(new Intent(MainActivity.this,Communicationservice.class));
        Log.d("flow","2");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Here is Mainfest.xml

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity > ...  </activity>
        <service android:enabled="true" android:name="com.yal.Communication.Communicationservice"></service>
    </application>

Error

11-02 16:34:17.939: D/flow(1390): 1
11-02 16:34:17.939: W/ActivityManager(92): Unable to start service Intent { act=com.yal.Communication.Communicationservice }: not found
11-02 16:34:17.939: D/flow(1390): 2
Bucks
  • 669
  • 3
  • 11
  • 27
  • Are you sure Communicationservice is in com.yal.Communication ? Because the other stuff looks fine. – stoilkov Nov 02 '12 at 11:15

2 Answers2

2

you need to check Intent-Filter.

below snippet will help you.

<service android:name="com.yal.Communication.Communicationservice">
    <intent-filter>
        <action android:name="com.yal.Communication.Communicationservice" />
    </intent-filter>
</service>

Then you can call this services like follows.

Intent serviceIntent = new Intent("com.yal.Communication.Communicationservice");
startService(serviceIntent);
Vipul
  • 30,898
  • 7
  • 67
  • 86
  • Thanks Its Working .. Why should i need to add Intent-Filter ? – Bucks Nov 02 '12 at 11:25
  • Intent-filter is a mechanism whereby you can specify which activity/services/BroadCastReceiver should be invoked on particular action. – Vipul Nov 02 '12 at 11:28
0

Constructor public Intent (String action) can be used to specify action only see following java doc:

public Intent (String action)

Create an intent with a given action. All other fields (data, type, class) are null. Note that the action must be in a namespace because Intents are used globally in the system -- for example the system VIEW action is android.intent.action.VIEW; an application's custom action would be something like com.google.app.myapp.CUSTOM_ACTION.

Parameters
action  The Intent action, such as ACTION_VIEW.

make following change to you intent creation:

Intent in = new Intent(this,com.yal.Communication.Communicationservice.class);

Or specify the action in the me manifest file:

<service android:name="com.yal.Communication.Communicationservice">
    <intent-filter>
        <action android:name="com.yal.Communication.Communicationservice" />
    </intent-filter>
</service>
Praful Bhatnagar
  • 7,390
  • 2
  • 34
  • 44