I’ve only just really started android developing and have been reading about services here. I’ve created a service which I then try to start in an activity, but I cannot work out why it will not show the toast notifications.
I’ve got the following activity:
package com.example;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class Example extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Context context = getApplicationContext();
Intent intent = new Intent(context, MService.class);
startService(intent);
setContentView(R.layout.main);
}
}
and in my manifest I have this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<application android:label="example" android:icon="@drawable/icon">
<activity android:name="Example"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<service android:name=".MService" />
</activity>
</application>
and then my service looks like this:
package com.example;
import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;
public class MService extends IntentService {
public MService() {
super("MService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent,flags,startId);
}
@Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(this, "Systems starting", Toast.LENGTH_SHORT).show();
}
}
I know your not really mean to override onStartCommand but this was more as testing exercise since I also tried without
Service was a child of activity in the manifest so it couldn’t find the service.