我开始阅读Android for dummies,我正在尝试制作我的第一个应用程序,即静音模式切换。
现在我遇到的问题是:
在布局中,我需要的东西应该只有一个按钮。如果手机处于静音状态,则文本应显示为“静音模式已激活”,并且当手机处于正常模式时“正常模式已激活”。
应该询问用户他/她是否想要激活振动模式。
以下是我的代码:
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个按钮:切换到静音模式并切换到振动模式。
与其他模式也是如此。
答案 0 :(得分: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
}
等...
答案 1 :(得分: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();
}
}