우공이산(愚公移山)

자신과 세상을 바꾸는 것은 머리좋고 가진것이 많은 사람이 아니라 결코 포기하지 않는 의지로 꾸준히 노력해 가는 사람이다. 오늘이 쌓여 내일을 만들고, 내일이 쌓여 인생을 만든다.

Code Story

미디어 컨트롤을 위한 이벤트 발생.

보노보노 2022. 4. 18. 08:46

1. 권한

<uses-permission android:name="android.permission.INJECT_EVENTS" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.intent.action.MEDIA_BUTTON" />

* 권한이 필요하지 않은것으로 보임

 

2. 버튼을 누르면 이벤트를 브로드케스팅!

package com.example.youbackplayer;

import androidx.appcompat.app.AppCompatActivity;


import android.app.Instrumentation;
import android.os.Bundle;
import android.content.Intent;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    public int isPlaying = 0;

    final Handler mHandler = new Handler();

    Runnable mGoHome = new Runnable(){
        public void run(){
            Intent intent_home = new Intent(Intent.ACTION_MAIN); //태스크의 첫 액티비티로 시작
            intent_home.addCategory(Intent.CATEGORY_HOME);   //홈화면 표시
            intent_home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //새로운 태스크를 생성하여 그 태스크안에서 액티비티 추가
            startActivity(intent_home);

            finish();  // 브라우저만 띄우고 종료
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//        // 1. 브라우저띄우기
//        String my_url = "https://youtu.be/4_QyLhP5qHs";
//        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(my_url));
//        intent.setPackage("com.android.chrome");   // 브라우저가 여러개 인 경우 콕 찍어서 크롬을 지정할 경우
//        startActivity(intent);
//
//
//        // 2. 영상플레이시키기
//        Intent intent2 = new Intent("com.android.chrome");
//        intent2.putExtra("command", "next");
//        sendBroadcast(intent2);
//
//
//        // 3. 홈화면으로 빠지기
////        mHandler.postDelayed(mGoHome, 30000);
//        //mHandler.removeCallbacks(mGoHome);
//
//
//        // 4. Next영상으로 이동
////        putCommand(KeyEvent.KEYCODE_MEDIA_NEXT); // 다음 곡
//        new Thread(new Runnable() {
//            public void run() {
//                new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_NEXT);
//            }
//        }).start();

        Button btn01 = (Button) findViewById(R.id.btn);
        Button btn02 = (Button) findViewById(R.id.btn2);
        Button btn03 = (Button) findViewById(R.id.btn3);
        Button btn04 = (Button) findViewById(R.id.btn4);
        Button btn05 = (Button) findViewById(R.id.btn5);

        //볼륨업
        btn01.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new Thread(new Runnable() {
                    public void run() {
                        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_VOLUME_UP);
                    }
                }).start();
            }
        });

        //볼륨다운
        btn02.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new Thread(new Runnable() {
                    public void run() {
                        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_VOLUME_DOWN);
                    }
                }).start();
            }
        });

        //플레이/스탑
        btn03.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new Thread(new Runnable() {
                    public void run() {
//                        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
                        if(isPlaying == 1){
                            new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_PAUSE);
                            isPlaying = 0;
                        } else {
                            new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_PLAY);
                            isPlaying = 1;
                        }

                    }
                }).start();
            }
        });

        //넥스트
        btn04.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new Thread(new Runnable() {
                    public void run() {
                        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_NEXT);
                    }
                }).start();
            }
        });

        //프리브
        btn05.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new Thread(new Runnable() {
                    public void run() {
                        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_PREVIOUS);
                    }
                }).start();
            }
        });
    }
}