我创建了一项服务,但即使在手机锁定期间,此服务仍然有效。我不想这样做。我想在我的程序上手动检查,只是我尝试了一切,但我无法做到。你能帮我吗?
这是我的主要活动
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Timer();
}
@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;
}
public void Timer(){
new CountDownTimer(5000, 1000) {
Intent intent = new Intent(MainActivity.this, Servis.class);
public void onTick(long millisUntilFinished) {
stopService(intent);
}
public void onFinish() {
startService(intent);
Timer();
}
}.start();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这就是服务
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.os.Vibrator;
public class Servis extends Service implements SensorEventListener {
SensorManager sManager;
Sensor sensor;
private Vibrator vibrator;
boolean flag;
Intent intent;
private BroadcastReceiver broadcastReceiver= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
sManager.unregisterListener(Servis.this);
sManager.registerListener(Servis.this, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
};
@Override
public void onCreate() {
sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(broadcastReceiver, filter);
sManager.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
flag=true;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float z = event.values[2];
//AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (z<5 && flag==true) {
vibrator.vibrate(200);
flag=false;
//audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
/*else {
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}*/
}
}
}