Question

I am creating an application and a certain part of my code I need to create a notification when I receive a call. The problem is that I am not able to implement the status bar notification, since I have to check the number in the database and then show the required data in Notification.

Here is my Code :

public class IncomingCall extends BroadcastReceiver {

private Context mContext = null;
NotificationManager NM;
public String msg;

public void onReceive(Context context, Intent intent) {
    mContext = context;

    try {
        // TELEPHONY MANAGER class object to register one listner
        TelephonyManager tmgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);

        // Create Listner
        MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

        // Register listener for LISTEN_CALL_STATE
        tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    } catch (Exception e) {
        Log.e("Phone Receive Error", " " + e);
    }

}

private class MyPhoneStateListener extends PhoneStateListener {

    public void onCallStateChanged(int state, String incomingNumber) {

        Log.d("MyPhoneListener", state + "   incoming no:" + incomingNumber);

        if (state == 1) {

            msg = "New Phone Call Event. Incoming Number : "
                    + incomingNumber;
            int duration = Toast.LENGTH_LONG;
            notify();  // I am Getting the Error Here
            Toast toast = Toast.makeText(mContext, msg, duration);
            toast.show();
        }
    }
}

public void notify(View vobj) 
{
    String title = "TEST";
    String subject = "THIS IS TEST DATA";
    NM = (NotificationManager) mContext
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notify = new Notification(
            android.R.drawable.stat_notify_more, title,
            System.currentTimeMillis());
    PendingIntent pending = PendingIntent.getActivity(mContext, 0,
            new Intent(), 0);
    notify.setLatestEventInfo(mContext, subject, msg, pending);
    NM.notify(0, notify);
}


}

// Here is the Manifest File :

<receiver android:name=".IncomingCall" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
</receiver>

// I have added the Phone State Permission as well :

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

I understand that I am doing wrong some where but I am not able to recognize it... Please Tell me where I am wrong...

Was it helpful?

Solution

This might help some one. Here is the Implementation :

import android.annotation.SuppressLint;
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.support.v4.app.NotificationCompat;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

@SuppressLint("NewApi")
public class IncomingCall extends BroadcastReceiver {

    private Context mContext = null;
    NotificationManager NM;
    public String msg;

    public void onReceive(Context context, Intent intent) {
        mContext = context;

        try {
            // TELEPHONY MANAGER class object to register one listner
            TelephonyManager tmgr = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);

            // Create Listner
            MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

            // Register listener for LISTEN_CALL_STATE
            tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        } catch (Exception e) {
            Log.e("Phone Receive Error", " " + e);
        }

    }

    private class MyPhoneStateListener extends PhoneStateListener {

        public void onCallStateChanged(int state, String incomingNumber) {

            Log.d("MyPhoneListener", state + "   incoming no:" + incomingNumber);

            if (state == 1) {

                msg = incomingNumber;
                createNotification();
            }
        }
    }

    @SuppressLint("NewApi")
    public void createNotification() {
        // Prepare intent which is triggered if the
        // notification is selected
        Intent intent = new Intent(mContext, NotificationImplementActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent,
                0);

        // Build notification
        // Actions are just fake
        Notification noti = new NotificationCompat.Builder(mContext)
                .setContentTitle("New Call from " + msg)
                .setContentText("Details :")
                .setSmallIcon(R.drawable.logo).setContentIntent(pIntent)
                .build();
        NotificationManager notificationManager = (NotificationManager) mContext
                .getSystemService(Activity.NOTIFICATION_SERVICE);

        // hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, noti);

    }
}

Make Sure you have added the receiver to Manifest File as follows :

<receiver android:name=".IncomingCall" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top