Статический receiver не регистрируется
Пытаюсь понять, в чем проблема. Статически регистрируя ресивер, он не подает признаков жизни вообще. Перепробовал множество кода с интернета, никакого результата. Причем, если регистрировать ресивер динамически, то все хорошо и он работает как положено. Вот только мне нужно, что бы ресивер постоянно обрабатывал события.
MainActivity.java
private AppBarConfiguration appBarConfiguration;
private ActivityMainBinding binding;
public static final String NEW_CAT = "ru.app.NEW_CAT";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
binding.fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Моё действие 111", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
Intent newIntent = new Intent(NEW_CAT);
newIntent.putExtra("catname", "барсик");
newIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(newIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
}
PhoneReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class PhoneReceiver extends BroadcastReceiver {
public PhoneReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Сработал ресивер: " + context + " |||||||| " + intent);
Toast.makeText(context, "Сработал ресивер.", Toast.LENGTH_LONG).show();
}
}
Manifest
<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/Theme.PhoneRipper">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.PhoneRipper.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".PhoneReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="ru.app.NEW_CAT"></action>
</intent-filter>
</receiver>
</application>