Bu derste ilk broadcastreceiver örneklerimizi yaptık.
Öncelikle BroadcastReceiver sınıfından extend ettiğimiz bir sınıf yarattık. Daha sonra da broadcast receiver sınıf örneğini inner class olarak nasıl tanımlarız onu gördük.
Activity’lerden broadcastreceiverlara erişmek için intentleri kullanıyoruz. Intent nesnesini oluşturduktan sonra sendBroadcast methoduyla activity’den receiverlarımıza erişebiliyoruz. Bu erişim sonrasında ilgili broadcast receiver sınıfının onReceive methodu çağrılıyor.
Aynı activitylerde olduğu gibi bir receiver sınıf yarattığımızda manifest dosyasına ilgili tanımı yapmayı unutmayalım(statik broadcast örneklerinde). Eğer broadcast receiverımızı inner class ise manifestte direk sınıf adını yazarak tanımlayamıyoruz. Bunun yerine
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 |
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); sendBroadcast(intent); } public void sendBroadcastMessajetoInnerClass(View view) { Intent intent=new Intent(this, MySecondReceiver.class); 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 14 15 16 17 18 19 20 21 22 23 24 25 |
<?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"></receiver> <receiver android:name=".MainActivity$MySecondReceiver"></receiver> </application> </manifest> |