Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java监听电话代码是否未执行?

我的应用程序播放音频,因此我想在用户接到电话时将应用程序的音频静音。这样,音乐就不会在用户通话中播放。我使用了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完全没有做任何事情。我真的很感谢你帮我修好^{

谢谢, 富有的


共 (2) 个答案

  1. # 1 楼答案

    你忘了引用电话经理的话了吗

    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);
        }
    }
    
  2. # 2 楼答案

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

    <?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 PhoneStateListenerlisten_uu标志的按位或组合

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

    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;
     }
     }