Johan
Johan

Reputation: 35223

Service not starting

Probably an easy question for you guys, but its my first attempt at creating a service that should run in the background when my app closes. My problem is: The service doesnt start when I click on my "start service" button. I don't see any of the toast, and nothing in the logcat (no errors either). Thanks in advance!

Service class

public class MyService extends Service {
private static final String TAG = "MyService";

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");

}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");

}
}

My main activity

public void startServiceClick(View v){

    Log.d("foo", "onClick: starting srvice");
    startService(new Intent(this, MyService.class));
}

public void stopServiceClick(View v){

    Log.d("foo", "onClick: stopping srvice");
    stopService(new Intent(this, MyService.class));
}

Manifest xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.johanberntsson.main"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>

<application
    android:icon="@drawable/maps1"
    android:label="@string/app_name" >
    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name=".LongitudeActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DebugActivity" />
    <activity android:name=".GoogleMapsActivity" />

    <service android:enabled="true" android:name=".MyService" />

</application>

</manifest>

Upvotes: 1

Views: 9013

Answers (3)

wolfaviators
wolfaviators

Reputation: 503

You may have already figured this out. In your code you override several native functions so you could add a toast and a catlog entry. Don't forget to add super.{whatever function you are calling}. What that does is it tells the android device to do whatever the function would have done had the function not been overrided, then do the extra stuff. In your case, if you call your oncreate function, it then the computer will make the toast and add a catlog entry, but it will not create the service.

@Override    //forget about doing whatever onCreate usually does.
        public void onCreate() {
                        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast
            Log.d(TAG, "onCreate");//Add message to cat- log
            //note: Service not created because function was overrided
        }

however, if you add super.onCreate, the phone will create the service, then it will do what you tell it to do.

@Override    //forget about doing whatever onCreate usually does.
    public void onCreate() {
        super.onCreate(); // do whatever onCreate usually does. Then do whatever is after me.
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast
        Log.d(TAG, "onCreate");//Add message to cat- log

    }

I hope this helps.

Upvotes: 1

Dheeresh Singh
Dheeresh Singh

Reputation: 15699

this is suggestion as all else looking fine 

should call super in each function like super.onCreate();

as may be possible service is already started and try to stop and start again and check it toast appears ....

try

<service android:enabled="true" android:name="se.johanberntsson.servies.MyService" />

Upvotes: 4

AAnkit
AAnkit

Reputation: 27549

I added your service into my code and it is working fine for me. please see below code and match with yours.

Activity >>

 public class TestActivity extends Activity implements OnClickListener {
   /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button bt = (Button)findViewById(R.id.button11);
    bt.setOnClickListener(this);
}


public void startServiceClick(View v){

    Log.d("foo", "onClick: starting srvice");
    startService(new Intent(this, MyService.class));
}

public void stopServiceClick(View v){

    Log.d("foo", "onClick: stopping srvice");
    stopService(new Intent(this, MyService.class));
}

@Override
public void onClick(View v) {
    startServiceClick(v);   

}

Manifest File >>

   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".TestActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  <service android:enabled="true" android:name=".MyService" />

</application>

Upvotes: 0

Related Questions