2014-10-27 34 views
5

Ho un servizio nella mia applicazione che funziona su back-end, il servizio può essere avviato quando premo il volume del tasto su e fermato quando premo il volume del pulsante.pulsante volume di ascolto quando il telefono è bloccato Android

public class SettingsContentObserver extends ContentObserver { 


int previousVolume; 
Context context; 

public SettingsContentObserver(Context c, Handler handler) { 
    super(handler); 
    context=c; 

    AudioManager audio = (AudioManager)  context.getSystemService(Context.AUDIO_SERVICE); 
    previousVolume = audio.getStreamVolume(AudioManager.STREAM_RING); 

} 



@Override 
public boolean deliverSelfNotifications() { 
    return super.deliverSelfNotifications(); 
} 

@Override 
public void onChange(boolean selfChange) { 
    super.onChange(selfChange); 

    AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
    int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING); 

    int delta=previousVolume-currentVolume; 



     if(delta > 0) 
     { 
      System.out.println("Decreased") ; 
      Intent intent = new Intent(context, MyAlarmService.class); 
      context.stopService(intent); 
      previousVolume=currentVolume; 
     } 
     else if(delta < 0) 
     { 
       System.out.println("Increased"); 
       Intent intent = new Intent(context, MyAlarmService.class); 
       context.startService(intent); 
       previousVolume=currentVolume; 
     } 


} 
} 

voglio fare le stesse cose quando il mio telefono è bloccato (quando lo schermo è spento), quando cerco in rete trovo che devo usare un BroadcastReceivre, provo, ma non lo fa lavoro.

public class YourBoardcastReceiver extends BroadcastReceiver { 
    @Override 
public void onReceive(Context context, Intent intent) { 


    Log.e("get something", "i dont know what!!"); 

    String intentAction = intent.getAction(); 
    KeyEvent event = null; 
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { 
     event = (KeyEvent) intent 
      .getParcelableExtra(Intent.EXTRA_KEY_EVENT); 
    } 

    if (event == null) { 
     return; 
    } 

    int keycode = event.getKeyCode(); 
    int action = event.getAction(); 
    long eventtime = event.getEventTime(); 

    if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE 
     || keycode == KeyEvent.KEYCODE_HEADSETHOOK) { 
    if (action == KeyEvent.ACTION_DOWN) { 

      Intent intent1 = new Intent(context, MyAlarmService.class); 
      context.stopService(intent1); 

     if (isOrderedBroadcast()) { 
      abortBroadcast(); 
     } 

     else if (action == KeyEvent.ACTION_UP) { 

       Intent intent11 = new Intent(context, MyAlarmService.class); 
       context.startService(intent11); 

     } 

      if (isOrderedBroadcast()) { 
       abortBroadcast(); 


    } 
} 
} 
} 


} 

In manifest di Android aggiungo:

<receiver android:name="YourBoardcastReceiver"> 
      <intent-filter> 
        <action android:name="android.intent.action.SCREEN_ON" /> 
      </intent-filter> 
    </receiver> 

E nel mainActivity sul Methode oncrete aggiungo:

AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
ComponentName mRemoteControlResponder = new ComponentName(getPackageName(), 
YourBoardcastReceiver.class.getName()); 

Motivi Can me dare qualche aiuto, pensa.

risposta

0

Prova registri con il ricevitore broadcast per Intent.ACTION_SCREEN_OFF e Intent.ACTION_SCREEN_ON

5

Quando il telefono è bloccato modalità/il sonno non siamo in grado di ascoltare i pulsante cambia, comunque siamo in grado di fare qualche hack per ascoltare pulsante del volume.

Github: https://github.com/praslnx8/volume_Screen_Lock

queste applicazioni stanno giocando media con 0 Volume in background e quindi il tasto sarà sempre ascoltato dal ricevente con MediaButtonIntent