我该如何在我的Android APK中修复“下一个媒体密钥”

时间:2019-06-23 10:54:16

标签: java android media-player next programmatically

我想以编程方式在我的apk中执行“下一个媒体键”,以在当前播放的音乐播放器中播放下一首歌曲。我根本无法正常工作。我尝试了3个选项(请参见下面的代码),但没有任何效果。从终端(直接或通过adb)发出“ input keyevent 87”输入可以正常工作。从/ system / bin运行脚本也可以正常工作。

我尝试在2个Android车载主机上运行此程序。一个运行Android 6.0.1,另一个运行Android 8.0.0。两者都是有根的,但是我不使用它,我认为我不需要使用它。

编辑:我首先尝试了OPTION 0。

然后我在下面的代码中进行“ // FIRST OPTION TRIED”。 然后我通过几个shell命令选项(下面的代码中的选项2和3)进行了尝试。 什么都没有。我以为我做些愚蠢的事,但我不知道该怎么办。

我的apk示例

<li v-if="is_logged"><router-link tag="a" :to="{ name: 'Statistics' }">
  Statistics
</router-link></li>

Closer.java源文件

package org.hvdw.mediakeynext;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.content.Context;
import android.content.Intent;
//import android.media.AudioManager;
//shellExec
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;

public class MediaKeyNext extends Activity {
    public static final String TAG = "MediaKeyNext";
    private static Context mContext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getApplicationContext();

        Log.d(TAG, "Started MediaKeyNext; in OnCreate void");
        // OPTION 0
        /*AudioManager mAudioManager = (AudioManager) mContext.getSystemService(mContext.AUDIO_SERVICE);
        if (mAudioManager.isMusicActive()) {
            Intent sIntent = new Intent("com.android.music.musicservicecommand");
            sIntent.putExtra("command" , "next" );
            mContext.sendBroadcast(sIntent); 
        }*/


        // FIRST OPTION TRIED
        /*AudioManager mAudioManager = (AudioManager) mContext.getSystemService(mContext.AUDIO_SERVICE);
        Intent sIntent = new Intent("com.android.music.musicservicecommand");
        sIntent.putExtra("command" , "next" );
        mContext.sendBroadcast(sIntent); */

        // SECOND OPTION TRIED
        //shellExec("/system/bin/mediakeynext.sh");

        // THIRD OPTION TRIED
        try {
            Process p = Runtime.getRuntime().exec("/system/bin/mediakeynext.sh");
            Log.d(TAG, "/system/bin/mediakeynext.sh");
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, e.getMessage());
        }

        Log.d(TAG, "Executed the mediakeynext command");

        finish();
    }


    // USED METHODS FOR OPTION 2
    public static String shellExec(String... strings) {
        String res = "";
        DataOutputStream outputStream = null;
        InputStream response = null;
        try {
            Process sh = Runtime.getRuntime().exec("sh");
            outputStream = new DataOutputStream(sh.getOutputStream());
            response = sh.getInputStream();

            for (String s : strings) {
                s = s.trim();
                outputStream.writeBytes(s + "\n");
                outputStream.flush();
            }

            outputStream.writeBytes("exit\n");
            outputStream.flush();
            try {
                sh.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            res = readFully(response);
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, e.getMessage());
        } finally {
            Closer.closeSilently(outputStream, response);
        }
        return res;
    }

    public static String readFully(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = is.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }
        return baos.toString("UTF-8");
    }
}

使用的shell脚本

package org.hvdw.mediakeynext;

import java.io.Closeable;
import java.net.DatagramSocket;
import java.net.Socket;

//copied from https://stackoverflow.com/questions/20932102/execute-shell-command-from-android/26654728
// from the code of CarloCannas

public class Closer {
    // closeAll()
    public static void closeSilently(Object... xs) {
        // Note: on Android API levels prior to 19 Socket does not implement Closeable
        for (Object x : xs) {
            if (x != null) {
                try {
                    //Log.d("closing: "+x);
                    if (x instanceof Closeable) {
                        ((Closeable)x).close();
                    } else if (x instanceof Socket) {
                        ((Socket)x).close();
                    } else if (x instanceof DatagramSocket) {
                        ((DatagramSocket)x).close();
                    } else {
                        //Log.d("cannot close: "+x);
                        throw new RuntimeException("cannot close "+x);
                    }
                } catch (Throwable e) {
                    //Log.x(e);
                }
            }
        }
    }
}

不显示错误消息。 日志仅显示

#!/system/bin/sh

input keyevent 87

0 个答案:

没有答案