2017-09-11 6 views
1

近接センサーの値の変化を監視するアプリを開発しています。アプリケーションでは、サービスを開始して近接センサーの監視を開始するための2つの別個のボタンが必要です。Androidで機能を呼び出す開始サービス

これは私のサービスクラス

public class MyService extends Service{ 

Sensor proxSensor; 
SensorManager sm; 
public static MyService instance; 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    instance = this; 
    return Service.START_STICKY; 
} 

public void startScan(){ 
    sm=(SensorManager)getSystemService(SENSOR_SERVICE); 
    proxSensor=sm.getDefaultSensor(Sensor.TYPE_PROXIMITY); 
    SensorEventListener eventListener = new SensorEventListener() { 
     @Override 
     public void onSensorChanged(SensorEvent sensorEvent) { 
      Log.e("Sensor","Value "+sensorEvent.values[0]); 
     } 
     @Override 
     public void onAccuracyChanged(Sensor sensor, int i) { 
     } 
    }; 
    sm.registerListener(eventListener, proxSensor, SensorManager.SENSOR_DELAY_NORMAL); 
} 

私はアプリの実行中にログ出力が正しく印刷され

public void viewNotification(View view){ 
    startService(new Intent(this,MyService.class)); 
} 

public void viewNotification2(View view){ 
    MyService.instance.startScan(); 
} 

私の主な活動からサービスを開始するが、私は活動を終了し、時に削除していますですそれ以前のアプリのリストから出力は与えられていません。しかし、私がonStartCommand内でstartScan()を呼び出すと、アプリケーションを閉じた後でも実行されます。

なぜ出力が得られないのですか? これを達成するために静的なMyServiceを使用する代わりに、他の方法がありますか?

+1

サービスにアクティビティをバインドし、静的メソッドでアクセスしないでください –

+0

私は、アプリケーションの終了後でもセンサの値を引き続き印刷したいと思っていますか?また静的な方法でサービスを呼び出さないでください –

+0

私はbindService()を試しましたが、アクティビティが生きている場合にのみセンサ出力も表示されます。サービスが無期限に実行されるようにしたい – chamathabeysinghe

答えて

-2

まず、サービスを使用するためのサービスバインディングまたはアシストのアプローチを使用します。 (参照:https://developer.android.com/guide/components/bound-services.html

をたとえば:

たちはMyServiceでという名前のサービスを持っている、と仮定します。

private void activityMethod(){ 
    if (isBounded){ 
     mService.someMethod(); 
    } 
} 
:今、あなたのようなあなたのサービスのメソッドを呼び出すことができます

private MyService mService; 
boolean isBounded; 
private ServiceConnection mServiceConnection = new ServiceConnection() { 
     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      Log.d(TAG, "onServiceConnected"); 
      MyService.LocalServiceBinder binder = (MyService.LocalServiceBinder) service; 
      mService = binder.getBinder(); 
      isBounded = true; 

     } 

     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      Log.d(TAG, "onServiceDisconnected"); 
      isBounded = false; 
     } 
    }; 

そして

@Override 
    protected void onStart() { 
     super.onStart(); 
     bindService(new Intent(this, MyService.class), mServiceConnection, BIND_AUTO_CREATE); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 

     if (isBounded) { 
      unbindService(mServiceConnection); 
      isBounded = false; 
     } 
    } 

:あなたがあなたの活動に

private final IBinder mBinder = new LocalServiceBinder(); 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 

public class LocalServiceBinder extends Binder { 
    public MyService getBinder() { 
     return MyService.this; 
    } 
} 

次の次の書き込み権限が必要です。このクラスで

Se cond、foregroundで作業する場合は、startForeground(int id、Notification notification)メソッドを呼び出します。

+0

開始したサービスにバインドすることは可能ですか? – chamathabeysinghe

+0

@chamathabeysinghe更新された回答を確認する – Slampy

関連する問題