extends BroadcastReceiver : BroadcastReceiver « Core Class « Android






extends BroadcastReceiver

   
package app.test;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

 class AndroidReceiver1 extends BroadcastReceiver {
  Context context;
  public static int NOTIFICATION_ID = 21321;

  @Override
  public void onReceive(Context context, Intent intent) {
    this.context = context;
    showNotification();
  }

  private void showNotification() {

    NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.icon,
        "EoeAndroidReceiver1", System.currentTimeMillis());

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
        new Intent(context, Test.class), 0);
    notification.setLatestEventInfo(context, "EoeAndroidReceiver1", null,
        contentIntent);
    notificationManager.notify(NOTIFICATION_ID, notification);
  }
}
class AndroidReceiver2 extends BroadcastReceiver {
  Context context;
  @Override
  public void onReceive(Context context, Intent intent) {
    this.context = context;
    DeleteNotification();
  }
  private void DeleteNotification() {
    NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(AndroidReceiver1.NOTIFICATION_ID);
  }
}

public class Test extends Activity {
  public static final int ITEM0 = Menu.FIRST;
  public static final int ITEM1 = Menu.FIRST + 1;

  static final String ACTION_1 = "com.eoeandroid.action.NEW_BROADCAST_1";
  static final String ACTION_2 = "com.eoeandroid.action.NEW_BROADCAST_2";

  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
  }
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, ITEM0, 0, "Notification");
    menu.add(0, ITEM1, 0, "1Notification");
    menu.findItem(ITEM1);
    return true;
  }

  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case ITEM0:
      actionClickMenuItem1();
      break;
    case ITEM1:
      actionClickMenuItem2();
      break;
    }
    return true;
  }
  private void actionClickMenuItem1() {
    Intent intent = new Intent(ACTION_1);
    sendBroadcast(intent);
  }
  private void actionClickMenuItem2() {
    Intent intent = new Intent(ACTION_2);
    sendBroadcast(intent);
  }
}


//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="Start" />
</LinearLayout>

   
    
    
  








Related examples in the same category

1.Scan SDReceiver extends BroadcastReceiver
2.Handles all calling, receiving calls, and UI interaction in the WalkieTalkie app