-1

in the below code i am trying to start a service as shown. but at run time

serviceCtrl.isMyServiceRunning()

returns false and the toast doesnt show up despite the service is added to the manifest file.

i am starting the service wrongly?! why the service does not start

code:

if (mIsBonded) {
            mSB.append("Pairing Completed" + "\n");
            Intent intSPPService = new Intent(getApplicationContext(), SPPService.class);
            intSPPService.putExtra("key", "starting SPP Service");
            startService(intSPPService);

            ServiceCtrl serviceCtrl = new ServiceCtrl(getApplicationContext(), ActMain.class);
            if (serviceCtrl.isMyServiceRunning()) {
                Toast.makeText(getApplicationContext(), "Starting SPP Service", Toast.LENGTH_SHORT).show();
            }

            //mATRFCConn = new ATRFCConn();
            //mATRFCConn.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {
            mSB.append("Pairing Failed" + "\n");
        }
...
...
...

public boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) this.mCtx.getSystemService(this.mCtx.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (this.mClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

service:

public class SPPService extends Service {

private final String TAG = this.getClass().getSimpleName();
final static String SPP_SERVICE_ACTION = "SPP_ACTION";

@Override
public void onCreate() {
    Log.w(TAG, "onCreate");

    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.w(TAG, "onStartCommand");

    String str = intent.getStringExtra("key");
    Log.v(TAG, "Act->Service : " + str);


    return Service.START_NOT_STICKY;
}

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

}

Update

at run time i receive

 W/ActivityManager: Unable to start service Intent { 
 cmp=com.example.com.myapplication/com.example.com.servicebt_01.SPPService 
 (has extras) } U=0: not found

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.servicebt_01" >

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".ActMain" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

<service
    android:enabled="true"
    android:name=".SPPService"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/service_name"
    >
</service>

Amrmsmb
  • 1
  • 27
  • 104
  • 226

2 Answers2

0

Please check this answer : Unable to start service Intent

It says you have not given the name of the service in the manifest correctly. Please verify it.

Try to enter the Service name along with the entire package name in which it is present.

Community
  • 1
  • 1
rahul
  • 1,095
  • 8
  • 22
0

Your service name in manifest is error,not

<service
android:name="SPPService"
android:icon="@mipmap/ic_launcher"
android:label="@string/service_name"
>
</service>

If your SSPService is located in your application package,you can specify it like this:

<service
android:name=".SPPService"
android:icon="@mipmap/ic_launcher"
android:label="@string/service_name"
>
</service>

If not,you should specify its full path.

starkshang
  • 8,228
  • 6
  • 41
  • 52