我想要在插入耳机后显示通知,并在拔出耳机后隐藏它。因此,我在一个在BOOT_COMPLETED触发的BroadCastReceiver中注册了ACTION_HEADSET_PLUG意图侦听器。在我的例子中,我使用HTC Sense (HTC One S),只有当我插入耳机,而Sense正在加载(WaitDialog)时,通知才会显示,之后它就不会再显示了。
如果我在运行时通过‘OnBootListener OnBootreceiver().onReceive(this,null)’触发应用程序中的新函数,它就能完美地工作。
清单:
...
<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:
public void onReceive(Context context, Intent intent) {
IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
HeadsetStateReceiver receiver = new HeadsetStateReceiver();
context.registerReceiver( receiver, receiverFilter );
}HeadsetStateReceiver:
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();
}
}发布于 2012-07-16 18:35:47
为什么需要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();
}
}
Ok直接接收器将不工作(here is good explanation)。
我想我知道为什么你的接收器在一段时间后停止工作。您的应用程序必须由系统清理。为了让它工作,你必须做一些事情来阻止你的应用程序被清理。所以你必须创建一个服务。在BOOT_COMPLETED中启动一个服务,并在该服务中为ACTION_HEADSET_PLUG注册广播接收器。这应该可以防止应用程序被清理。
发布于 2015-03-19 23:16:29
您的代码是正确的,但据我所知,您不能将HEADSET_PLUG过滤器放在清单上。取而代之的是,从清单中删除接收器,并以编程方式从应用程序的某个部分执行此操作。主活动的onCreate()方法应该没问题,例如:
ReceiveBroadcast receiver=new ReceiveBroadcast();
registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); https://stackoverflow.com/questions/11502187
复制相似问题