0

デバイスへの接続を含むサービスがありますbluetooth。最初のアクティビティからこのサービスを呼び出しています。そして、サービスが正常に作成され、開始されました。しかし、最初のアクティビティでバインドを使用してサービスのメソッドを呼び出すと、実行されません。LocalServiceの例を参照しました

私のサービスコード:

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

4

1 に答える 1