Android Silent Mode Toggle

后端 未结 2 748
慢半拍i
慢半拍i 2021-02-04 22:45

I started reading Android for dummies and I am trying to make my first app that is Silent Mode Toggle.

Now the problem I\'m facing is:

In the layout, I need som

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 23:22

    AndriodManifest.xml
    
    
    
    package com.example.myapplication;
    
    import android.Manifest;
    import android.app.AlertDialog;
    import android.app.NotificationManager;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.database.ContentObserver;
    import android.media.AudioManager;
    import android.os.Build;
    import android.os.Bundle;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Handler;
    import android.provider.Settings;
    import android.service.notification.StatusBarNotification;
    import android.view.View;
    import android.widget.FrameLayout;
    import android.widget.ImageView;
    import static android.app.NotificationManager.*;
    
    
    public class MainActivity extends AppCompatActivity {
        AudioManager audioManager;
        //private int CAMREA_CODE = 1;
    
        private int MOD_ADO_CODE=1;
        private NotificationManager mNotificationManager;
        private StatusBarNotification[] mText;
        private int MZenMode;
        private static final String TAG = "ZenModeObserver";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
            setContentView(R.layout.activity_main);
            FrameLayout contentView = (FrameLayout) findViewById(R.id.content);
            mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            contentView.setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            //RingerHelper.performToggle(audioManager);
                            //Toast.makeText(MainActivity.this, Policy, Toast.LENGTH_SHORT).show();
                            try {
                                MZenMode=logZenModeState();
                            } catch (Settings.SettingNotFoundException e) {
                                e.printStackTrace();
                            }
                            if (MZenMode==2)
                                changeInterruptionFiler(INTERRUPTION_FILTER_ALL);
                            else
                                changeInterruptionFiler(INTERRUPTION_FILTER_NONE);
                            updateUi();
                        }
                    }
            );
        }
        private void updateUi() {
            ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
            int phoneImage = MZenMode==2?R.drawable.ringer_off : R.drawable.ringer_on;
            //int phoneImage = RingerHelper.isPhoneSilent(audioManager) ? R.drawable.ringer_off : R.drawable.ringer_on;
            imageView.setImageResource(phoneImage);
        }
    
        protected void changeInterruptionFiler(int interruptionFilter) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (mNotificationManager.isNotificationPolicyAccessGranted()) {
                    mNotificationManager.setInterruptionFilter(interruptionFilter);
    
                } else {
                    Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
                    startActivity(intent);
                }
            }
        }
    
        private int logZenModeState() throws Settings.SettingNotFoundException {
            int zenModeValue = Settings.Global.getInt(getContentResolver(), "zen_mode");
            return zenModeValue;
        }
    
        private class ZenModeObserver extends ContentObserver {
    
            ZenModeObserver(Handler handler) {
                super(handler);
            }
        }
            @Override
        protected void onResume()
        {
            super.onResume();
            updateUi();
        }
    
    }
    

提交回复
热议问题