Eileen
Eileen

Reputation: 19

use Broadcast Receiver to catch incoming call, onReceive not triggered? Log cannot be printed out

In broadcastReceiver:

public void onReceive(final Context context, Intent intent) {

  Log.e("Current:","entered111!");

  String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
  String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
  if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){



      Intent intent1=new Intent(context,myService.class);
      intent1.putExtra("incomingNumber",incomingNumber);
      context.startService(intent1);
    }
}

This is my Manifest file

<activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 </activity>
  <service android:name=".myService" />
  <receiver android:name=".ServiceReceiver"
        android:enabled="true">
  <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
  </intent-filter>
  </receiver>

Upvotes: 0

Views: 127

Answers (1)

Samantha
Samantha

Reputation: 1001

You have to add below permission in your manifest file;

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

Then enable 'Phone' in 'Permissions' of your application in 'Settings' -> 'Apps' -> Application -> 'Permissions'.

Thank you.

Upvotes: 0

Related Questions