Phone 进入地理围栏时不会保持沉默

Phone doesnt get silent on entering Geofence

每当我进入地理围栏时,我想打开我的 phone silent.The 通知也应该出现。然而,在进入地理围栏时它只会静默 1 秒,这是生成通知的时间,通知显示在我的屏幕上后,它会自动返回正常模式。请帮助我了解如何在进入地理围栏时永久关闭我的 phone 并在离开时将其恢复正常 geofence.Here 是我的 GeofenceintentService class->

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";
    }

}

在方法 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);

所以您将模式设置为静音,然后在几行代码内直接恢复正常,这就是为什么您的解决方案无法按您希望的那样工作。