我想在android中獲取被叫號碼,但是當我開始撥出電話時,它失敗我正在使用廣播接收器並將其註冊到服務中以保持偵聽,如果活動不在焦點中是我的代碼。在廣播接收器中接收呼叫意圖
Menifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vampirepc.androidservice" >
<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" />
<action android:name="android.intent.action.BOOT_COMPLETED">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</action>
</intent-filter>
</activity>
<service android:name=".MyService" />
<receiver
android:name="com.example.vampirepc.androidservice.OutgoingReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
</manifest>
廣播接收器類
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(ctx,
"Inside broadcast",
Toast.LENGTH_LONG).show();
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show();
}
服務
@Override
public void onCreate() {
Toast.makeText(getApplicationContext(),"Service created",Toast.LENGTH_SHORT).show();
try {
IntentFilter filter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL");
OutgoingReceiver myReceiver = new OutgoingReceiver();
registerReceiver(myReceiver, filter);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"Exception is "+String.valueOf(e),Toast.LENGTH_SHORT).show();
}
}
在你的情況下,沒有必要在運行時註冊接收器,只需在清單中定義它。 –
當我撥號時仍然不工作應用程序停止工作 – DumpsterDiver
請發佈堆棧跟蹤。 –