监听器使用服务而不活动的媒体按钮(耳机钩) [英] Listener for media button (headset-hook) using service without Activity

查看:849
本文介绍了监听器使用服务而不活动的媒体按钮(耳机钩)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能使服务能聆听到Android设备上的媒体按钮(耳机挂机)。
我尝试这样做,使用这个code,但没有奏效。
你能帮助我。

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.

服务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();
                        }
                    });

                }
            }
        }

    }

}

这是reciver code:

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);               
        }

}

这是明显的:

<?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>

感谢您提前。

推荐答案

您服务,期待一个意图 ACTION_MEDIA_BUTTON 意图的行动。您正在使用不启动该服务这样的意图

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

修改

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

    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.

还有:


  • 摆脱的android:过程=:远程,因为它是没有必要在这里,是用户敌对

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

摆脱你的&LT;意向滤光器&gt; &LT;服务&GT; ,除非你打算为一百万其他应用程序来调用这个服务

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

这篇关于监听器使用服务而不活动的媒体按钮(耳机钩)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆