-2

I am trying to implement a Broadcast receiver to get the events when I uninstall, install and make a phone call (incoming and outgoing). I have registered the intent-filters in the AndroidManifest.xml but when I run my app on my phone then it throws runtime exception.

My code:

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

and my AndroidManifest.xml looks like below:-

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

and I get 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) 

Tried searching through different posts available here on topic "cannot be cast to android.app.Activity" but unable to get any hint. Can somebody point out what is wrong and what should I do to run this code on my phone?

Thanks

Pagar
  • 87
  • 1
  • 12

1 Answers1

1

Please analyse the error logs with your code/implementation.

Caused by: java.lang.ClassCastException: com.local.broadcast_receiver.MainActivity cannot be cast to android.app.Activity

You have declared <activity android:name=".MainActivity"> which is actually a BroadcastReceiver. So declare it as <receiver />.

Update your manifest properly, refer Android Manifest Documentation

VishnuSP
  • 599
  • 5
  • 16