2

Android を使用するのはやや新しいため、サービスに関するヘルプが必要です。間隔 X で現在の位置をポーリングするサービスがあります。そのサービスにバインドし、getLastKnownLocation をサービスからアクティビティ A に渡したいと考えています。バインドされたサービスからアクティビティに情報がどのように渡されるのか正確にはわかりません。そのバインダーまたは何を介して。とにかく、ここに私のコードがあります。

サービス:

public class LocationService extends Service implements LocationListener {


    LocationManager myLocationManager;
    public Location myLocation;
    LocationListener myLocationListener;
    public static final String TAG = LocationService.class.getSimpleName();
    MyDB db;
    double latitude,longitude;
    Cursor c;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.d(TAG, "service started (onCreate)");
        db = new MyDB(getApplicationContext());
        myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAccuracy(Criteria.ACCURACY_LOW);
        String locationProvider = myLocationManager.getBestProvider(criteria, true);
        myLocationManager.requestLocationUpdates(locationProvider, 1000*60*2, 100, this);
        myLocation = myLocationManager.getLastKnownLocation(locationProvider);


    }
 public class MyBinder extends Binder {
            LocationService getService() {
                return LocationService.this;
            }
        }

アクティビティ A:

public class myActivity extends Activity {
    LocationManager myLocationManager;
    Location myLocation;

    boolean isBound = false;

    private LocationService mBoundService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        bindLocationService();

}
private void bindLocationService() {
        try {
            isBound = getApplicationContext().bindService( new Intent(getApplicationContext(), LocationService.class), mConnection, BIND_AUTO_CREATE );
            bindService(new Intent(this, LocationService.class), mConnection, BIND_AUTO_CREATE);
        } catch (SecurityException e) {
            // TODO: handle exception
        }
    }
private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((LocationService.MyBinder)service).getService();
            Log.d(LocationService.TAG, "activity bound to service");

        }

        public void onServiceDisconnected(ComponentName className) {

            mBoundService = null;
            Log.d(LocationService.TAG, "activity unbound to service");
        }
    };
}
4

3 に答える 3

4

次のように、サービスからブロードキャストを送信します。

Intent i = new Intent(NEW_MESSAGE);  
Bundle bundle = new Bundle();
bundle.putString("yourvalue", value);
i.putExtras(bundle);
sendBroadcast(i);

次のようにアクティビティでレシーバーに登録します。

newMessage messageReceiver = new newMessage();
registerReceiver(messageReceiver, new IntentFilter(NEW_MESSAGE));

これは、アクティビティ クラスのレシーバーです。

public class newMessage extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {    
        String action = intent.getAction();
        if(action.equalsIgnoreCase(IMService.NEW_MESSAGE)){    
        Bundle extra = intent.getExtras();
        String username = extra.getString("yourvalue");
    }
}
于 2012-04-16T18:39:59.110 に答える
2

Service と Activity 間の通信については、CustomBroadcastReceiverを作成し、Activity を Intent で更新するたびに Service からブロードキャストします。

    Intent i = new Intent();
    i.setAction(CUSTOM_INTENT);
    context.sendBroadcast(i);

カスタムブロードキャストについては、この例を参照してください

于 2012-04-16T18:32:57.123 に答える
0

それがバインドするActivityために「サブスクライブ」する必要があります。Serviceは、 から受け取るデータを介してServiceタイプのオブジェクトを受け取ることができます。したがって、あなたの内部で単純な を定義し、それを使用する を定義できます。このようにして、は タイプのオブジェクトを に送信し、必要なすべての情報をに送信できます。MessengerIntentActivityHandlerActivityMessengerServiceMessageActivityMessage

于 2012-04-16T18:36:14.817 に答える