停止服务的问题

时间:2013-06-10 04:43:38

标签: android android-service android-lifecycle

我有一个名为"Callreceiver"的广播接收器,它会在接到来电时收听它会启动名为"MyService"的服务,这实际上是SensorEventListener所以我可以在手机上做某事处于某个位置。 好的,一切正常,但是当我挂断电话时,我想结束服务"MyService",我尝试context.stopService(i);但它没有用。然后我尝试this.stopService(null);它确实结束了服务,但这只是因为我收到了一个大错误。

我的问题: 通过广播接收者停止服务的正确方法是什么?

先谢谢你。

package com.example.enablebtwithorientation;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;


public class Callreceiver extends BroadcastReceiver {
String TAG="IncomingInterceptCombo";
static boolean isRinging=false;
int x=0; float roll_angle=0;
int goin_50=0, goin_30=0;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Ringer", "ringer started");
        Intent i=new Intent(context, MyService.class);
        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_RINGING)) {
            Log.d("Ringer", "ringing");
            isRinging = true;
            context.startService(i);                
        }

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_IDLE)) {
            Log.d("Ringer", "hanging up");
            isRinging = false;
            //context.stopService(i);
        }
    }
}

import android.app.Service;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service implements SensorEventListener {
private static final String TAG = "MyService";
protected static final ContextWrapper context = null;
MediaPlayer player;
public SensorManager mSensorManager;
public Sensor mOrientation;
public static float x, y, z;
public AudioManager amanager;


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

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");
}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);        
    mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_NORMAL);
    Log.d(TAG, "register sensor");
}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    float azimuth_angle = event.values[0];
    float pitch_angle = event.values[1];
    float roll_angle = event.values[2];
   Log.d(TAG, "roll_angle: " +roll_angle);

   if(roll_angle>60){Toast.makeText(this, "roll angle over 60" + roll_angle, Toast.LENGTH_LONG).show();
   this.stopService(null);// works, but not in a good way
   }
}

}

1 个答案:

答案 0 :(得分:0)

您可以在onSensorChanged方法中使用stopSelf()代替stopService(null)