2012-11-28 59 views
5

我試圖捕獲一個包含呼叫廣播但它不工作。傳入呼叫廣播接收器不工作(Android 4.1)

這是我的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.test.bgPicture" 
    android:versionCode="1" 
    android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="9" /> 

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

<uses-feature 
    android:name="android.hardware.camera" 
    android:required="false" /> 
<uses-feature 
    android:name="android.hardware.camera.front" 
    android:required="false" /> 

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" > 
    <receiver android:name=".PhoneStateBroadcastReceiver" > 
     <intent-filter> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
     </intent-filter> 
    </receiver> 
</application> 

這是我的廣播接收器

package com.test.bgPicture; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 

public class PhoneStateBroadcastReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
     telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE); 
    } 
} 

與聽者..

package com.test.bgPicture; 

import android.content.Context; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.widget.Toast; 

public class CustomPhoneStateListener extends PhoneStateListener { 

//private static final String TAG = "PhoneStateChanged"; 
Context context; //Context to make Toast if required 
public CustomPhoneStateListener(Context context) { 
    super(); 
    this.context = context; 
} 

@Override 
public void onCallStateChanged(int state, String incomingNumber) { 
    super.onCallStateChanged(state, incomingNumber); 

    switch (state) { 
    case TelephonyManager.CALL_STATE_IDLE: 
     //when Idle i.e no call 
     Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show(); 
     break; 
    case TelephonyManager.CALL_STATE_OFFHOOK: 
     //when Off hook i.e in call 
     //Make intent and start your service here 
     Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show(); 
     break; 
    case TelephonyManager.CALL_STATE_RINGING: 
     //when Ringing 
     Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show(); 
     break; 
    default: 
     break; 
    } 
} 
} 

但祝酒詞不顯示,a你有想法嗎?

編輯:我只是用另一部手機與android 2.3.6試了一下,它在那裏工作,但在第一個設備(android 4.1)它沒有工作。爲什麼會這樣呢?

+0

嘗試'Log.v(「onCallStateChanged」,「手機狀態空閒」);',檢查logcat的,因爲你正在使用的顯示吐司也許可能情況下不可用 –

+0

這並沒有幫助..我做注意它可以在android 2.3.6上運行,但在android 4.1上不行,任何想法? –

回答

10

我發現這個問題,在廣播接收機開始工作之前,您必須從應用程序手動啓動一個活動,從android 3.0開始工作。

+0

接受你的回答... !!! –

+0

我必須等到明天才能接受它.. –

+0

我在博客上讀到這是一些安全功能,因此用戶從未通過啓動活動而未激活的應用程序不會獲得廣播。 –

-1

您是否嘗試在服務中添加廣播接收器?即使應用程序已關閉/最小化,服務仍然保持活動狀態。 Check this link