BroadcastRiver register edilmesine rağmen çağırılmıyor
Kod bu fakat BroadcastReceiver'ı ne yaptıysam çağıramadım bir işlevi yokmuş gibi kod fazlalığıymış gibi çalışıyor etkin değil daha doğrusu . Eksik yaptığım bir şeyler mi var acaba ?
[code]
package doruk.bodyhealth;
import android.annotation.SuppressLint;
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,sflag=true;
Intent intent;
ScreenReceiver s;
//PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
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);
flag=false;
}
}
};
@Override
public void onCreate() {
sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
/*IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);*/
flag=true;
//IntentFilter filter2 = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
sManager.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
sflag=false;
} else {
sflag=true;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@SuppressLint("NewApi")
@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 && sflag==true) {
vibrator.vibrate(200);
flag=false;
//audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
/*else {
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}*/
}
}
}
[/code]