Listener for media button (headset-hook) using service without Activity

笑着哭i 提交于 2019-12-14 02:39:59

问题


How can I make service listen for media button (headset-hook) in android device . I try to do that using this code but did not work. Can you help me.

Service code:

public class RLservice extends IntentService {
    private Handler handler;

    public RLservice() {
        super("my service");
        // TODO Auto-generated constructor stub
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handler = new Handler();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    protected void onHandleIntent(Intent arg0) {

        String intentAction = arg0.getAction();
        if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            KeyEvent event = (KeyEvent) arg0
                    .getParcelableExtra(Intent.EXTRA_KEY_EVENT);

            if (event == null) {
                return;
            }

            int keycode = event.getKeyCode();
            int action = event.getAction();
            long eventtime = event.getEventTime();

            if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
                    || keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                if (action == KeyEvent.ACTION_DOWN) {
                    // Start your app here!
                    handler.post(new Runnable() {

                        public void run() {
                            // TODO Auto-generated method stub
                            Toast.makeText(getApplicationContext(), "Welcome",
                                    Toast.LENGTH_LONG).show();
                        }
                    });

                }
            }
        }

    }

}

This is reciver code:

public class RLreciver extends BroadcastReceiver
{


    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        Intent i=new Intent(RLservice.class.getName());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        arg0.startService(i);               
        }

}

This is manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ypu.RLservice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name=".RLreciver" >
            <intent-filter android:priority="100000" >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>
        </receiver>

        <service
            android:name=".RLservice"
            android:process=":remote" >
            <intent-filter>
                <action android:name="com.ypu.RLservice.RLservice" />
            </intent-filter>
        </service>
    </application>

</manifest>

Thank you in advance.


回答1:


Your service is expecting an Intent with the ACTION_MEDIA_BUTTON Intent action. You are not starting the service using such an Intent.

Change:

    Intent i=new Intent(RLservice.class.getName());
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    arg0.startService(i);

to:

    arg1.setClass(this, RLservice.class);
    arg0.startService(arg1);

This will effectively "redirect" the broadcast as a command to your service, with the action and extras intact.

Also:

  • Get rid of android:process=":remote", as it is not necessary here and is user-hostile

  • Get rid of your <intent-filter> in your <service>, unless you intend for a million other apps to be calling this service



来源:https://stackoverflow.com/questions/18423893/listener-for-media-button-headset-hook-using-service-without-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!