Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Toast over PhoneScreen in LockState

Our goal is to show a toast when an incomming call happens. This won't work when the device is locked and an incomming call occures. Then the toast is visible behind the "locked fullscreen incomming call view".

We tried different approches with like the same result:

  • PhoneCallListener / BroadCastReciver
  • Instead of a toast, use a new Intent with some Flags (ShowOnLockScreen etc.)

Permission:

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

Setup for PhoneCallListener:

public class PhoneCallDetector : PhoneStateListener
{
    public override void OnCallStateChanged(CallState state, string incomingNumber)
    {
        ShowToast(incomingNumber);
        base.OnCallStateChanged(state, incomingNumber);
    }


    private void ShowToast(string phonenumber)
    {
        Toast toast = Toast.MakeText(Application.Context, phonenumber, ToastLength.Long);
        toast.SetGravity(GravityFlags.Center, 0, 0);
        toast.Show();
    }
}

We know some apps which can display toasts successfully over the "locked fullscreen incomming call view", but they written in java... They also don't do anything special then Toast.MakeText(....).

Edit: => The PhoneStateListener lifes in the background. Started from a service.

How the service get started?

Intent serviceStart = new Intent(context, typeof(PhoneCallService));
context.StartService(serviceStart);

How the PhoneCallDetector is invoked?

 var phoneCallDetector = m_scope.Resolve<PhoneCallDetector>();
 var tm = (TelephonyManager)GetSystemService(TelephonyService);
 tm.Listen(phoneCallDetector, PhoneStateListenerFlags.CallState);

Thanks for helping me :-)

like image 252
Cyril Iselin Avatar asked Jul 09 '18 09:07

Cyril Iselin


1 Answers

You need to read this and also refer this link.

Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS = "lock_screen_allow_private_notifications"

Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS = "lock_screen_show_notifications"


int ShowAll = Settings.Secure.getInt(getContentResolver(),"lock_screen_allow_private_notifications", -1); 
int NotificationEnable = Settings.Secure.getInt(getContentResolver(),"lock_screen_show_notifications", -1); 

if(ShowAll > 0 && NotificationEnable > 0){
//post notification
}

Refer this Also Section:-Lock screen notifications

like image 147
Hitesh Anshani Avatar answered Nov 08 '22 07:11

Hitesh Anshani