AudioManager audio = (AudioManager) context.getSystemService(Context.AUdio_SERVICE); audio.setStreamVolume(AudioManager.STREAM_RING,volume,AudioManager.FLAG_ALLOW_RINGER_MODES | AudioManager.FLAG_PLAY_SOUND);解决方法 经过一些研究和测试后,我发现只有在接入呼叫检测的早期阶段才能实现音量变化.在onReceive()方法中做很多 *** 作至关重要.
public class callreceiver extends broadcastReceiver {private boolean isRinging = false;private boolean isInProgress = false;private Listener mListener = null;// interface declaration public interface Listener { public voID onPhonestateChange(boolean state); }// registration of Listener public voID registerListener (Listener Listener) { mListener = Listener; }@OverrIDepublic voID onReceive(Context context,Intent intent) { String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); if(TelephonyManager.EXTRA_STATE_RINGING.equals(state)) { // do something while ringing BUT NOT TOO MUCH! isRinging = true; if (context instanceof StateChecker) ((StateChecker) context).serviceDestroy(); Log.v("phone","ringing"); } else if(TelephonyManager.EXTRA_STATE_IDLE.equals(state)) { //do something when the call ends if (context instanceof StateChecker) ((StateChecker) context).serviceStart(); Log.v("phone","IDle"); isRinging = false; } // sending notification to Listener if(mListener != null) { mListener.onPhonestateChange(isRinging); Log.v("Listener","notifIEd"); }}}
以及来自服务的一些代码:
// Listeners callbackspublic voID onPhonestateChange(boolean state) { if (state) { if (!callinProgress) { // Do the main task here callinProgress = true; serviceDestroy(); } } else { callinProgress = false; serviceStart(); }}
ServiceStart:
// starting servicepublic voID serviceStart() { preferencesRead(); //The intent to launch when the user clicks the expanded notification Intent intent = new Intent(this,MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_top | Intent.FLAG_ACTIVITY_SINGLE_top); PendingIntent pendIntent = PendingIntent.getActivity(this,intent,0); if(settings.getBoolean("hIDe_noti_icon",false)) { Notification noti = new NotificationCompat.Builder(this) .setContentTitle("Intelligent Ringer") .setContentText(getString(R.string.is_running)) .setSmallicon(R.drawable.invisible) .setContentIntent(pendIntent) .setPriority(Notification.PRIORITY_MIN) .build(); startForeground(12345,noti); } else { Notification noti = new NotificationCompat.Builder(this) .setContentTitle("Intelligent Ringer") .setContentText(getString(R.string.is_running)) .setSmallicon(R.drawable.ic_launcher) .setContentIntent(pendIntent) .setPriority(Notification.PRIORITY_MIN) .build(); startForeground(12345,noti); } Log.v("service","started"); // registering Listener for callreceiver if(isCRRegistered) { unregisterReceiver(mcallreceiver); isCRRegistered = false; } if(!isCRRegistered) { IntentFilter filter2 = new IntentFilter("androID.intent.action.PHONE_STATE"); filter2.addAction("androID.intent.action.PHONE_STATE"); callreceiver mcallreceiver = new callreceiver(); this.mcallreceiver = mcallreceiver; registerReceiver(mcallreceiver,filter2); mcallreceiver.registerListener(this); isCRRegistered = true; } if(isScreenRegistered) { unregisterReceiver(mReceiver); isScreenRegistered = false; } if(!ispositionRegistered && proximityOn) { position mposition = new position(this.getApplicationContext()); mposition.registerListener(this); mposition.isInPocket(); this.mposition = mposition; mposition.register(); ispositionRegistered = true;} if(!isScreenRegistered) { IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); ScreenReceiver mReceiver = new ScreenReceiver(); registerReceiver(mReceiver,filter); this.mReceiver = mReceiver; mReceiver.registerListener(this); isScreenRegistered = true; }}
Servicedestroy()
// stopPing servicepublic voID serviceDestroy() { if (this.mposition != null) { this.mposition.unregister(); } Log.v("service","Destroyed"); if(isScreenRegistered) { unregisterReceiver(mReceiver); isScreenRegistered = false; }}
的OnDestroy()
public voID onDestroy () { serviceDestroy(); if(isCRRegistered) { unregisterReceiver(mcallreceiver); isCRRegistered = false; }}总结
以上是内存溢出为你收集整理的android – 在响铃期间改变铃声音量全部内容,希望文章能够帮你解决android – 在响铃期间改变铃声音量所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)