2013-04-21 58 views
1

我想從一個線程開始一個服務,我已經用getApplicationContext()進行了初始化,但context.startService返回null。我無法弄清楚爲什麼。 在此先感謝。Android服務不會從線程啓動

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> 
+0

請發表您的清單。 – CommonsWare 2013-04-21 15:16:39

+0

我已添加清單 – wrecker 2013-04-21 15:32:11

回答

2

問題可能出現在清單聲明中。 嘗試啓動此服務時,您必須有異常,並告訴您未找到它。 您應該輸入服務的完全限定名稱(包名稱)或至少一個點「。」。在名稱前面,如果您的服務位於根包中。你的清單上應該是這樣的:

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

或者:

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

謝謝,它的工作。但我不明白爲什麼它以前在沒有完整聲明的早期版本中工作。而且我從來沒有任何例外。 – wrecker 2013-04-23 18:43:29

+0

顯然你只會得到例外與活動,而不是服務,我的壞,我會編輯我的答案爲未來的讀者。 – Joffrey 2013-05-08 15:55:28