bluetooth
장치에 연결하는 서비스가 있습니다. 첫 서비스에서이 서비스를 호출합니다. 그리고 서비스가 성공적으로 생성되고 시작되었습니다. 하지만 첫 번째 활동에서 bind를 사용하여 서비스중인 메소드를 호출하면 실행되지 않습니다. 나는안드로이드 서비스 메소드가 액티비티에서 호출하지 않습니다.
LocalService example라고 내 서비스 코드 :
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
// A client is binding to the service with bindService()
return mBinder;
}
public class LocalBinder extends Binder {
public BluetoothServices getService() {
// Return this instance of LocalService so clients can call public methods
return BluetoothServices.this;
}
}
public void SendData(){
Log.d("msg", "Send msg");
}
내 첫번째 활동에 코드 아래 사용하고 있습니다 : 위의 코드에서 문제가 무엇
BluetoothServices mService;
boolean mBound = false;
@Override
public void onStart(){
super.onStart();
Intent btservices = new Intent(getApplication(),BluetoothServices.class);
startService(btservices);
bindService(btservices, mConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(mBound){
mService.SendData();
}
}
이유는 무엇입니까? 그것은 바인딩 및 호출 메서드가 아닌가요?
내 매니페스트 :
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:debuggable="true"
android:icon="@drawable/logo_50"
android:theme="@style/app_theme" >
<service android:name="com.example.utilities.BluetoothServices"></service>
<activity
android:name="com.example.pkg.Thisisit"
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>
위의 코드에서 문제가 무엇
logcat을 보았습니까? 오류 또는 다른 유용한 로그? –
@DavidWasser 실제로 (mBound) 내에서 해당 메서드를 호출하면 오류가 발생하지 않습니다.하지만 NullPointer 예외를 얻는 측면에서 호출합니다. 바인딩되지 않기 때문에 내 것입니다. –
매니페스트를 게시하십시오. –