• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How to execute a class method by clicking a notification action button?

Hello everybody!
I`m developing a simple messaging/calling app, using Android`s self managed connection service.
So, when I want to display incoming call, i create notification with two action buttons (Accept and Decline).
The problem is, i cannot call setDisconnected method of my Connection when users clicks "Decline" button, because, AFAIK, notification actions can only launch pending intents.
Here`s my Connection class
Java:
public class Connection extends android.telecom.Connection {
    private static final String TAG = "Connection";

    public Connection() {
        super();
        setConnectionProperties(android.telecom.Connection.PROPERTY_SELF_MANAGED);
    }

    @Override
    public void onStateChanged(int state) {
        super.onStateChanged(state);
        Log.i(TAG, "onStateChanged state=" + android.telecom.Connection.stateToString(state));
    }

    public void setConnectionDisconnected(int cause) {
        setDisconnected(new DisconnectCause(cause));
        destroy();
        Log.i(TAG, "disconnected");
    }


    public void displayIncomingCallNotification() {
        final NotificationManager notificationManager = (NotificationManager) Config.context.getSystemService(Config.context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
        Handler.Callback callback = new Handler.Callback() {
            public boolean handleMessage(Message msg) {
                Log.d("Call", "back");
                return true;
            }
        };
        PendingIntent dismissIntent = NotificationActivity.getDismissIntent(1, Config.context, callback);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);

            notificationChannel.setDescription("Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            notificationChannel.setSound(ringtoneUri, new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build());
            notificationManager.createNotificationChannel(notificationChannel);
        }


        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Config.context, NOTIFICATION_CHANNEL_ID);
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(Config.context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(Config.context, 1, intent, 0);

        notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(android.R.drawable.sym_action_call)
                .setTicker("Hearty365")
                //.setPriority(Notification.PRIORITY_HIGH)
                .setContentTitle("Default notification")
                .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
                .setContentInfo("Info")
                .setOngoing(true)
                .setContentIntent(pendingIntent)
                .setFullScreenIntent(pendingIntent, true)
                .addAction(android.R.drawable.sym_call_incoming, "Accept", null)
                .addAction(android.R.drawable.sym_call_missed, "Decline", dismissIntent);
        final Notification notification = notificationBuilder.build();
        notification.flags |= Notification.FLAG_INSISTENT;

        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        notificationManager.notify(1, notification);
                    }
                },
                4200);
    }
   

    @Override
    public void onShowIncomingCallUi() {
        displayIncomingCallNotification();

        Log.i(TAG, "onShowIncomingCallUi ");
    }

    @Override
    public void onDisconnect() {
        setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
        destroy();
        Log.i(TAG, "disconnected local");
    }


    @Override
    public void onReject() {
        setDisconnected(new DisconnectCause(DisconnectCause.REJECTED));
        destroy();
        Log.i(TAG, "disconnected reject");
    }

    @Override
    public void onAnswer() {
        Log.i(TAG, "Call answered");
    }
}

When user clicks "Dismiss" button, dismissIntent is called

Java:
public class NotificationActivity extends Activity {

    public static final String NOTIFICATION_ID = "NOTIFICATION_ID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));

        finish();
    }

    public static PendingIntent getDismissIntent(int notificationId, Context context) {
       
        Intent intent = new Intent(context, NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(NOTIFICATION_ID, notificationId);
        PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        return dismissIntent;
    }
}

Ideally, i would want to call Connection.setConnectionDisconnected() in NotificationActivity`s onCreate()
 
I don't execute code outside the IDE very well but can't you do the following to call the method you want to use ?


Connection callConnection0 = new Connection()
callConnection0.setConnectionDisconnected(1)

No, it doesn`t work. It creates a new instance of Connection (as far as I understand), but i want to disconnect my current, already created connection.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones