Bu derste action name ile broadcast receiver kullanımına örnekler yaptık.
Daha önceki dersimizde sınıf adını parametre olarak alan intent ile activity’den broadcast mesaj yollamıştık. Bu derste aynı örneği action name ile yaptık.
Bunu yapmak için manifest dosyasında tanımladığımız receiver etiketli alıcılara intent filter ve action name tanımladık ve bunları intentlerizi oluştururken kullandık.
Eğer tüm receiverlarımızda aynı action name’i kullanırsak, receiverlarımız manifest dosyasındaki yaratılma sırasıyla çağrılırlar.
Bu derste yazılan kodlar aşağıdaki gibidir:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
public class MainActivity extends AppCompatActivity { private static final String TAG=MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendBroadcastMessage(View v){ //Intent intent=new Intent(this, MyFirstReceiver.class); Intent intent=new Intent("my.custom.action.name"); sendBroadcast(intent); } public void sendBroadcastMessajetoInnerClass(View view) { //Intent intent=new Intent(this, MySecondReceiver.class); Intent intent = new Intent("my.custom.anotheraction.name"); sendBroadcast(intent); } public static class MySecondReceiver extends BroadcastReceiver{ private static final String TAG=MySecondReceiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "Inner receiver'dan selamlar"); Toast.makeText(context, "Inner receiver'dan selamlar", Toast.LENGTH_LONG).show(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.example.emrealtunbilek.broadcastreceiverornek1.MainActivity"> <Button android:id="@+id/btnBroadcastNow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:onClick="sendBroadcastMessage" android:text="Broadcast Now!" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/btnBroadcastNow" android:layout_centerHorizontal="true" android:onClick="sendBroadcastMessajetoInnerClass" android:layout_marginBottom="100dp" android:text="Broadcast to InnerClass" /> </RelativeLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 |
public class MyFirstReceiver extends BroadcastReceiver { private static final String TAG=MyFirstReceiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "Birinci receiver'dan selamlar"); Toast.makeText(context, "Birinci receiver'dan selamlar", Toast.LENGTH_LONG).show(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class MySecondReceiver extends BroadcastReceiver { private static final String TAG=MySecondReceiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "İkinci receiver'dan selamlar"); Toast.makeText(context, "İkinci receiver'dan selamlar", Toast.LENGTH_LONG).show(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.emrealtunbilek.broadcastreceiverornek1"> <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=".MyFirstReceiver"> <intent-filter> <action android:name="my.custom.action.name"></action> </intent-filter> </receiver> <receiver android:name=".MySecondReceiver"> <intent-filter> <action android:name="my.custom.action.name"></action> </intent-filter> </receiver> <receiver android:name=".MainActivity$MySecondReceiver"> <intent-filter> <action android:name="my.custom.action.name"></action> </intent-filter> </receiver> </application> </manifest> |