首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >注册的ACTION_HEADSET_PLUG监听器仅触发几次

注册的ACTION_HEADSET_PLUG监听器仅触发几次
EN

Stack Overflow用户
提问于 2012-07-16 18:07:07
回答 2查看 3.9K关注 0票数 2

我想要在插入耳机后显示通知,并在拔出耳机后隐藏它。因此,我在一个在BOOT_COMPLETED触发的BroadCastReceiver中注册了ACTION_HEADSET_PLUG意图侦听器。在我的例子中,我使用HTC Sense (HTC One S),只有当我插入耳机,而Sense正在加载(WaitDialog)时,通知才会显示,之后它就不会再显示了。

如果我在运行时通过‘OnBootListener OnBootreceiver().onReceive(this,null)’触发应用程序中的新函数,它就能完美地工作。

清单:

代码语言:javascript
复制
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        ...
    </activity>

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

OnBootReceiver:

代码语言:javascript
复制
public void onReceive(Context context, Intent intent) {
    IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    HeadsetStateReceiver receiver = new HeadsetStateReceiver();
    context.registerReceiver( receiver, receiverFilter );
}

HeadsetStateReceiver:

代码语言:javascript
复制
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context,
            "" + (Integer) (intent.getExtras().get("state")),
            Toast.LENGTH_SHORT).show();
    mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Log.e(this.getClass().toString(), "Intent Headset_plug received "
            + intent);
    if ((Integer) (intent.getExtras().get("state")) != 0) {
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        notification.contentIntent = PendingIntent.getActivity(
                context.getApplicationContext(), 0, new Intent(), 0);
        notification.setLatestEventInfo(context, title, message,
                notification.contentIntent);
        mNotificationManager.notify(0xBEA15, notification);
    } else {
        mNotificationManager.cancelAll();
    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-16 18:35:47

为什么需要BOOT_COMPLETED接收器?您是否尝试使用清单文件接收ACTION_HEADSET_PLUG?

代码语言:javascript
复制
<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
        ... 
    </activity> 

    <receiver android:name=".HeadsetStateReceiver" > 
        <intent-filter> 
            <action android:name="android.intent.ACTION_HEADSET_PLUG" /> 
        </intent-filter> 
    </receiver> 

你的代码也有点小气:

代码语言:javascript
复制
public void onReceive(Context context, Intent intent) {
    boolean isConnected = intent.getIntExtra("state",0)==1; // Can also be 2 if headset is attached w/o mic.

    Toast.makeText(context, 
            isConnected?"connected":"disconnected"), 
            Toast.LENGTH_SHORT).show(); 
    mNotificationManager = (NotificationManager) context 
        .getSystemService(Context.NOTIFICATION_SERVICE); 

    if (isConnected) {
        Log.e(this.getClass().toString(), "Intent Headset_plug connected");
        notification.flags = Notification.FLAG_ONGOING_EVENT; 
        notification.contentIntent = PendingIntent.getActivity( 
                context.getApplicationContext(), 0, new Intent(), 0); 
        notification.setLatestEventInfo(context, title, message, 
                notification.contentIntent); 
        mNotificationManager.notify(0xBEA15, notification); 
    } else {
        Log.e(this.getClass().toString(), "Intent Headset_plug disconnected");
        mNotificationManager.cancelAll(); 
    } 
} 

Ok直接接收器将不工作(here is good explanation)。

我想我知道为什么你的接收器在一段时间后停止工作。您的应用程序必须由系统清理。为了让它工作,你必须做一些事情来阻止你的应用程序被清理。所以你必须创建一个服务。在BOOT_COMPLETED中启动一个服务,并在该服务中为ACTION_HEADSET_PLUG注册广播接收器。这应该可以防止应用程序被清理。

票数 2
EN

Stack Overflow用户

发布于 2015-03-19 23:16:29

您的代码是正确的,但据我所知,您不能将HEADSET_PLUG过滤器放在清单上。取而代之的是,从清单中删除接收器,并以编程方式从应用程序的某个部分执行此操作。主活动的onCreate()方法应该没问题,例如:

代码语言:javascript
复制
ReceiveBroadcast receiver=new ReceiveBroadcast();       
registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11502187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档