1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by e_voloshchak, Jul 8, 2019.

  1. e_voloshchak

    e_voloshchak Lurker
    Thread Starter

    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
    Code (Java):
    1.  
    2. public class Connection extends android.telecom.Connection {
    3.     private static final String TAG = "Connection";
    4.  
    5.     public Connection() {
    6.         super();
    7.         setConnectionProperties(android.telecom.Connection.PROPERTY_SELF_MANAGED);
    8.     }
    9.  
    10.     @Override
    11.     public void onStateChanged(int state) {
    12.         super.onStateChanged(state);
    13.         Log.i(TAG, "onStateChanged state=" + android.telecom.Connection.stateToString(state));
    14.     }
    15.  
    16.     public void setConnectionDisconnected(int cause) {
    17.         setDisconnected(new DisconnectCause(cause));
    18.         destroy();
    19.         Log.i(TAG, "disconnected");
    20.     }
    21.  
    22.  
    23.     public void displayIncomingCallNotification() {
    24.         final NotificationManager notificationManager = (NotificationManager) Config.context.getSystemService(Config.context.NOTIFICATION_SERVICE);
    25.         String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
    26.         Handler.Callback callback = new Handler.Callback() {
    27.             public boolean handleMessage(Message msg) {
    28.                 Log.d("Call", "back");
    29.                 return true;
    30.             }
    31.         };
    32.         PendingIntent dismissIntent = NotificationActivity.getDismissIntent(1, Config.context, callback);
    33.  
    34.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    35.             NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
    36.  
    37.             notificationChannel.setDescription("Channel description");
    38.             notificationChannel.enableLights(true);
    39.             notificationChannel.setLightColor(Color.RED);
    40.             Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
    41.             notificationChannel.setSound(ringtoneUri, new AudioAttributes.Builder()
    42.                     .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
    43.                     .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
    44.                     .build());
    45.             notificationManager.createNotificationChannel(notificationChannel);
    46.         }
    47.  
    48.  
    49.         NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Config.context, NOTIFICATION_CHANNEL_ID);
    50.         Intent intent = new Intent(Intent.ACTION_MAIN, null);
    51.         intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
    52.         intent.setClass(Config.context, MainActivity.class);
    53.         PendingIntent pendingIntent = PendingIntent.getActivity(Config.context, 1, intent, 0);
    54.  
    55.         notificationBuilder.setAutoCancel(true)
    56.                 .setDefaults(Notification.DEFAULT_ALL)
    57.                 .setWhen(System.currentTimeMillis())
    58.                 .setSmallIcon(android.R.drawable.sym_action_call)
    59.                 .setTicker("Hearty365")
    60.                 //.setPriority(Notification.PRIORITY_HIGH)
    61.                 .setContentTitle("Default notification")
    62.                 .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
    63.                 .setContentInfo("Info")
    64.                 .setOngoing(true)
    65.                 .setContentIntent(pendingIntent)
    66.                 .setFullScreenIntent(pendingIntent, true)
    67.                 .addAction(android.R.drawable.sym_call_incoming, "Accept", null)
    68.                 .addAction(android.R.drawable.sym_call_missed, "Decline", dismissIntent);
    69.         final Notification notification = notificationBuilder.build();
    70.         notification.flags |= Notification.FLAG_INSISTENT;
    71.  
    72.         new android.os.Handler().postDelayed(
    73.                 new Runnable() {
    74.                     public void run() {
    75.                         notificationManager.notify(1, notification);
    76.                     }
    77.                 },
    78.                 4200);
    79.     }
    80.    
    81.  
    82.     @Override
    83.     public void onShowIncomingCallUi() {
    84.         displayIncomingCallNotification();
    85.  
    86.         Log.i(TAG, "onShowIncomingCallUi ");
    87.     }
    88.  
    89.     @Override
    90.     public void onDisconnect() {
    91.         setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
    92.         destroy();
    93.         Log.i(TAG, "disconnected local");
    94.     }
    95.  
    96.  
    97.     @Override
    98.     public void onReject() {
    99.         setDisconnected(new DisconnectCause(DisconnectCause.REJECTED));
    100.         destroy();
    101.         Log.i(TAG, "disconnected reject");
    102.     }
    103.  
    104.     @Override
    105.     public void onAnswer() {
    106.         Log.i(TAG, "Call answered");
    107.     }
    108. }
    When user clicks "Dismiss" button, dismissIntent is called

    Code (Java):
    1.  
    2. public class NotificationActivity extends Activity {
    3.  
    4.     public static final String NOTIFICATION_ID = "NOTIFICATION_ID";
    5.  
    6.     @Override
    7.     protected void onCreate(Bundle savedInstanceState) {
    8.         super.onCreate(savedInstanceState);
    9.         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    10.         manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));
    11.  
    12.         finish();
    13.     }
    14.  
    15.     public static PendingIntent getDismissIntent(int notificationId, Context context) {
    16.        
    17.         Intent intent = new Intent(context, NotificationActivity.class);
    18.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    19.         intent.putExtra(NOTIFICATION_ID, notificationId);
    20.         PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    21.  
    22.         return dismissIntent;
    23.     }
    24. }
    Ideally, i would want to call Connection.setConnectionDisconnected() in NotificationActivity`s onCreate()
     


  2. Brian Berry

    Brian Berry Well-Known Member

    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)
     
    Sagar Shah likes this.
  3. e_voloshchak

    e_voloshchak Lurker
    Thread Starter

    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.
     
  4. Brian Berry

    Brian Berry Well-Known Member

    There must be a cancel method for the connection
     
Loading...

Share This Page

Loading...