2016-01-27 8 views
0

Всякий раз, когда я вхожу в Geofence, я хочу отключить свой телефон. Также должно появиться уведомление. Однако при вводе геофорумы он просто отключается в течение 1 секунды, то есть время, когда генерируется уведомление, после того, как уведомление отображается на моем экране, оно автоматически возвращается в обычный режим. Пожалуйста, помогите мне о том, как постоянно включаю свой телефон умалчивает о вводе Geofence и точно так же, чтобы превратить его обратно в нормальное состояние на выходе из geofence.Here являются мой GeofenceintentService class->Телефон не молчит о входе Geofence

public class GeofenceIntentService extends IntentService { 
    private final String TAG = this.getClass().getCanonicalName(); 

    /** 
    * Creates an IntentService. Invoked by your subclass's constructor. 
    * 
    * //@param name Used to name the worker thread, important only for debugging. 
    */ 
    private AudioManager audioManager; 
    public GeofenceIntentService() { 
     super("GeofenceIntentService"); 
    } 
    public void onCreate() { 
     super.onCreate(); 
     Log.v(TAG, "onCreate GeofenceIntentService"); 
    } 

    public void onDestroy() { 
     super.onDestroy(); 
     Log.v(TAG, "onDestroy"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); 
     Log.v(TAG, "onHandleIntent"); 
     audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
     if(!geofencingEvent.hasError()) { 
      int transition = geofencingEvent.getGeofenceTransition(); 
      String notificationTitle; 
      if(transition==1 || transition==4) 
      { 
       audioManager=(AudioManager)getSystemService(AUDIO_SERVICE); 
       audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,6,0); 
       audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE | AudioManager.RINGER_MODE_SILENT); 
      } 
      else if(transition==2) 
      { 
       audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 

      } 
      switch(transition) { 
       case Geofence.GEOFENCE_TRANSITION_ENTER: 
//     int flag = PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 
//     ComponentName cName = new ComponentName(GeofenceIntentService.this,RingerModeChangeReceiver.class); 
//     audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE | AudioManager.RINGER_MODE_SILENT); 
//     getPackageManager().setComponentEnabledSetting(cName,flag,PackageManager.DONT_KILL_APP); 
        Log.i("the phone is:", getSystemService(Context.AUDIO_SERVICE) + ""); 
        notificationTitle = "Geofence Entered"; 
        Log.v(TAG, "Geofence Entered"); 
        break; 
       case Geofence.GEOFENCE_TRANSITION_DWELL: 
        notificationTitle = "Geofence Dwell"; 
        Log.v(TAG, "Dwelling in Geofence"); 
        break; 
       case Geofence.GEOFENCE_TRANSITION_EXIT: 

        notificationTitle = "Geofence Exit"; 
        Log.v(TAG, "Geofence Exited"); 
        break; 
       default: 
        notificationTitle = "Geofence Unknown"; 
      } 
      Log.i("notification", "sent"); 
      sendNotification(this, getTriggeringGeofences(intent), notificationTitle); 
     } 
     else 
     { 

      Log.i("Geofence ","Error"); 
     } 
    } 
    private void sendNotification(Context context, String notificationText, 
            String notificationTitle) { 

     PowerManager pm = (PowerManager) context 
       .getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wakeLock = pm.newWakeLock(
       PowerManager.PARTIAL_WAKE_LOCK, ""); 
     wakeLock.acquire(); 

     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
       context).setSmallIcon(R.drawable.index) 
       .setContentTitle(notificationTitle) 
       .setContentText(notificationText) 
       .setDefaults(Notification.DEFAULT_ALL).setAutoCancel(false); 


     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notificationBuilder.build()); 
     AudioManager audiomanage = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
     audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT); 
     Toast.makeText(getApplicationContext(), "Now in Silent mode", Toast.LENGTH_SHORT).show(); 
     wakeLock.release(); 

     audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 
    } 
    private String getTriggeringGeofences(Intent intent) { 
     GeofencingEvent geofenceEvent = GeofencingEvent.fromIntent(intent); 
     Log.i("Event is:",geofenceEvent+""); 
     List<Geofence> geofences = geofenceEvent 
       .getTriggeringGeofences(); 



     return "Mobile turned silent"; 
    } 

} 

ответ

1

В коде в методе sendNotification() у вас есть

audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT); 
Toast.makeText(getApplicationContext(), "Now in Silent mode", Toast.LENGTH_SHORT).show(); 
wakeLock.release(); 

audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 

Таким образом, вы установите режим беззвучный, а затем прямо назад к нормальному в течение нескольких строк кода, следовательно, почему ваше решение не работает, как вы хотите.

+0

большое спасибо, такая глупая ошибка! –

 Смежные вопросы

  • Нет связанных вопросов^_^