听电话代码没有执行?

时间:2015-11-11 01:53:06

标签: java android multithreading service listener

我的应用播放音频,因此我想在用户接听电话时将我应用的音频静音。这样,音乐就不会在用户通话中播放。我使用了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();
            }
        }

    };

我已经浏览了整个网络以了解如何执行此操作 - 我找到的一种方法是使用intent,但这需要扩展BroadcastReceiver,而我无法扩展两件事。所以,就目前而言,phoneStateListener绝对没有做任何事情。我非常感谢你帮助我修复PhoneStateListener

谢谢, 富

2 个答案:

答案 0 :(得分:0)

您是否忘记引用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);
    }
}

答案 1 :(得分:0)

我看了你的代码,这很好。但我只是考虑你的清单文件,你是否有足够的权限来阅读手机状态。看看我的清单文件(工作正常):



<?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>
&#13;
&#13;
&#13;

最好是访问/使用这个&#34;自定义电话状态监听器&#34;使用TelephonyManager的类如下:

&#13;
&#13;
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);
	}

}
&#13;
&#13;
&#13;

这里我们将tManager.listen方法设置为CustomPhoneStateListener的新对象,并在events参数中传递Android PhoneStateListener LISTEN_标志的按位或组合。

你可以设置自己的自定义手机状态监听器,(我只是在这里添加我的):

&#13;
&#13;
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;
 }
 }
&#13;
&#13;
&#13;