7

I'm trying to make an new incoming call screen in android,

when i get an incoming call my app starts - but crashes immediately, and the default incoming call screen is coming up.

what am i doing wrong?

my code:

AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.myfirstapp"
      android:versionCode="7"
      android:versionName="7">
    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="10"></uses-sdk>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <receiver android:name=".MyPhoneBroadcastReceiver" android:enabled="true">
            <intent-filter android:priority="99999">
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
        <activity
            android:name=".Call" >
        </activity>
    </application>
</manifest>

MyPhoneBroadcastReceiver.java:

package com.example.myfirstapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class MyPhoneBroadcastReceiver extends Activity{

    public void onReceive(final Context context, Intent intent) {
            Intent main_intent = new Intent(this, Call.class);
            context.startActivity(main_intent);
    }


}

Call.java:

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;

public class Call extends Activity{

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }

}

the log is:

 10-14 20:59:51.056: E/AndroidRuntime(1826): FATAL EXCEPTION: main
 10-14 20:59:51.056: E/AndroidRuntime(1826):
 java.lang.RuntimeException: Unable to instantiate receiver
 com.example.myfirstapp.MyPhoneBroadcastReceiver:
 java.lang.ClassCastException:
 com.example.myfirstapp.MyPhoneBroadcastReceiver cannot be cast to
 android.content.BroadcastReceiver 10-14 20:59:51.056:
 E/AndroidRuntime(1826): at
 android.app.ActivityThread.handleReceiver(ActivityThread.java:2210)
 10-14 20:59:51.056: E/AndroidRuntime(1826): at
 android.app.ActivityThread.access$1500(ActivityThread.java:130) 10-14
 20:59:51.056: E/AndroidRuntime(1826): at
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
 10-14 20:59:51.056: E/AndroidRuntime(1826): at
 android.os.Handler.dispatchMessage(Handler.java:99) 10-14
 20:59:51.056: E/AndroidRuntime(1826): at
 android.os.Looper.loop(Looper.java:137) 10-14 20:59:51.056:
 E/AndroidRuntime(1826): at
 android.app.ActivityThread.main(ActivityThread.java:4745) 10-14
 20:59:51.056: E/AndroidRuntime(1826): at
 java.lang.reflect.Method.invokeNative(Native Method) 10-14
 20:59:51.056: E/AndroidRuntime(1826): at
 java.lang.reflect.Method.invoke(Method.java:511) 10-14 20:59:51.056:
 E/AndroidRuntime(1826): at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
 10-14 20:59:51.056: E/AndroidRuntime(1826): at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-14
 20:59:51.056: E/AndroidRuntime(1826): at
 dalvik.system.NativeStart.main(Native Method) 10-14 20:59:51.056:
 E/AndroidRuntime(1826): Caused by: java.lang.ClassCastException:
 com.example.myfirstapp.MyPhoneBroadcastReceiver cannot be cast to
 android.content.BroadcastReceiver 10-14 20:59:51.056:
 E/AndroidRuntime(1826): at
 android.app.ActivityThread.handleReceiver(ActivityThread.java:2205)
 10-14 20:59:51.056: E/AndroidRuntime(1826): ... 10 more
Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
Dan Methad
  • 217
  • 2
  • 6
  • 17

3 Answers3

3

You are getting a ClassCastException because your manifest defines MyPhoneBroadcastReceiver as a receiver, not an activity. You don't need an activity to create the intent, since it takes a Context, and one is provided with onReceive(). Have it extend BroadcastReceiver and alter the intent slightly like this:

public class MyPhoneBroadcastReceiver extends BroadcastReceiver{

    public void onReceive(final Context context, Intent intent) {
            Intent main_intent = new Intent(context, CallActivity.class);
            context.startActivity(main_intent);
    }
}
Geobits
  • 22,218
  • 6
  • 59
  • 103
  • Thanks for that, but i got another error that i need to set "FLAG_ACTIVITY_NEW_TASK". so i changed the code to: Intent i = new Intent(); i.setClass(context, Call.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); Now it works, but shows up under the original incoming call screen (so i see it only after i end the call)... – Dan Methad Oct 15 '12 at 01:17
  • I don't believe you can completely override the built-in call screen, at least up to Gingerbread. Whether it works on newer APIs I haven't been able to figure out. Just googling a bit seems to indicate it's done for security reasons. I see a lot of attempts, but most just dead end with "Oh, you can't do that". Good luck on your search, but at least you got your crash problems cleared up. – Geobits Oct 15 '12 at 01:23
  • 1
    Thanks for the help! i see some apps like: https://play.google.com/store/apps/details?id=com.perracolabs.tccp and: http://code.google.com/p/incomingcallplus/ , that change this screen so it must be possible. – Dan Methad Oct 15 '12 at 01:32
3

Ok, in order to make my new page in the front i need to make it sleep for a while... so the new code will be:

public class MyPhoneBroadcastReceiver extends BroadcastReceiver{

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


        Thread pageTimer = new Thread(){
            public void run(){
                try{
                    sleep(1000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                } finally {
                    Intent i = new Intent();
                    i.setClass(context, Call.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
                }
            }
        };
        pageTimer.start();
    }
}

But - the original incoming call program is still running in the background...

Is there any way to replace it instead opening new app ontop of it?

Thanks!

Dan Methad
  • 217
  • 2
  • 6
  • 17
  • 1
    I've done a bit of research on this and have found a lot of posts saying this can't be done - though this app claims to do what you want (https://play.google.com/store/apps/details?id=com.perracolabs.tcc). However, the app doesn't work on my Galaxy Nexus, so it may not be something that will work on all phones. If you found a solution to this problem, please comment back! – Eric Brynsvold Oct 25 '12 at 13:23
  • The above app does not replace the incoming call screen - the screenshots are for an outgoing call confirm - so I don't believe that it is possible to replace the default app, only to overlay something else on top of it. – Eric Brynsvold Nov 01 '12 at 14:04
0

Add this to your manifest:

<activity> 
    <intent-filter>
        <action android:name="android.intent.action.ANSWER" />
        <action android:name="android.intent.action.PHONE_STATE" />
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>  
</activity>
arghtype
  • 4,376
  • 11
  • 45
  • 60
WhiteWolfza
  • 225
  • 3
  • 7