2017-04-06 9 views
0

したがって、1つのアクティビティで定義されたbtDeviceをparamsのサービスに送信する必要があります。ここではサービスのコードは次のとおりです。アクティビティからサービスへのエクストラを設定するには?

public class Servicio extends JobService implements ServiceConnection { 

private static BluetoothDevice btDevice; 
public TemperaturaFragment temperaturaFragment; 
private MetaWearBoard mwBoard; 

@Override 
public boolean onStartJob(JobParameters params) { 
    mJobHandler.sendMessage(Message.obtain(mJobHandler, 1, params)); 
    params.getExtras(btDevice = getParcelableExtra(EXTRA_BT_DEVICE)); 
    getApplicationContext().bindService(new Intent(this, BtleService.class), (ServiceConnection) this, BIND_AUTO_CREATE); 
    return true; 
} 



@Override 
public boolean onStopJob(JobParameters params) { 

    return false; 
} 
private Handler mJobHandler = new Handler(new Handler.Callback() { 

    @Override 
    public boolean handleMessage(Message msg) { 

     Toast.makeText(getApplicationContext(), 
       "JobService task running", Toast.LENGTH_SHORT) 
       .show(); 
     jobFinished((JobParameters) msg.obj, false); 
     return true; 
    } 

}); 

そして、ここでは、私はそれを送信したいオブジェクトがアクティビティに定義されている。

btDevice= getIntent().getParcelableExtra(EXTRA_BT_DEVICE); 

私はサービスで、その後builder.setExtras(the object here)を使用したいgetExtras(get the object)セットエキストラはそれがBluetoothDeviceだとバンドルが必要だと言う。何か案が?助けてくれてありがとう。データが読み込まれたばかりされているので、私はこれを入れて動作しません

Intent serviceIntent = new Intent(Servicio.class.getName()) 
        serviceIntent.putExtra(1, getIntent().getParcelableExtra(EXTRA_BT_DEVICE)); 
        getApplicationContext().startService(serviceIntent); 
+0

であなたは、デバイスIDを渡すためにしようとしていますか?共有設定を使用するとどうなりますか? –

+0

私はIDを渡そうとしていない、私は取得するデータを渡そうとしている、今私はシリアル化メソッド、任意のアイデアを参照してください? : –

答えて

1

IntentsetExtras()方法がBundle対象として入力された値をとり、BluetoothDeviceオブジェクトがBundleクラスオブジェクトではないために送信されません。

ParcelableArrayListは、BundleとIntentを使用してプリミティブなデータ型を渡すことができます。

バウンドサービス

バウンドサービスは、クライアント・サーバ・インタフェース内のサーバーです。これにより、コンポーネント(アクティビティなど)がサービスにバインドされ、要求を送信し、応答を受信し、プロセス間通信(IPC)を実行できます。バインドされたサービスは通常、別のアプリケーションコンポーネントを提供し、バックグラウンドで無期限に実行されない間のみ存在します。

Bound Service Example In Android

そして、あなたは、あなたがコードの下のようにIntentを通じてServiceにデータを送信することができEDIT

Activityから

Define an interface your Service will use to communicate events:

をサービスクラスのメソッドを呼び出す方法を見つける:

Intent serviceIntent = new Intent(YourService.class.getName()) 
serviceIntent.putExtra("UserID", "123456"); 
context.startService(serviceIntent); 

そして、あなたが呼ばれるServiceクラスのcallback方法でそのIntentオブジェクトを取得することができます。

Serviceはあなたのために意図オブジェクトから値(UserID)を取得することができ、この方法ではので、そのonStartCommand()メソッドが呼び出されますが開始されると例えば

public int onStartCommand (Intent intent, int flags, int startId) { 
    String userID = intent.getStringExtra("UserID"); 
    return START_STICKY; 
} 

EDIT

BluetoothDeviceオブジェクトは、BluetoothDeviceクラスがParcelableインターフェイスを実装しているため、以下のコードのようにIntentに直接送信することもできます。

Intent serviceIntent = new Intent(YourService.class.getName()) 
Bundle bundle = new Bundle(); 
bundle.putParcelable("btDevice",bluetoothDevice); 
serviceIntent.putExtras(bundle); 
context.startService(serviceIntent); 

そして、あなたは、サービス・クラスでその下のように取得することができます。onStartCommand方法

BluetoothDevice btDevice = (BluetoothDevice) bundle.getParcelable("btDevice"); 
+0

どうすればいいですか?デバイスをサービスに渡すための他の方法はありますか?またはデバイスをバンドルに変換する方法はありますか? –

+0

ParcelabeとArrayListをデバイス、ソースまたは説明? –

+0

の場合、バインドサービスを作成し、バインダーオブジェクトを使用してサービスとアクティビティの間であらゆる種類のデータを渡す必要があります。 –

関連する問題