explicit broadcast implemented

This commit is contained in:
Paul 2020-10-21 19:33:32 +02:00
parent 3c769ef513
commit 76f18dab01
7 changed files with 39 additions and 5 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

View File

@ -16,10 +16,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".TestReceiver">
<intent-filter>
<action android:name="com.ploedige.BroadcastReceiver.intentfilters.TESTBROADCAST"/>
<action android:name="com.ploedige.TESTBROADCAST"/>
</intent-filter>
</receiver>
</application>

View File

@ -2,7 +2,11 @@ package com.ploedige.broadcastreceiver;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {

View File

@ -4,11 +4,13 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class TestReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("IntentFilterTest","Broadcast Received!");
Toast.makeText(context, "Broadcast Received",Toast.LENGTH_SHORT).show();
}
}

View File

@ -7,6 +7,7 @@
tools:context=".MainActivity">
<TextView
android:id="@+id/message_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the Receiver App"
@ -15,5 +16,4 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -2,7 +2,10 @@ package com.ploedige.broadcastsender;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@ -11,4 +14,16 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void broadcast(View view){
Intent broadcastIntent = new Intent("com.ploedige.TESTBROADCAST");
broadcastIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
//since Android O implicit Broadcasts are disabled.
//this gets the component and makes an explicit broadcast
ComponentName componentName = new ComponentName(
"com.ploedige.broadcastreceiver",
"com.ploedige.broadcastreceiver.TestReceiver");
broadcastIntent.setComponent(componentName);
sendBroadcast(broadcastIntent);
}
}

View File

@ -7,12 +7,25 @@
tools:context=".MainActivity">
<TextView
android:id="@+id/message_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="This is the Broadcast Sender App"
android:textSize="4mm"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:text="Send Broadcast"
android:onClick="broadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/message_tv"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>