我正在尝试一整天,但是我的广播接收器在按下媒体按钮时没有收到任何信息
我在下面粘贴代码:
来自我的onCreate()我的服务:
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
context = getApplicationContext();
am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
来自onHandleIntent方法的:
protected void onHandleIntent(Intent intent) {
am.registerMediaButtonEventReceiver(new ComponentName(this, RemoteReceiver.class));
while(check){
try{
switch(ac_time){
case 15 : Thread.sleep(15000);
break;
case 30: Thread.sleep(30000);
break;
case 60 : Thread.sleep(60000);
break;
case 120: Thread.sleep(120000);
break;
case 300: Thread.sleep(300000);
break;
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
if(!pm.isScreenOn())
break;
}
Log.d("service1", "Screen is now off!");
if(check)
mSensorManager.registerListener(Myservice.this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
while(check){
Log.d("service1", "Maximum Value: " +mProximity.getMaximumRange());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onSensorChanged(SensorEvent event) {
Log.d("service1", "Current value is:" +event.values[0] );
}
来自onDestroy()的
am.unregisterMediaButtonEventReceiver(new ComponentName(this, RemoteReceiver.class));
mSensorManager.unregisterListener(this);
来自清单:
<receiver android:name=".RemoteReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
来自收件人班:
public class RemoteReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d("service1", "Broadcast recieved");
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if ( event.getKeyCode()==KeyEvent.KEYCODE_VOLUME_UP) {
Log.d("service1","Volume Up Pressed");
context.stopService(intent);
}
if ( event.getKeyCode()==KeyEvent.KEYCODE_VOLUME_DOWN) {
Log.d("service1","Volume Down Pressed");
context.stopService(intent);
}
if ( event.getKeyCode()==KeyEvent.KEYCODE_POWER) {
Log.d("service1","Power Pressed");
context.stopService(intent);
}
}
在按下音量按钮和激活服务后,我没有从接收器类获取任何日志,而接近传感器正在提供日志。
有什么帮助吗?
更新:
我也发布了一个日志,以防万一:
08-01 22:36:26.022: D/service1(14219): Startservice invoked
08-01 22:36:26.095: D/NotificationPresenter(1559): Notification posted: package=com.saurabh.carefreetravel id=0 user_id=0 tag=null post_time=1406912786071 is_valid_global=false is_valid_local=true
08-01 22:36:26.135: I/MediaFocusControl(1030): Remote Control registerMediaButtonIntent() for PendingIntent{425e7c00: PendingIntentRecord{422fa478 com.saurabh.Myfirstapp broadcastIntent}}
08-01 22:36:29.507: I/ActivityManager(1030): Killing 13303:de.robv.android.xposed.installer/u0a122 (adj 15): empty #17
08-01 22:36:30.260: E/QCALOG(658): [MessageQ] ProcessNewMessage: [XT-CS] unknown deliver target [OS-Agent]
08-01 22:36:30.261: E/QCALOG(658): [MessageQ] ProcessNewMessage: [XTWWAN-PE] unknown deliver target [OS-Agent]
08-01 22:36:30.261: E/QCALOG(658): [MessageQ] ProcessNewMessage: [XTWiFi-PE] unknown deliver target [OS-Agent]
08-01 22:36:30.604: D/AlertService(14353): Beginning updateAlertNotification
08-01 22:36:30.619: D/AlertService(14353): No fired or scheduled alerts
08-01 22:36:30.632: D/AlertService(14353): Scheduling next alarm with AlarmScheduler. sEventReminderReceived: null
08-01 22:36:30.644: D/AlarmScheduler(14353): Query result count for events starting within 1 week: 1
08-01 22:36:30.658: D/AlarmScheduler(14353): Scheduling alarm for EVENT_REMINDER_APP broadcast for event 1 at 1406946601000 (Sat, Aug 02, 2014 08:00am)
08-01 22:36:30.677: I/ActivityManager(1030): Killing 13397:com.ceco.kitkat.gravitybox/u0a123 (adj 15): empty #17
08-01 22:36:30.738: D/AudioPolicyManager(292): getOutput() device 2, stream 2, samplingRate 0, format 0, channelMask 3, flags 0
08-01 22:36:30.738: D/AudioPolicyManager(292): getOutput() returns output 2
08-01 22:36:30.738: D/AudioPolicyManager(292): getOutput() device 2, stream 2, samplingRate 0, format 0, channelMask 3, flags 0
08-01 22:36:30.739: D/AudioPolicyManager(292): getOutput() returns output 2
08-01 22:36:30.739: D/AudioPolicyManager(292): getOutput() device 2, stream 2, samplingRate 48000, format 1, channelMask 1, flags 4
08-01 22:36:30.739: D/AudioPolicyManager(292): getOutput() returns output 3
08-01 22:36:30.741: D/AudioFlinger(292): start(4097), calling pid 1030 session 78
08-01 22:36:30.741: D/AudioFlinger(292): ? => ACTIVE (4097) on thread 0xb8128f40
08-01 22:36:30.748: D/audio_hw_primary(292): start_output_stream: enter: stream(0xb811ed70)usecase(1: low-latency-playback) devices(0x2)
08-01 22:36:30.748: E/audio_hw_primary(292): voice_extn_compress_voip_is_active: COMPRESS_VOIP_ENABLED is not defined
08-01 22:36:30.748: E/msm8974_platform(292): voice_extn_compress_voip_is_active: COMPRESS_VOIP_ENABLED is not defined
08-01 22:36:30.748: D/audio_hw_primary(292): select_devices: out_snd_device(2: speaker) in_snd_device(0: )
08-01 22:36:30.748: D/hardware_info(292): hw_info_append_hw_type : device_name = speaker
08-01 22:36:30.751: D/audio_hw_primary(292): select_devices: done
08-01 22:36:32.843: D/AudioFlinger(292): start(4097), calling pid 1030 session 78
08-01 22:36:32.843: D/AudioFlinger(292): ? => ACTIVE (4097) on thread 0xb8128f40
08-01 22:36:35.260: E/QCALOG(658): [MessageQ] ProcessNewMessage: [XT-CS] unknown deliver target [OS-Agent]
08-01 22:36:35.261: E/QCALOG(658): [MessageQ] ProcessNewMessage: [XTWWAN-PE] unknown deliver target [OS-Agent]
08-01 22:36:35.261: E/QCALOG(658): [MessageQ] ProcessNewMessage: [XTWiFi-PE] unknown deliver target [OS-Agent]
08-01 22:36:35.923: D/audio_hw_primary(292): out_standby: enter: stream (0xb811ed70) usecase(1: low-latency-playback)
08-01 22:36:35.954: D/hardware_info(292): hw_info_append_hw_type : device_name = speaker
: E/(): Device disconnected