我已經非常新進入Android
開發,並決定我在這個新領域的第一次征服將是掌握電話如何響應來電。Android,TelephonyManager,PhoneStateListener和來電號碼的樂趣
稍後使用Google進行搜索導致我登錄到http://www.compiletimeerror.com/2013/08/android-call-state-listener-example.html#.Vi3Ren4vfwM(所以我的代碼與他/她有着驚人的相似之處)。
我的主要(唯一的)活動是這樣的:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(new TeleListener(),
PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class TeleListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// CALL_STATE_IDLE;
Log.d("MyLittleDebugger", "I'm in " + state + " and the number is " + incomingNumber);
Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK;
Log.d("MyLittleDebugger", "I'm in " + state + " and the number is " + incomingNumber);
Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
// CALL_STATE_RINGING
Log.d("MyLittleDebugger", "I'm in " + state + " and the number is " + incomingNumber);
Toast.makeText(getApplicationContext(), incomingNumber,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
}
現在,這裏的樂趣停在那裏。我的應用程序在仿真器上運行,並使用DDMS
欺騙幾個電話到我的仿真設備,以查看碎片落在哪裏。
當然,足夠的敬酒彈出和MyLittleDebugger
爆發了國家掉期。聽衆正在工作,但是我的日誌或烤麪包上沒有顯示任何號碼。
這只是空白的數字應該是!不爲空或任何東西,不,但是空白!
經過多一點Google搜索,我意識到我的AndroidManifest.xml
可能是問題所在。這是因爲如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.x.xy" >
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
現在,這裏的問題:我缺少什麼?
顯然,某個東西的一小部分在某處出錯了,因爲我可以讓我的TelephonyMgr
對象.listen()
調用狀態,但我無法獲取要顯示的數字。
新的信息:
我也試着這樣我的手機上,而不會仿效完全相同的結果。
我跑你的代碼,它爲我工作。 #出現使用ddm僞造的電話。 – JJF
另外我的應用程序甚至不會運行,如果我沒有給它READ_PHONE_STATE權限。這聽起來不同於你所看到的。你嘗試過哪些版本的Android? – JJF
我有完全相同的問題,但廣播接收器不適合我。 – Mainak