Android - 自定义新来电屏幕

时间:2012-10-13 02:37:28

标签: android

我正在尝试在Android中创建一个新的来电屏幕,

当我收到来电时,我的应用程序启动 - 但会立即崩溃,并且默认的来电屏幕即将出现。

我做错了什么?

我的代码:

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);
    }

}

日志是:

 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

3 个答案:

答案 0 :(得分:3)

您收到ClassCastException,因为您的清单将MyPhoneBroadcastReceiver定义为接收者,而不是活动。您不需要活动来创建意图,因为它需要Context,其中一个提供onReceive()。让它扩展BroadcastReceiver并稍微改变意图:

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);
    }
}

答案 1 :(得分:3)

好的,为了让我的新页面在前面我需要让它睡一会儿...... 所以新代码将是:

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();
    }
}

- 原始来电程序仍在后台运行......

有没有办法替换它而不是在它上面打开新的应用程序?

谢谢!

答案 2 :(得分:0)

将此添加到您的清单:

<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>