Skip to content

Instantly share code, notes, and snippets.

@thiagotn
Created December 10, 2015 21:37
Show Gist options
  • Save thiagotn/47b867f87442af97862a to your computer and use it in GitHub Desktop.
Save thiagotn/47b867f87442af97862a to your computer and use it in GitHub Desktop.
BroadcastReceiver
package br.com.heiderlopes.democomponentesandroid.broadcast;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.telephony.TelephonyManager;
import android.util.Log;
import br.com.heiderlopes.democomponentesandroid.R;
public class MeuBroadCast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("MY_DEBUG_TAG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
mBuilder.setContentTitle("Ligacao");
mBuilder.setContentText(phoneNumber);
mBuilder.setTicker("Você esta recebendo uma ligacao");
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationManager.notify(100, mBuilder.build());
Log.w("MY_DEBUG_TAG", phoneNumber);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment