Android:Broadcastreceiver抛出java.lang.RuntimeException

时间:2017-06-13 11:33:03

标签: android android-broadcastreceiver

我正在尝试实现广播接收器,以便在我卸载,安装和拨打电话(传入和传出)时获取事件。我在AndroidManifest.xml中注册了intent-filters,但是当我在手机上运行我的应用程序时,它会抛出运行时异常。

我的代码:

package com.local.broadcast_receiver;

import android.content.BroadcastReceiver;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MainActivity extends android.content.BroadcastReceiver {

static boolean flag = false;
static long start_time_incoming = 0;
static long start_time_outgoing = 0;
static long end_time;

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

    String action = intent.getAction();

    if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) {

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_INCOMING_NUMBER)) {
            start_time_incoming = System.currentTimeMillis();

        }else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_RINGING)) {
            start_time_outgoing = System.currentTimeMillis();
        }

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_IDLE)) {
            end_time = System.currentTimeMillis();
        }

        if (start_time_incoming != 0) {
            //Total time talked =
            long total_time = end_time - start_time_incoming;
            //Store total_time somewhere or pass it to an Activity using intent
            Log.i("Broadcast", "Last incoming call duration:" + total_time);

        }else if (start_time_outgoing != 0) {
            //Total time talked =
            long total_time = end_time - start_time_outgoing;
            //Store total_time somewhere or pass it to an Activity using intent
            Log.i("Broadcast", "Last outgoing call duration:" + total_time);
        }

    }else if(intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {

        Uri uri = intent.getData();
        String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
        Log.i("Broadcast", "name of package removed is:" + pkg);
    }else if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {

        Uri uri = intent.getData();
        String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
        Log.i("Broadcast","name of package Added is:" + pkg);
    }
}
}

我的AndroidManifest.xml如下所示: -

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

<uses-permission
    android:name="android.permission.READ_CALL_LOG"
    android:maxSdkVersion="25" />
<uses-permission
    android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="25" />

<receiver android:name="MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
        <action android:name="android.intent.action.PACKAGE_ADDED" />

        <data android:scheme="package" />
    </intent-filter>
</receiver>

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

我得到了runtimeexception: -

                                                                               java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.local.broadcast_receiver/com.local.broadcast_receiver.MainActivity}: java.lang.ClassCastException: com.local.broadcast_receiver.MainActivity cannot be cast to android.app.Activity
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2819)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
                                                                            at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6682)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                                                                         Caused by: java.lang.ClassCastException: com.local.broadcast_receiver.MainActivity cannot be cast to android.app.Activity
                                                                            at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) 
                                                                            at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:154) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6682) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

尝试搜索主题&#34中可用的不同帖子;无法转换为android.app.Activity&#34;但无法得到任何暗示。有人可以指出什么是错的,我应该怎么做才能在我的手机上运行这个代码?

由于

1 个答案:

答案 0 :(得分:1)

请使用您的代码/实现分析错误日志。

  

引起:java.lang.ClassCastException:   com.local.broadcast_receiver.MainActivity无法强制转换为   android.app.Activity

您已宣布<activity android:name=".MainActivity">,实际上是BroadcastReceiver。因此将其声明为<receiver />

正确更新您的清单,请参阅Android Manifest Documentation