0

I searched for many resources, but nothing helped me.. i also tried this but it didn't worked...

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chotu.mybot">

<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"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".IncomingCall">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

IncomingCall.java:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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)) {
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Toast.makeText(context," its name :"+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)) {
        Toast.makeText(context," something", Toast.LENGTH_LONG).show();
    }
}
}
Community
  • 1
  • 1

1 Answers1

0

You need to add the following permission to make this code work:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
activesince93
  • 1,716
  • 17
  • 37
  • Then you should review Runtime Permissions Check introduced in Android 6.0. Google official documentation: [Runtime Permissions Check:] (https://developer.android.com/training/permissions/requesting.html) – activesince93 May 17 '16 at 08:48