Search code examples
javaandroidbroadcastreceiverdevelopment-environment

Unable to start receiver (Activity Not Found)


I developed an application and put that on app store. Afterwords i wanted to change the package name so i just changed the application ID in build.gradle so that it looks appropriate in link. I didn't change anything else not the package name, not the manifest file etc. Application worked fine but now it's showing an error of ActivityNotFound exception on the launcher activity which is called through a broadcast receiver although that activity is defined in manifest file. May i know where am i wrong at? This is the manifest file coding:

<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>

<activity
android:name=".BatteryChargerFast"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

And below is the coding of broadcast Receiver:

public class PowerConnectionReceiver extends BroadcastReceiver {
private String TAG="PowerConnectionReceiver";

@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setClassName("packagename", 
"packagename.BatteryChargerFast");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("fast", true);
context.startActivity(i);
}
}

the error states:

java.lang.RuntimeException: Unable to start receiver packagename.PowerConnectionReceiver: android.content.ActivityNotFoundException: Unable to find explicit activity class {packagename/packagename.BatteryChargerFast}; have you declared this activity in your AndroidManifest.xml?


Solution

  • When you change applicationId in gradle, it overrides the Manifest's id. so you need to change your code from:

    Intent i = new Intent();
    i.setClassName("packagename", 
    "packagename.BatteryChargerFast");
    

    To:

    Intent i = new Intent();
        i.setClassName("your.new.app.id", 
        "packagename.BatteryChargerFast");
    

    or may be even simpler where you don't need to consider all this:

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context.getApplicationContext(), BatteryChargerFast.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("fast", true);
        context.startActivity(i);
    }