2

我想在插入耳机后显示通知,并在拔下耳机后隐藏它。因此,我在一个在 BOOT_COMPLETED 触发的 BroadCastReceiver 中注册了 ACTION_HEADSET_PLUG Intent Listener。在我的情况下,我使用 HTC Sense (HTC One S),并且只有在 Sense 正在加载 (WaitDialog) 时插入耳机时才会显示通知,之后它将不再显示。

如果我在运行时通过“new OnBootreceiver().onReceive(this, null)”触发应用程序中的 OnBootListener,它会完美运行。

显现:

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

OnBoot接收器:

public void onReceive(Context context, Intent intent) {
    IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    HeadsetStateReceiver receiver = new HeadsetStateReceiver();
    context.registerReceiver( receiver, receiverFilter );
}

耳机状态接收器:

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

}
4

2 回答 2

2

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

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

你的代码也有点乱:

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


好的直接接收器不起作用(这里有很好的解释)。

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

于 2012-07-16T10:35:46.737 回答
1

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

ReceiveBroadcast receiver=new ReceiveBroadcast();       
registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); 
于 2015-03-19T15:16:28.753 回答