[embedyt]https://www.youtube.com/watch?v=prueLyTvOwI[/embedyt]
Broadcast receiving app:
MainActivity.javaOrderedReceiver1.javaOrderedReceiver2.javaOrderedReceiver3.javaAndroidManifest.xml
package com.codinginflow.broadcastexample; import android.Manifest; import android.content.IntentFilter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { OrderedReceiver1 orderedReceiver1 = new OrderedReceiver1(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter filter = new IntentFilter("com.codinginflow.EXAMPLE_ACTION"); filter.setPriority(1); registerReceiver(orderedReceiver1, filter, Manifest.permission.VIBRATE, null); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(orderedReceiver1); } }
package com.codinginflow.broadcastexample; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class OrderedReceiver1 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int resultCode = getResultCode(); String resultData = getResultData(); Bundle resultExtras = getResultExtras(true); String stringExtra = resultExtras.getString("stringExtra"); resultCode++; stringExtra += "->OR1"; String toastText = "OR1\n" + "resultCode: " + resultCode + "\n" + "resultData: " + resultData + "\n" + "stringExtra: " + stringExtra; Toast.makeText(context, toastText, Toast.LENGTH_LONG).show(); resultData = "OR1"; resultExtras.putString("stringExtra", stringExtra); setResult(resultCode, resultData, resultExtras); } }
package com.codinginflow.broadcastexample; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class OrderedReceiver2 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int resultCode = getResultCode(); String resultData = getResultData(); Bundle resultExtras = getResultExtras(true); String stringExtra = resultExtras.getString("stringExtra"); resultCode++; stringExtra += "->OR2"; String toastText = "OR2\n" + "resultCode: " + resultCode + "\n" + "resultData: " + resultData + "\n" + "stringExtra: " + stringExtra; Toast.makeText(context, toastText, Toast.LENGTH_LONG).show(); resultData = "OR2"; resultExtras.putString("stringExtra", stringExtra); setResult(resultCode, resultData, resultExtras); //abortBroadcast(); } }
package com.codinginflow.broadcastexample; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class OrderedReceiver3 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int resultCode = getResultCode(); String resultData = getResultData(); Bundle resultExtras = getResultExtras(true); String stringExtra = resultExtras.getString("stringExtra"); resultCode++; stringExtra += "->OR3"; String toastText = "OR3\n" + "resultCode: " + resultCode + "\n" + "resultData: " + resultData + "\n" + "stringExtra: " + stringExtra; Toast.makeText(context, toastText, Toast.LENGTH_LONG).show(); resultData = "OR3"; resultExtras.putString("stringExtra", stringExtra); setResult(resultCode, resultData, resultExtras); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.codinginflow.broadcastexample"> <permission android:name="com.codinginflow.CUSTOM_PERMISSION" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <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/AppTheme"> <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=".OrderedReceiver2" android:permission="android.permission.INTERNET"> <intent-filter android:priority="2"> <action android:name="com.codinginflow.EXAMPLE_ACTION" /> </intent-filter> </receiver> <receiver android:name=".OrderedReceiver3" android:permission="com.codinginflow.CUSTOM_PERMISSION"> <intent-filter android:priority="3"> <action android:name="com.codinginflow.EXAMPLE_ACTION" /> </intent-filter> </receiver> </application> </manifest>
Broadcast sending app (BroadcastSender):
SenderReceiver.javaAndroidManifest.xmlactivity_main.xmlMainActivity.java
package com.codinginflow.broadcastsender; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class SenderReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int resultCode = getResultCode(); String resultData = getResultData(); Bundle resultExtras = getResultExtras(true); String stringExtra = resultExtras.getString("stringExtra"); resultCode++; stringExtra += "->SenderReceiver"; String toastText = "SenderReceiver\n" + "resultCode: " + resultCode + "\n" + "resultData: " + resultData + "\n" + "stringExtra: " + stringExtra; Toast.makeText(context, toastText, Toast.LENGTH_LONG).show(); resultData = "SenderReceiver"; resultExtras.putString("stringExtra", stringExtra); setResult(resultCode, resultData, resultExtras); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.codinginflow.broadcastsender"> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="com.codinginflow.CUSTOM_PERMISSION" /> <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/AppTheme"> <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=".SenderReceiver" /> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:padding="16dp" tools:context=".MainActivity"> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Message" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendBroadcast" android:text="Send Broadcast" /> </LinearLayout>
package com.codinginflow.broadcastsender; import android.Manifest; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text_view); } public void sendBroadcast(View v) { Intent intent = new Intent("com.codinginflow.EXAMPLE_ACTION"); intent.setPackage("com.codinginflow.broadcastexample"); Bundle extras = new Bundle(); extras.putString("stringExtra", "Start"); sendOrderedBroadcast(intent, Manifest.permission.WAKE_LOCK, new SenderReceiver(), null, 0, "Start", extras); } }