onCallStateChanged - 在模拟时自动运行和字符串incomingNumber为空

时间:2017-12-03 14:07:48

标签: java android

首先我的代码
MainActivity.java

package com.example.crazy.anrufstatustracker2;

import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

//Dec. and init
private EditText txtOutput = null;

//Telefonobjekte
private TelephonyManager telephonyManager = null;
private PhoneStateListener phoneStateListener = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Setzen des Layouts der MainActivity
    setContentView(R.layout.activity_main_layout);

    //Generierung Widgets
    this.txtOutput = (EditText) findViewById(R.id.txtOutput);

    //TelefonObjekte
    this.telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

    //Generierung des PhoneStateListeners
    this.phoneStateListener = new PhoneStateListener()
    {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            String strCallStateAndNr = "\nNummer:\n" + incomingNumber + "\nStatus:\t";

            switch(state){
                case TelephonyManager.CALL_STATE_RINGING:
                    strCallStateAndNr += "Einkommender Anruf";
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    strCallStateAndNr += "Anruf angenommen";
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    strCallStateAndNr += "Aufgelegt / Standardzustand";
                    break;
            }
            //Ausgabe
            txtOutput.setText(txtOutput.getText().toString() +" "+ strCallStateAndNr);

        }
    };
    //Registrieren PhoneStateListener bei dem TelephonyManager
    telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);

}

@Override
protected void onDestroy() {
    super.onDestroy();
    telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_NONE);
}
}

的AndroidManifest.xml

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.crazy.anrufstatustracker2">


    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.crazy.anrufstatustracker2.MainActivity">


    <EditText
        android:id="@+id/txtOutput"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:ems="10"
        android:hint="@string/strCallState"
        android:inputType="textMultiLine"
        android:scrollbars="horizontal|vertical" />
</LinearLayout>

我会很快 我的问题是&#34; onCallStateChanged&#34;方法在模拟应用程序时运行,因此incomingNumber字符串中没有数字。 但我可以忍受这一点。 主要问题是,即使我执行一个调用,字符串incomingNumber仍然是空的。

第一部分是当我运行应用程序时运行&#34; onCallStateChange方法,但启动应用程序时没有chaning状态,如果是,请解释一下。第二部分显示来电,应用程序重新通知呼叫并显示状态,但是你可以看到空的incomingNunber字符串。第三部分是结束通话,仍然可以识别,但仍然没有号码:

enter image description here

0 个答案:

没有答案