1

I am trying to start a service from a thread which I hava initialized with getApplicationContext() but context.startService returns null. I can't figure out why. Thanks in advance.

Mainlogic.java

public class MainLogic extends Thread {
Context context;

public MainLogic(Context context) {
    this.context = context;
}

ArrayList<Messenger> mClients = new ArrayList<Messenger>();
Messenger bluetoothService;
Messenger mMessanger = new Messenger(new MainHandler());

private class MainHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case MessageType.NEW_HEARTH_RATE: {
                         //stuff

            break;
        }
        case MessageType.REGISTER: {
            mClients.add(msg.replyTo);break;
        }
        case MessageType.UNREGISTER: {
            mClients.remove(msg.replyTo);break;
        }
        case MessageType.START_BLUETOOTH_SERVICE: {
            startBluetoothService();
            break;
        }

        default:
            break;
        }

    }
}

private ServiceConnection bluetoothConnection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Message msg = Message.obtain();
        msg.what = MessageType.CONNECTION_ENDED;
        msg.replyTo = mMessanger;

        try {
            bluetoothService.send(msg);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        bluetoothService = new Messenger(service);

        Message msg = Message.obtain();
        msg.replyTo = mMessanger;
        msg.what = MessageType.CONNECTION_ESTABLISHED;

        try {
            bluetoothService.send(msg);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
};

public void startBluetoothService() {
            //This is where i start the service
    Intent intent = new Intent(context, BluetoothService.class);
    ComponentName name=  context.startService(intent);
            //name equals null after startService 

    boolean bind=context.bindService(new Intent(context,    BluetoothService.class),
            //bind equals false after bindService
            bluetoothConnection, Context.BIND_AUTO_CREATE);

}

@Override
public void run() {
    while(true){

    }

}

public Messenger getMessanger() {
    return mMessanger;
}

}

BluetoothService.java:

public class BluetoothService extends Service {
private NumberGenerator nGenerator;

final Messenger mMessenger = new Messenger(new BluetoothServiceHandler());
ArrayList<Messenger> mClients = new ArrayList<Messenger>();


private final class BluetoothServiceHandler extends Handler {

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
             //random messaging
        }

        default:
            break;
        }

    }
}

@Override
public void onCreate() {
    HandlerThread thread = new HandlerThread("BluetoothService",
            Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();

    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Message msg = Message.obtain();

    msg.arg1 = startId;
    msg.what = MessageType.START_NUMBERGENERATOR;
    try {
        mMessenger.send(msg);
    } catch (RemoteException e) {
        e.printStackTrace();
    }

    return START_STICKY;
}

@Override
public void onDestroy() {
    Message msg = Message.obtain();
    msg.what = MessageType.BLUETOOTH_SERVICE_STOPED;

    try {
        mMessenger.send(msg);
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    super.onDestroy();
}

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

And this is how I start the thread:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MainLogic mainLogic=new MainLogic(this.getApplicationContext());
    mainLogic.setPriority(Thread.MAX_PRIORITY);
    mainLogic.start();

    mainMessenger=mainLogic.getMessanger();

    Message message= Message.obtain();
    message.what=MessageType.REGISTER;
    message.replyTo=actMessenger;
    try {
        mainMessenger.send(message);
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }}

Update 1: Manifest:

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service android:name="BluetoothService"  />
        <activity
            android:name="com.bugra.bluetoothcomponent.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

1 回答 1

2

问题可能出在清单声明中。尝试启动此服务时,您必须有一个异常,告诉您找不到它。您应该输入服务的完全限定名称(带有包名称),或者至少一个点“。” 如果您的服务位于根包中,则在名称前面。您的清单行应如下所示:

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

或者:

<service android:name="com.bugra.bluetoothcomponent.BluetoothService"  />
于 2013-04-21T22:24:46.753 回答