正在侦听未执行的电话代码



我的应用程序播放音频,因此我想在用户接到电话时将应用程序的音频静音。这样,音乐就不会在用户的通话中播放。我使用过phoneStateListener,但由于某种原因,其中的方法没有执行——我知道是因为日志没有显示:

PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                //Incoming call: Pause music
                  TwentySeconds.stopTimer(); //Stop service which mutes app
           Intent i = new Intent(BaseActivity.this, Main_Menu.class);
            startActivity(i);
                Toast.makeText(BaseActivity.this, "INCOMING CALL", Toast.LENGTH_SHORT).show();
                Log.v(TAG, "INCOMING CALL");
            } else if (state == TelephonyManager.CALL_STATE_IDLE) {
                //Not in call: Play music
                Log.v(TAG, "NOT IN A CALL");
            } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                //A call is dialing, active or on hold
                   TwentySeconds.stopTimer(); //Stop service which mutes app
           Intent i = new Intent(BaseActivity.this, Main_Menu.class);
            startActivity(i);
                Log.v(TAG, "CALL IS ACTIVE!");
                Toast.makeText(BaseActivity.this, "DIALING", Toast.LENGTH_SHORT).show();
            }
        }
    };

我在网上到处找了找如何做到这一点——我找到的一种方法是使用意图,但这需要扩展BroadcastReceiver,我不能扩展两件事。因此,就目前而言,phoneStateListener完全没有做任何事情。我真的很感谢你帮我修PhoneStateListener

谢谢,富

你忘了引用TelephonyManager吗?

TelephonyManager manager = this.getSystemService(TELEPHONY_SERVICE);

试试这个:

public class PhoneCallStateActivity extends Activity {
   TelephonyManager manager;
   @override
   public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main); 
      manager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
      manager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
   }
    class MyPhoneStateListener extends PhoneStateListener{
       @override
       public void onCallStateChanged(int state, String incomingNumber) {
          switch(state) {
          case TelephonyManager.CALL_STATE_IDLE:
          //TODO:
          break;
          case TelephonyManager.CALL_STATE_RINGING:
          //TODO:
          break;
          case TelephonyManager.CALL_STATE_OFFHOOK:
          //TODO:
          break;
          default:
          break;
         }
       }
       super.onCallStateChanged(state, incomingNumber);
    }
}

我看过你的代码,它很好。但我只是在想你的清单文件,你是否有足够的权限读取手机状态。看看我的清单文件(它正在工作):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.truiton.phonestatelistener"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".PhoneStateListenerActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

最好使用TelephonyManager访问/使用这个"自定义电话状态侦听器"类,如下所示:

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class PhoneStateListenerActivity extends ActionBarActivity {
	TelephonyManager tManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		tManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		tManager.listen(new CustomPhoneStateListener(this),
				PhoneStateListener.LISTEN_CALL_STATE
						| PhoneStateListener.LISTEN_CELL_INFO // Requires API 17
						| PhoneStateListener.LISTEN_CELL_LOCATION
						| PhoneStateListener.LISTEN_DATA_ACTIVITY
						| PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
						| PhoneStateListener.LISTEN_SERVICE_STATE
						| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS
						| PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR
						| PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR);
	}
}

在这里,我们将tManager.listen方法设置为CustomPhoneStateListener的一个新对象,并在events参数中传递Android PhoneState监听器listen_标志的按位OR组合。

您可以设置自己的自定义电话状态侦听器(我只是在这里添加我的):

public class CustomPhoneStateListener extends PhoneStateListener {
  
  public CustomPhoneStateListener(Context context) {
     mContext = context;
  }
  
  @Override
 public void onCallStateChanged(int state, String incomingNumber) {
 super.onCallStateChanged(state, incomingNumber);
 switch (state) {
 case TelephonyManager.CALL_STATE_IDLE:
 Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_IDLE");
 break;
 case TelephonyManager.CALL_STATE_RINGING:
 Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_RINGING");
 break;
 case TelephonyManager.CALL_STATE_OFFHOOK:
 Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_OFFHOOK");
 break;
 default:
 Log.i(LOG_TAG, "UNKNOWN_STATE: " + state);
 break;
 }
 }

最新更新