无法启动接收器(Activity 未找到)

Unable to start receiver (Activity Not Found)

我开发了一个应用程序并将其放在应用程序商店中。之后我想更改包名称,所以我只是更改了 build.gradle 中的应用程序 ID,以便它在 link 中看起来合适。我没有更改任何其他内容,不是包名称,不是清单文件等。应用程序工作正常,但现在它在启动器 activity 上显示 ActivityNotFound 异常 错误,这是尽管清单文件中定义了 activity,但通过广播接收器调用。我可以知道我哪里错了吗? 这是清单文件编码:

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

下面是广播接收器的编码:

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

错误状态:

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?

当您在 gradle 中更改 applicationId 时,它会覆盖清单的 ID。 所以你需要改变你的代码:

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

收件人:

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

或者在您不需要考虑所有这些的情况下可能更简单:

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