1

我开始为傻瓜阅读 Android,我正在尝试制作我的第一个应用程序,即静音模式切换。

现在我面临的问题是:

在布局中,我需要的东西应该只有一个按钮。如果手机处于静音状态,则文本应显示“已激活静音模式”,而当手机处于正常模式时,应显示“已激活正常模式”。

它应该询问用户他/她是否想要激活振动模式。

下面是我的代码:

XML 布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >    

    <ImageView
        android:id="@+id/phone_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="7dp"
        android:src="@drawable/phone_on" />

    <Button
        android:id="@+id/silent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="35dp"
        android:text="@string/silent_off" />

</LinearLayout>

Java 代码

public class MainActivity extends Activity {
    private AudioManager mAudioManager;
    private boolean mPhoneIsSilent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);
        mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        checkIfPhoneIsSilent();
        setButtonClickListener();
        Log.d("SilentModeApp", "This is a test");
    }    

    private void setButtonClickListener() {
        Button toggleButton = (Button)findViewById(R.id.silent);
        toggleButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (mPhoneIsSilent) {
                    //change back to normal mode
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    mPhoneIsSilent = false;
                }
                else
                {
                    //change to silent mode
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                    mPhoneIsSilent = true;
                }
                // Now toggle the UI again
                toggleUi(); 
            }
        });
    }

    private void checkIfPhoneIsSilent() {
        int ringermode = mAudioManager.getRingerMode();
        if (ringermode == AudioManager.RINGER_MODE_SILENT) {
            mPhoneIsSilent = true;
        }
        else
        {
            mPhoneIsSilent = false;
        }
    }

    private void toggleUi() {
        ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
        Drawable newPhoneImage;
        if (mPhoneIsSilent){
            newPhoneImage = 
                    getResources().getDrawable(R.drawable.phone_silent);
        }
        else
        {
            newPhoneImage = 
                    getResources().getDrawable(R.drawable.phone_on);

        }
        imageView.setImageDrawable(newPhoneImage);
    }

    @Override      
    protected void onResume() {
        super.onResume();
        checkIfPhoneIsSilent();
        toggleUi();
    }
}

更新

现在我删除了“mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);”,它从正常模式进入振动模式。

我需要布局方面的帮助。

我需要 3 个按钮和一个文本状态文本 - 我知道如何创建按钮和文本消息,但我不知道如何将它与 java 集成。

如果手机处于正常模式,我需要在按钮上方的图片下方显示一条状态消息:(XXX 模式已激活)其中 XXX 是静音、正常或振动。

如果手机处于正常模式,则应该只有 2 个按钮:切换到静音模式和切换到振动模式。

其他模式也是如此。

4

2 回答 2

1

听起来您想要一个文本视图来说明静音模式是否已激活/未激活?即只是按钮附近的一小段文字,显示状态?你的按钮也会动态出现/消失吗?

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...

于 2012-09-02T18:41:25.563 回答
0
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();
    }

}
于 2020-02-18T16:38:32.463 回答