Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 按下电源按钮时播放音频_Android_Audio - Fatal编程技术网

Android 按下电源按钮时播放音频

Android 按下电源按钮时播放音频,android,audio,Android,Audio,我在我的应用程序中编写了此代码片段 Receiver.java if (action.equals(Intent.ACTION_SCREEN_ON) && am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) { if(u!=null) { stopPlaying(); mp = MediaPlayer.create(context, u); mp.start();

我在我的应用程序中编写了此代码片段

Receiver.java

if (action.equals(Intent.ACTION_SCREEN_ON) && am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL)
{
    if(u!=null) {
        stopPlaying();
        mp = MediaPlayer.create(context, u);
        mp.start();
    }
}
else if (action.equals(Intent.ACTION_SCREEN_OFF) && am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL)
{
    if(u2!=null) {
        stopPlaying();
        mp = MediaPlayer.create(context, u2);
        mp.start();
    }
}
}

private void stopPlaying() {
if (mp != null) {
    mp.stop();
    mp.release();
    mp = null;
}
}
@Override
public void onReceive(Context context, Intent intent) {

    // TODO Auto-generated method stub

    Log.v("onReceive", "Power button is pressed.");

    Toast.makeText(context, "power button clicked", Toast.LENGTH_LONG)
            .show();

    // perform what you want here

}
它工作得很好,但我注意到当屏幕关闭时,音频播放有延迟。我认为这是因为设备在启动到关闭状态之前会使屏幕变暗

我希望它在点击触发关闭状态的动作(如点击电源按钮、手势或双击以唤醒和睡眠)时播放关闭音频。在这种情况下,我希望它在电源按钮被触发时播放

有没有办法做到这一点?

您可以添加此代码

首先,您需要向清单文件添加以下权限:

<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
广播接收机

在menifest文件中

<receiver android:name=".MyReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_OFF" >
            </action>
            <action android:name="android.intent.action.SCREEN_ON" >
            </action>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" >
            </action>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" >
            </action>
            <action android:name="android.intent.action.ACTION_SHUTDOWN" >
            </action>
        </intent-filter>
    </receiver>

你必须检查你的屏幕灯是开着还是关着。当你检测到,那么必须有一个接收器,它会告诉你是屏幕上或的,然后你可以播放你的音频。或者你想做什么就做什么。你能告诉我如何实现吗?我能在BroadcastReceiver中定义这个方法吗?因为即使在应用程序终止后,我的接收器仍会播放音频。我尝试将action.equals(Intent.action\u SCREEN\u OFF)更改为.action.equals(action\u POWER\u DISCONNECTED),但它似乎不起作用。哦,据我所知,你给我的接收器是用来给你的设备充电/解锁的。我想做的是,当用户按下锁定或解锁按钮时,将播放音频。
@Override
public void onReceive(Context context, Intent intent) {

    // TODO Auto-generated method stub

    Log.v("onReceive", "Power button is pressed.");

    Toast.makeText(context, "power button clicked", Toast.LENGTH_LONG)
            .show();

    // perform what you want here

}