如何在侦听震动事件的Android上创建永不停止的服务?

时间:2018-11-04 14:53:33

标签: java android android-studio service

我想让此服务在后台运行,而无需用户启动应用程序,甚至在应用程序被杀死后也是如此。

摇动手机时,会显示提示信息。这是该服务的代码:

public class MyService extends Service {

    private SensorManager sensorManager;

    private float accelerationValue;  //current acceleration value and gravity
    private float accelerationLast; //last acceleration value and gravity
    private float shake; //difference between acceleration value and gravity

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        sensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorManager.registerListener(sensorListener,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);

        accelerationValue=SensorManager.GRAVITY_EARTH;
        accelerationLast=SensorManager.GRAVITY_EARTH;
        shake=0.00f;
        return START_STICKY;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        // TODO Auto-generated method stub
        Intent restartService = new Intent(getApplicationContext(),
                mainTest.class);
        restartService.setPackage(getPackageName());
        PendingIntent restartServicePI = PendingIntent.getService(
                getApplicationContext(), 1, restartService,
                PendingIntent.FLAG_ONE_SHOT);

        //Restart the service once it has been killed android


        AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +100, restartServicePI);

    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        //start a separate thread and start listening to your network object
    }

    private final SensorEventListener sensorListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {

            float x= event.values[0];
            float y= event.values[1];
            float z= event.values[2];

            accelerationLast=accelerationValue;
            accelerationValue=(float) Math.sqrt((double)(x*x + y*y + z*z));
            float delta = accelerationValue-accelerationLast;
            shake = shake * 0.9f + delta;

            // getName=getIntent().getStringExtra("APP_CLASS_PATH");

            if(shake > 12){
               // Intent launchGoogleChrome=getPackageManager().getLaunchIntentForPackage(setName);
              //  startActivity(launchGoogleChrome);
                Toast.makeText(getApplicationContext(),"shaken",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

我使用这样的意图启动此服务:
startService(new Intent(getBaseContext(),MyService.class));

0 个答案:

没有答案