Android Silent Mode Toggle

后端 未结 2 741
慢半拍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:15

    It sounds like you want a textview that says whether silent mode is/isnt activated? ie just a small bit of text near a button, that shows status? And your buttons to dynamically appear/dissapear as well?

    TextView tv = (TextView)findviewbyid(R.id.relevanttextview); //text above button/wherever
    Button bt = (Button)findviewbyid(R.id.relevantbutton); //button to make appear/dissapear 
    
    if(vibrate is on){
    tv.setText("vibrate is on");
    bt.setVisibility(View.VISIBLE); //make button appear
    }
    
    if(normal mode is on){
    tv.setText("normal");
    bt.setVisibility(View.INVISIBLE); //make button dissapear
    }
    

    etc...

    0 讨论(0)
  • 2021-02-04 23:22
    AndriodManifest.xml
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
    
    
    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();
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题