Android服务不会从线程启动

时间:2013-04-21 15:01:32

标签: android android-service

我正在尝试从我用getApplicationContext()初始化的线程启动服务,但context.startService返回null。我无法弄清楚为什么。 提前谢谢。

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();
}
}

这就是我启动线程的方式:

    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();
    }}

更新1:清单:

 <?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>

1 个答案:

答案 0 :(得分:2)

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

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

或者:

<service android:name="com.bugra.bluetoothcomponent.BluetoothService"  />