应用程序中的Android系统小部件是否可配置? [英] Are Android system widgets within app configurable?

查看:77
本文介绍了应用程序中的Android系统小部件是否可配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下音量硬件按钮之一时,Android系统所示.当我在应用程序中按whith时,自然也会发生这种情况.

When I press one of the volume hardware buttons, Android's system is shown a depicted. Naturally this also happens when I press whithin my app.

是否可以配置这些Android系统内容(例如音量)的样式?或者至少当我在应用程序中打开这些系统内容时?

Is it possible to configure the style of these Android system stuff like volume? Or at least when I open these system stuff in my app?

按照注释中的建议,我已经覆盖了 onKeyDown ,但是AdjustStreamVolume仅在0和1之间切换.

As recommended in the comments, I've overwritten onKeyDown, but the adjustStreamVolume switches between 0 and 1 only.

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
    when (event?.keyCode) {
        KeyEvent.KEYCODE_VOLUME_UP -> {
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE)
            return true
        }
        KeyEvent.KEYCODE_VOLUME_DOWN -> {
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE)
            return true
        }
        else -> {
            return super.onKeyDown(keyCode, event)
        }
    }
}

streamVolume确实增加.不幸的是,当我连续按下Volume_up时,"getStreamVolume()"值保持为t 1

The streamVolume does not increase well. Unfortunately, when I press continuously Volume_up, the 'getStreamVolume()' value remains t 1

推荐答案

音量"对话框

对于该卷,使用 android-hide-volume-change-bar-from-device ,您可以将其替换为简单的内容,例如活动中的进度条,因为Android为此提供了界面.

Volume dialog

For the volume, it is very easy to replace the volume dialog with a custom one, from android-hide-volume-change-bar-from-device you can replace it with something simple such as a progressbar in your activity, because Android provide an interface for it.

您也可以(如果很有动力)为传入的通知创建自定义显示.您将需要有一个服务来实现 NotificationListenerService 的运行,拦截通知,然后将其发送到主要活动中,以所需方式显示它们.

You can also (if you are very motivated) have a custom display for the incoming notifications. You will need to have a service implementing NotificationListenerService running, it will have to intercepts the notifications, and send them to the main activity to display them the way you want.

其他功能(例如电源菜单)由于明显的原因将无法更改.

Others, such as the power menu will not be changeable for obvious reasons.

未经root许可,您无法修改android系统的样式,因为它是在系统应用中编码的.

You cannot modify the style of the android system without root permission, because it is encoded in the system app.

Android为您提供了不同的方式来更改其行为,但是它有其局限性.

Android offers you different way to change its behavior but it has its limitations.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ProgressBar
        android:id="@+id/progress_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

</androidx.constraintlayout.widget.ConstraintLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity {

    AudioManager manager;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        progressBar = findViewById(R.id.progress_horizontal);
        updateVolume();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                updateVolume();
                manager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                        AudioManager.ADJUST_RAISE,
                        AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                updateVolume();
                manager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                        AudioManager.ADJUST_LOWER,
                        AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                return true;

            default:
                return super.onKeyDown(keyCode, event);
        }
    }

    private void updateVolume()
    {
        int currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        progressBar.setProgress((int) ((float) currentVolume/maxVolume*100));
    }
}

编辑

有人说原始音量条仍然可见.我直接尝试了在S10 Android 10(三星框架)上提供的代码,默认音量栏不可见.因此,我也在运行LineageOS 10的S4上进行了尝试,结果相同.没有音量栏出现.

Edit

Some people said the original volume bar is still visible. I directly tried the code I provided on my S10 Android 10 (Samsung framework) and the default volume bar is not visible. Therefore I also tried it on my S4 running LineageOS 10, and same result. No volume bar appear.

关于 adjustStreamVolume adjustVolume ,它们都允许您更改音量频道,但是 adjustStreamVolume 允许您更改特定的频道,例如媒体,通话等.在我的示例中,它仅编辑媒体.

About the adjustStreamVolume or adjustVolume they both allow you to change volume channel but the adjustStreamVolume allow you to change a specific one for example, media, call etc. In my example it is only editing the media.

这篇关于应用程序中的Android系统小部件是否可配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆