1

I'm new to Android Development, I'm building an Incoming Call Screen Which will come up with "Answer" and "Decline" Button and a Blue color Background whenever My Phone would go in Ringing State.

What I've Achieved so far is I'm Getting My Buttons with my Background whenever the Call Rings! Recently I Added an Answer Button but It's not seems to be Working. Whenever I touch on Button the Back Screen got Touched! Please help me with Creating a touchable Overlay. And Also the Answering Call code that i used is not working.I tried in a Different Way.So Please guide me with Answering and declining Incoming Call on Button Click, I'll be Thankful to You!

This is My Broadcast Receiver file!

package com.utaiba.appguruz2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class PhoneStateReceiver extends BroadcastReceiver {

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

    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
    Intent i;

    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

        i = new Intent(context, MyService.class);
        i.putExtra("incomingNumber", incomingNumber);
        context.startService(i);
    }
 }
}

This is My Service Class Which Appeared on CALL_RINGING_STATE!

package com.utaiba.appguruz2;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class MyService extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

private View view;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("inez","inez onCreate");
    super.onCreate();

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            0,
            0,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            0,
            // WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
            PixelFormat.TRANSPARENT);

    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

    LayoutInflater inflater = (LayoutInflater) 
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.activity_main,null);
    TextView textview = view.findViewById(R.id.text1);
    textview.setText("Someone is calling: " + 
 intent.getStringExtra("incomingNumber"));
    textview.setBackgroundColor(Color.YELLOW);
    Button answerButton = view.findViewById(R.id.pickup);
    answerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent2 = new Intent("android.intent.action.ANSWER");
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent2);
        }
    });
            wm.addView(view, params);

    return START_STICKY;
  }

  public void onDestroy() {
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    if (wm != null) {
        wm.removeView(view);

    }
 }

}

Also, When the Call disconnects, Overlay doesn't Disappears. Which Means onDestroy() Method isn't doing it's Work! So How can i make Overlay disappears when call disconnects?

Here's my AndroidManifest.XML file

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

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">
    <service
        android:name=".MyService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.utaiba.appguruz2.MyService" />
        </intent-filter>
     </service>

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

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

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

</manifest>

Here's the activity_main.XML file!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:windowBackground="@android:color/transparent"
android:windowIsTranslucent="true"
android:windowAnimationStyle="@android:style/Animation.Translucent"
android:background="#00AFF0"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:layout_gravity="center"
    android:layout_marginTop="200dp"
    android:text="Skype Incoming Call Screen"
    android:textSize="20sp"
    android:id="@+id/text1"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/pickup"
    android:layout_marginTop="20dp"
    android:text="Answer Your Call"
    android:background="#3CD811"/>

</LinearLayout>

0 Answers0