В приложении 1 я передаю действие и данные (i.putExtra ("data", "Some data");), и мне нужно получить эти данные в приложении 2, но они не получают эти данные во втором приложении. Все работает только в приложении 1, но не в приложении 2. Я зарегистрировал указанное действие c приложения 1 в приложении 2 (ACTION1 = "com.test.add.mystaticevent";), но широковещательный приемник данных в приложении 2 не работает.
Приложение 1:
public class MainActivity extends AppCompatActivity {
private MyBroadcastReceiver MyReceiver;
public static final String ACTION1 = "com.test.add.mystaticevent";
public static final String ACTION2 = "com.test.add.mydyanicevent";
private Button button,button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyReceiver = new MyBroadcastReceiver();
button =findViewById(R.id.button);
button1 =findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.ACTION1);
i.putExtra("data", "Some data"); //I am passing this data to receive in App2
i.setPackage("com.test.add"); //in API 26, it must be explicit now.
//since it's registered as a global (in the manifest), use sendBroadCast
//LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(i);
sendBroadcast(i);
}
});
//dynamic
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.ACTION2);
//since it's registered a local, use the LocalBroadcastManager.
LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(i);
//use this if registered as a global receiver.
//getActivity().sendBroadcast(i);
Log.v("MainActivity", "Should have sent the broadcast.");
}
});
}
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(MyReceiver, new IntentFilter(ACTION2));
}
@Override
protected void onDestroy()
{
super.onDestroy();
if(MyReceiver != null)
LocalBroadcastManager.getInstance(this).unregisterReceiver(MyReceiver);
Log.v("MainActivity", "receiver should be unregistered");
}
}
MyBroadcastReceiver:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Received an intent.", Toast.LENGTH_SHORT).show();
if (intent.getAction().equals(MainActivity.ACTION1)) { //is it our action1?
Toast.makeText(context, "We received an intent for Action1.", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(MainActivity.ACTION2)) { //is it our action2?
Toast.makeText(context, "We received an intent for Action2.", Toast.LENGTH_SHORT).show();
}
}
}
В манифесте:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.test.add.mystaticevent"/>
</intent-filter>
</receiver>
Приложение 2:
public class MainActivity extends AppCompatActivity {
private Button button;
private MyBroadcastReceiver MyReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
MyReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter("com.test.add.mystaticevent");
if(intentFilter != null)
{
registerReceiver(MyReceiver, intentFilter);
}
}
@Override
protected void onDestroy()
{
super.onDestroy();
if(MyReceiver != null)
unregisterReceiver(MyReceiver);
}
MyBroadcastReceiver: (данные не принимаются)
public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String data = intent.getStringExtra("data"); //I am not receiving this data
Toast.makeText(context,data,Toast.LENGTH_SHORT)
.show();
Log.i("BROADCAST RECEIVING" ,"Data received: " + data);
}
}
Mainfest:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.test.add.mystaticevent"/>
</intent-filter>
</receiver>