com.jsw.callcastreceiver.Call_BroadCastReceiver.java Source code

Java tutorial

Introduction

Here is the source code for com.jsw.callcastreceiver.Call_BroadCastReceiver.java

Source

package com.jsw.callcastreceiver;

/*
 * Copyright (c) 2017 Jos Luis del Pino Gallardo.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  jose.gallardo994@gmail.com
 */

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.support.v4.app.NotificationCompat;
import android.telephony.TelephonyManager;

/**
 * Created by usuario on 15/02/17.
 */

public class Call_BroadCastReceiver extends BroadcastReceiver {

    public static final int CALLNOTIFICATION = 1;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            String state = bundle.getString(TelephonyManager.EXTRA_STATE);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                String number = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

                Intent newIntent = new Intent(context, Telefon_Activity.class);
                newIntent.putExtra("number", number);
                PendingIntent pending = PendingIntent.getActivity(context, CALLNOTIFICATION, newIntent,
                        PendingIntent.FLAG_ONE_SHOT);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
                builder.setContentTitle("Telefon Activity");
                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setContentText("Incoming call from: " + number);
                builder.setDefaults(Notification.DEFAULT_VIBRATE);
                builder.setDefaults(Notification.DEFAULT_LIGHTS);
                //Aadir el objeto PendingItem a la notificicacion
                builder.setContentIntent(pending);
                //Aadir la notificacion al Manager
                NotificationManager notify = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                notify.notify(CALLNOTIFICATION, builder.build());
            }
        }
    }
}