2016-08-10 2 views
0

받은 수신 전화에 토스트를 표시하려고합니다. 그러나 나는 어떤 전시도 얻지 않고있다. 매니페스트 파일 내부에 수신자가 언급되어 있는데, 여기에는 전화 통화에 필요한 권한이 포함되어 있습니다. 다음은 내가 사용한 코드입니다.수신 통화를위한 브로드 캐스트 수신기가 작동하지 않습니까?

// inside IncomingCall broadcastreceiver 
package com.example.shailesh.callbroadcastreceiver; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.Toast; 

public class IncomingCall extends BroadcastReceiver { 

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

    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
     // This code will execute when the phone has an incoming call 

     // get the phone number 
     String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 
     Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show(); 

    } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
      TelephonyManager.EXTRA_STATE_IDLE) 
      || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
      TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
     // This code will execute when the call is disconnected 
     Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show(); 

    } 

    } 
} 

다음과 같이 내가 menifest 파일에 지정했다 :

<?xml version="1.0" encoding="utf-8"?> 

<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: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> 

    <receiver android:name=".IncomingCall" android:enabled="true"> 
     <intent-filter> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
     </intent-filter> 
    </receiver> 
</application> 

내가 걸려 오는 전화에 토스트 표시를 얻기 위해 포함해야 다른 어떤 받았습니다. 클래스는 IncomingCall 서비스 나는 또한 내 MainActivity

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Intent i = new Intent(getApplicationContext(),IncomingCall.class); 
    startService(i); 
} 
} 
+0

possibel http://stackoverflow.com/a/27107056/5515371 –

+0

수신자에게 패키지 이름을 추가하십시오.

+0

아직 작동하지 않음 –

답변

2

에서 다음 코드를 포함 적이있다? 그렇지 않다면 왜 당신이 서비스를 시작하려는 의도를 발사하고 있는지.

+0

심지어 내가 전화를받을 때 그 라인을 제거하고 프로젝트를 다시 실행해도 출력이 나오지 않습니다. –