android - Lock Screen Volume Control with CCL and without CCL -
i can listen volume level in volumeservice. service listen volume level in background. problem is: can't control volume in lock screen. tried 2 different ways volume control
first way ccl
application class:
public class castapplication extends application { private static string application_id; public static final double volume_increment = 0.03; @override public void oncreate() { super.oncreate(); application_id = getstring(r.string.app_id); // initialize videocastmanager videocastmanager.initialize(this, application_id, videocastcontrolleractivity.class, null). setvolumestep(volume_increment).enablefeatures(videocastmanager.feature_notification |videocastmanager.feature_lockscreen | videocastmanager.feature_wifi_reconnect | videocastmanager.feature_captions_preference |videocastmanager.feature_debugging); } }
mainactivity:
public class mainactivity extends fragmentactivity { private videocastmanager mcastmanager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); videocastmanager.checkgoogleplayservices(this); mcastmanager = videocastmanager.getinstance(); intent i1 = new intent(this,volumeservice.class); startservice(i1); } @override public boolean dispatchkeyevent(keyevent event) { if (mcastmanager.ondispatchvolumekeyevent(event, castapplication.volume_increment)) { return true; } return super.dispatchkeyevent(event); } }
second way without ccl (with rrc)
mainactivity:
public class mainactivity extends fragmentactivity { contentobserver msettingscontentobserver; remotecontrolclient remotecontrolclient; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intent intent = new intent(intent.action_media_button); audiomanager maudiomanager = (audiomanager) getsystemservice(context.audio_service); maudiomanager.requestaudiofocus(null, audiomanager.stream_ring,audiomanager.audiofocus_gain_transient_may_duck); remotecontrolclientcompat mremotecontrolclientcompat = new remotecontrolclientcompat(pendingintent.getbroadcast(this.getapplicationcontext(), 0,intent, 0)); remotecontrolhelper.registerremotecontrolclient(maudiomanager,mremotecontrolclientcompat); remotecontrolclient = new remotecontrolclient((pendingintent.getbroadcast(this.getapplicationcontext(), 0,intent, 0))); mediarouter.getinstance(this.getapplicationcontext()).addremotecontrolclient(remotecontrolclient); intent i1 = new intent(this,volumeservice.class); startservice(i1); } }
service , contentobserver same both way
volumeservice:
public class volumeservice extends service { contentobserver msettingscontentobserver; @override public void oncreate() { super.oncreate(); msettingscontentobserver = new settingscontentobserver(this, new handler()); getapplicationcontext().getcontentresolver().registercontentobserver( android.provider.settings.system.content_uri, true,msettingscontentobserver); } @override public ibinder onbind(intent intent) { return null; } @override public void ondestroy() { super.ondestroy(); getapplicationcontext().getcontentresolver().unregistercontentobserver(msettingscontentobserver); } }
contentobserver:
public class settingscontentobserver extends contentobserver { int previousvolume; context context; public settingscontentobserver(context c, handler handler) { super(handler); context = c; audiomanager maudiomanager = (audiomanager) context.getsystemservice(context.audio_service); previousvolume = maudiomanager.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) { log.i("myservice", "volume down"); previousvolume = currentvolume; } else if (delta < 0) { log.i("myservice", "volume up"); previousvolume = currentvolume; } } }
manifest:
<uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-sdk android:minsdkversion="14" android:targetsdkversion="18" /> <application android:name="com.yns.volumelock.castapplication" android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name="com.google.android.libraries.cast.companionlibrary.remotecontrol.videointentreceiver" > <intent-filter> <action android:name="android.media.audio_becoming_noisy" /> <action android:name="android.intent.action.media_button" /> <action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" /> <action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" /> </intent-filter> </receiver> <service android:name="com.google.android.libraries.cast.companionlibrary.notification.videocastnotificationservice" android:exported="false" > <intent-filter> <action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" /> <action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" /> <action android:name="com.google.android.libraries.cast.companionlibrary.action.notificationvisibility" /> </intent-filter> </service> <service android:name="com.google.android.libraries.cast.companionlibrary.cast.reconnection.reconnectionservice" /> <activity android:name="com.google.android.libraries.cast.companionlibrary.cast.tracks.captionspreferenceactivity" android:label="@string/action_settings" > <intent-filter> <action android:name="android.intent.action.main" /> </intent-filter> </activity> <service android:name="com.yns.volumelock.volumeservice" android:enabled="true" > </service> </application>
Comments
Post a Comment