2011-06-23 2 views
0

日枝送信しますActivityクラスでサービスへの活動から文字列(デバイスアドレス)を送信する(アンドロイド)は(Androidのは、i)は、Bluetooth接続に取り組んでいると私は、デバイスアドレスを持っていると私はBluetooth接続に</p> <p>を扱うサービスに送信したいことをやるために、すべてのサービスへの活動から を文字列(データを)

コード:サービス・クラスで

コード:

public void onStart(Intent intent, int startId) { 
     // TODO Auto-generated method stub 
     super.onStart(intent, startId); 

     Bundle bundle = intent.getExtras(); 
     System.out.println("*******************"+"InsideOnStart"); 
     if(bundle != null) 
     { 
      CharSequence data = (String) bundle.getCharSequence("extraData"); 
      System.out.println("*******************"+data); 
     } 


     } 

答えて

-1

あなたはあなたがしたい場合は、アクティビティから.aidlファイルで定義されたサービスで任意のメソッドを呼び出すことができますAIDL使用Android Interface Definition Language (AIDL)

を使用する必要が 、サービスへの活動から直接データを送信カントあなたがメソッドの引数としてデータを渡すことができるよりも、追加の情報のため

参照データを渡すImplementing Remote Interface Using AIDL

+1

AIDLは 'Service'と通信するための唯一の方法ではありません、それは間違いなく最も簡単ではありません。 –

+0

この回答は間違っています。通信するサービスにバインドするか、インテントを送信できます。 [Android Developers](http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingIntentService)のドキュメントをご覧ください。 –

0

あなたが見てきました10とAndroidのドキュメントの例は?

簡潔に言えば、Messengerを使用して、意味のあるメッセージをサービスに送信できます。このセクションをご覧ください:Remote Messenger Service Sample

2

これによると、article私はString to Serviceを送るActivityを実装しました。あなたは下を見ることができます、多分それは助けるでしょう。

MainActivity.java

package com.example.androidtranslator; 

import android.app.Activity; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.os.Message; 
import android.os.Messenger; 
import android.os.RemoteException; 
import android.view.Menu; 
import android.view.View; 

public class MainActivity extends Activity { 

    private Messenger mService = null; 
    private boolean mBound; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // some gui code etc... 

     /** 
     * Connect to an application service, creating it if needed 
     */ 
     bindService(new Intent(this, TranslatorService.class), mConnection, 
       Context.BIND_AUTO_CREATE); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     // Unbind from the service 
     if (mBound) { 
      unbindService(mConnection); 
      mBound = false; 
     } 
    } 

    /** 
    * on some GUI action (click button) send message 
    * 
    * @param view 
    */ 
    public void translate(View view) { 
     if (!mBound) 
      return; 
     Message msg = Message.obtain(null, TranslatorService.MSG_STRING, 
       "Some message (it can be from textView for example)"); 
     try { 
      mService.send(msg); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    private ServiceConnection mConnection = new ServiceConnection() { 

     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      mService = null; 
      mBound = false; 
     } 

     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      mService = new Messenger(service); 
      mBound = true; 
     } 
    }; 

} 

TranslatorService.java

package com.example.androidtranslator; 

import android.app.Service; 
import android.content.Intent; 
import android.os.Handler; 
import android.os.IBinder; 
import android.os.Message; 
import android.os.Messenger; 
import android.widget.Toast; 

public class TranslatorService extends Service { 

    public static final int MSG_STRING = 0; 

    /** 
    * Handler of incoming messages from clients. 
    * Show Toast with received string 
    */ 
    class IncomingHandler extends Handler { 
     @Override 
     public void handleMessage(Message msg) { 
      switch (msg.what) { 
       case MSG_STRING: 
        Toast.makeText(getApplicationContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show(); 
        break; 
       default: 
        super.handleMessage(msg); 
      } 
     } 
    } 

    /** 
    * Target we publish for clients to send messages to IncomingHandler. 
    */ 
    final Messenger mMessenger = new Messenger(new IncomingHandler()); 

    /** 
    * When binding to the service, we return an interface to our messenger 
    * for sending messages to the service. 
    */ 
    @Override 
    public IBinder onBind(Intent intent) { 
     Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show(); 
     return mMessenger.getBinder(); 
    }  

}