1

I wrote applications for android 4.1(16 API) that should respond to the incoming call, but it don't happen. I see many tutorials and it too simple to make any mistake but... I use Android studio 1.4. Telephone with apk is Nexus 6 with android 6.0.1. Any idea what I do wrong?

This is a BroadcastReceiver class:

public class MyReceiver extends BroadcastReceiver {

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

}
}

And this is my manifest:

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

<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="MyReceiver"
        android:enabled="true"
        android:exported="false"
        >
        <intent-filter>
            <action
                android:name="android.intent.action.PHONE_STATE" >
            </action>
        </intent-filter>
    </receiver>
</application>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And mainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
ByteWelder
  • 5,464
  • 1
  • 38
  • 45
Mateusz
  • 83
  • 9

2 Answers2

1

You need to change the android:exported="false" to true so your receiver will be exported to other apps besides yours.
If you get a SecurityException in Marshmallow look a this guide.

Nir Duan
  • 6,164
  • 4
  • 24
  • 38
1

In MarshMallow Dangerous Permissions must give at run time for detail understanding visit the link https://developer.android.com/training/permissions/requesting.html