如何关闭闹钟?

时间:2017-11-14 13:30:09

标签: java android android-mediaplayer

我的应用程序是这样的:

  • 首次启动应用时,您会看到一个按钮。在点击开始按钮之后,连接或断开AC充电器不会影响任何事情。
  • 点击开始按钮后,将启动“服务”。
  • 现在,如果您取下AC充电器,您将听到警报声。
  • 再次将AC与设备连接应停止闹钟,但
  • 现在再次移除AC会启动另一个警报。警报重叠。
你可以帮我找到这个bug吗?

感谢

到目前为止我的工作:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yousef.mustafa.antitheft">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/antitheft"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/antitheft"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".PowerConnectionReceiver">

        </receiver>

    </application>

</manifest>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button startServiceButton;
    PowerConnectionReceiver powerConnectionReceiver;
    BroadcastReceiver powerDisconnectedBroadcastReceiver;
    BroadcastReceiver powerConnectedBroadcastReceiver;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        powerConnectionReceiver = new PowerConnectionReceiver(true);
        startServiceButton = (ToggleButton) findViewById(R.id.startServiceButton);

        powerDisconnectedBroadcastReceiver = new PowerConnectionReceiver(true);
        powerConnectedBroadcastReceiver = new PowerConnectionReceiver(false);

        startServiceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (startServiceButton.isActivated()) {
                    unregisterReceiver(powerDisconnectedBroadcastReceiver);
                    unregisterReceiver(powerConnectedBroadcastReceiver);
                    Toast.makeText(MainActivity.this, "Service stopped", Toast.LENGTH_SHORT).show();
                    startServiceButton.setActivated(false);
                } else {
                    monitorBatteryChanges();
                }
            }
        });
    }

    private void monitorBatteryChanges() {

        IntentFilter powerDisconnectedIntentFilter = new IntentFilter("android.intent.action.ACTION_POWER_DISCONNECTED");
        registerReceiver(powerDisconnectedBroadcastReceiver, powerDisconnectedIntentFilter);

        IntentFilter powerConnectedIntentFilter = new IntentFilter("android.intent.action.ACTION_POWER_CONNECTED");
        registerReceiver(powerConnectedBroadcastReceiver, powerConnectedIntentFilter);

        Toast.makeText(MainActivity.this, "Service started", Toast.LENGTH_SHORT).show();
        startServiceButton.setActivated(true);
    }

}

PowerConnectionReceiver.java

public class PowerConnectionReceiver extends BroadcastReceiver {

    MediaPlayer mediaPlayer = new MediaPlayer();

    // Determine whether the AC is disconnected or not
    private boolean POWER_DISCONNECTED = true;

    PowerConnectionReceiver(boolean disconnected) {
        this.POWER_DISCONNECTED = disconnected;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (POWER_DISCONNECTED) {
            mediaPlayer = MediaPlayer.create(context, R.raw.alarm_2);
            Toast.makeText(context, "Alarm started", Toast.LENGTH_SHORT).show();
            mediaPlayer.start();
            mediaPlayer.setLooping(true);
            //POWER_DISCONNECTED = false;
        } else {
            try {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                    Toast.makeText(context, "Alarm stopped", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}

2 个答案:

答案 0 :(得分:1)

该错误存在于您的复杂代码中。

您注册了PowerConnectionReceiver一个用于&#34;已连接&#34;的两个不同实例另一个用于&#34;断开&#34;事件。因此,MediaPlayer在&#34;已连接&#34;中启动接收器,你试图阻止MediaPlayer进入&#34; disconected&#34;,但它没有播放。

您应该删除POWER_DISCONNECTED字段,并在单个接收器实例中处理事件。您可以为多个事件注册单个接收器,并使用intent.getAction()检查如何处理它。

<强> UPD
您还可以使接收器单例,防止多次注册,以及播放多个MediaPlayer s

答案 1 :(得分:0)

我终于找到了解决方案。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button startServiceButton;
    BroadcastReceiver powerConnectionBroadcastReceiver;
    IntentFilter powerDisconnectedIntentFilter = new IntentFilter("android.intent.action.ACTION_POWER_DISCONNECTED");
    IntentFilter powerConnectedIntentFilter = new IntentFilter("android.intent.action.ACTION_POWER_CONNECTED");


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startServiceButton = (ToggleButton) findViewById(R.id.startServiceButton);
        powerConnectionBroadcastReceiver = new PowerConnectionBroadcastReceiver();

        startServiceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (startServiceButton.isActivated()) {
                    unregisterReceiver(powerConnectionBroadcastReceiver);
                    startServiceButton.setActivated(false);
                    Toast.makeText(MainActivity.this, "Service stopped", Toast.LENGTH_SHORT).show();
                } else {
                    monitorBatteryChanges();
                }
            }
        });
    }

    private void monitorBatteryChanges() {
        registerReceiver(powerConnectionBroadcastReceiver, powerDisconnectedIntentFilter);
        registerReceiver(powerConnectionBroadcastReceiver, powerConnectedIntentFilter);
        startServiceButton.setActivated(true);
        Toast.makeText(MainActivity.this, "Service started", Toast.LENGTH_SHORT).show();
    }
}

PowerConnectionBroadcastReceiver.java

public class PowerConnectionBroadcastReceiver extends BroadcastReceiver {

    MediaPlayer mediaPlayer;

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
            mediaPlayer = MediaPlayer.create(context, R.raw.alarm_1);
            mediaPlayer.start();
            mediaPlayer.setLooping(true);
            Toast.makeText(context, "Alarm started", Toast.LENGTH_SHORT).show();
        } else {
            try {
                if (mediaPlayer != null)
                mediaPlayer.release();
                Toast.makeText(context, "Alarm stopped", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

它可能不是最好的解决方案,但它有效:)

感谢您的帮助。