Search code examples
androidandroid-intentbroadcastreceiverintentfilter

BroadcastReceiver declared in Android Manifest, App force closing.


I keep getting Force close error while testing this app. App opening fine.. but after 3-4 seconds force close error dialog box coming up. code included. Any help will be appreciated. Thanks

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cy.headset"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver android:name=".Main" >
        <intent-filter>
            <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
        </intent-filter>
    </receiver>

    <activity
        android:name="com.cy.headset.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Java Code

package com.cy.headset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Main extends BroadcastReceiver{

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

    Toast.makeText(context, "Headphone connected", Toast.LENGTH_LONG).show();

}}

XML UI file

<RelativeLayout 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"
tools:context=".Main" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_margin="10dp"
    android:layout_marginTop="151dp"
    android:text="@string/hello_world"
    android:textAlignment="center" />


Solution

  • What is the package of your BroadcastReceiver class which is Main.java?

    <receiver android:name=".Main" >
        <intent-filter>
            <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
        </intent-filter>
    </receiver>
    

    It should not com.cy.headset right? Since it will be a conflict with your main activity which is Main.java

    I think there are two solutions on this one specify the package of your receiver in the manifest.

    example:

    <receiver android:name="com.package.name.Main" >
        <intent-filter>
            <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
        </intent-filter>
    </receiver>
    

    Or if you want your receiver to be at the same package of your main activity which is Main.java rename your receiver

    example:

    <receiver android:name=".BatteryChange" >
        <intent-filter>
            <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
        </intent-filter>
    </receiver>
    
    <activity
        android:name="com.cy.headset.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

    Another way to do it is.

    <receiver android:name="com.cy.headset.BatteryChange" >
        <intent-filter>
            <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
        </intent-filter>
    </receiver>
    
    <activity
        android:name="com.cy.headset.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>