2013-02-28 60 views
0

我正在創建應用程序和我的代碼的某個部分,以便在接到來電時創建通知。問題是我無法執行通知,因爲我必須傳遞給我打電話的人數。以下幾段我的代碼。在接到來電時創建通知

我的收款人:

@Override 
public void onReceive(Context context, Intent intent) { 
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
    CallIntercepterListener callIntercepterListener = new CallIntercepterListener(); 

    telephonyManager.listen(callIntercepterListener, PhoneStateListener.LISTEN_CALL_STATE); 

    Bundle bundle = intent.getExtras(); 
    String phoneNumber = bundle.getString("incoming_number"); 
    Log.d(TAG, "phoneNumber: " + phoneNumber); 
} 

和我的聽衆:

@Override 
public void onCallStateChanged(int state, String incomingNumber) { 
    Log.v(TAG, "Event Call: " + incomingNumber); 

    switch (state) { 
     case TelephonyManager.CALL_STATE_RINGING: 
      // Create a notification with de incomingNumber 
      break; 
    } 
} 

回答

1

什麼喲需要監聽?只要手機狀態發生變化,您的接收器就會被調用。

here is a nice article about how to use the BroadcastReceiver.

從這篇文章:

package de.vogella.android.receiver.phone; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.TelephonyManager; 
import android.util.Log; 

public class MyPhoneReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     String state = extras.getString(TelephonyManager.EXTRA_STATE); 
     Log.w("DEBUG", state); 
     if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
     String phoneNumber = extras 
      .getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
     Log.w("DEBUG", phoneNumber); 
     } 
    } 
    } 
} 
+0

謝謝!這對我幫助很大!! – rmenezes 2013-03-01 00:14:37