【问题标题】:Do something when a headset is plugged in when my app is running in the background. (if possible i want to do it with broadcast receivers)当我的应用程序在后台运行时插入耳机时做一些事情。 (如果可能的话,我想用广播接收器来做)
【发布时间】:2015-03-19 15:06:25
【问题描述】:

当我的应用在后台运行时,我想在插入耳机时做一些事情。 (如果可能的话,我想用广播接收器来做)

我尝试了下面的代码:

--接收广播--

package com.example.openmusiconheadsetconnect;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class ReceiveBroadcast extends BroadcastReceiver {
    public ReceiveBroadcast() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"Received!",Toast.LENGTH_LONG).show();
    }
}

--清单--

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.openmusiconheadsetconnect" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name=".ReceiveBroadcast"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.HEADSET_PLUG" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

谢谢!

【问题讨论】:

标签: android android-intent broadcastreceiver background-process headset


【解决方案1】:

您的代码是正确的,但据我所知,您不能将 HEADSET_PLUG 过滤器放在清单上。 相反,在它自己的类中创建一个接收器,并让它监听清单中的 USER_PRESENT(屏幕解锁)或 BOOT_COMPLETED:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>  
<receiver android:name="classes.myReceiver" >        
             <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
    </receiver>

当被此类事件触发时,您的接收者应该启动服务:

public class myReceiver extends BroadcastReceiver {
    @Override 
    public void onReceive(Context ctx, Intent intent) {     
        Intent service = new Intent(ctx, VoiceLaunchService.class);
        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)||intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            ctx.startService(service);
            }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            ctx.stopService(service);  
            }
        }

服务现在将在其 onCreate 方法中注册将监听 HEADSET_PLUG 意图的接收器:

@Override
public void onCreate() {
    super.onCreate();   
    speechReconRx=new SpeechReconControlReceiver(this);//"this" will allow you to call service's methods from the receiver
    registerReceiver(speechReconRx, new IntentFilter(Intent.HEADSET_PLUG)); 
    }

这很麻烦,但如果您不想使用活动,则需要它。 不让我们将 PLUG 接收器放在清单中是谷歌的错!最后制作将在耳机插入时采取行动的广播。

public class SpeechReconControlReceiver extends BroadcastReceiver {
    @Override 
    public void onReceive(Context ctx, Intent intent) {     
       Log.e("joshcsr","HEADSET PLUGGED!");
        if(intent.getStringExtra("command")!=null){
            c=intent.getStringExtra("command");
            }
        //run some methods from the service
        if (c.equals("resume")) {           
            sService.resume();  
            }

        if (c.equals("pause")) {            
            sService.pause();  
            }

        if (c.equals("stop")) {         
            sService.stop();  
            }       
        }
    }

总结一下,你需要: *引导/屏幕解锁事件的接收器。 *一项服务,用于保存将在后台运行的所有内容并注册您的耳机收听广播。 *还有一个耳机插头的接收器,它将采取行动并调用服务中托管的方法。

我昨天做过这个,它适用于从果冻豆到棒棒糖......甚至可能是更旧的版本。干杯。

【讨论】:

  • 我可以在没有user interface 的情况下创建一个mainactivity 吗?我希望我的应用在后台运行。
  • @SuperThomasLab 当然,方法略有不同。看看我上面的改进答案......
【解决方案2】:

首先,您需要在启动完成后在后台启动应用程序的权限。

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

并在您的广播接收器中指定这一点,

<receiver android:name=".YourBroadcastReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

然后创建一个service 在后台运行您的应用程序,并在服务内部使用AudioManager.isWiredHeadsetOn() 检查耳机是否已插入。如果是,请执行您想要的任务。

while(AudioManager.isWiredHeadsetOn()){
    //your task goes here
}

manifest中也添加权限

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

【讨论】:

  • 我希望我的应用在手机启动时在后台启动。
  • @SuperThomasLab 您希望您的应用在手机启动时启动吗?如果耳机已连接,并在后台执行任务?
  • 是的,没错。我想在耳机插入时启动一个应用程序。
  • @SuperThomasLab 我已经在我的答案中添加了你如何做到这一点,检查我编辑的答案
  • 我想在连接耳机时启动另一个应用程序。所以我的应用需要在后台运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多