Android: Presenting notification during a call?

I have a broadcast receiver that listens for incoming calls. And I want to customize the incoming call screen. Right now I can submit toasts and add notifications to the notification panel (BTW, the user cannot pull it out, because the screen is locked before accepting a call, it sucks). I tried to show a warning, but it crashed - not allowed? Is there a way for the code in the broadcast receiver to do other things, such as changing the caller's avatar or naming him (even if it does not exist in the contacts). Let me just say that my broadcast receiver intercepts a call - can it add a phone number and a personalized avatar to contacts so that they are immediately displayed on the call screen?

What do you think?


Edit

I tested the provider code and it worked, but it is not safe to change the interface from the background thread, so I tried to configure its code a little to make it thread safe, but the toast does not appear for some reason. What do you think?

private Handler handler = new Handler(); private void showToast() { Thread thread = new Thread(null, doBackgroundThreadProcessing, "Background"); thread.start(); } private Runnable doBackgroundThreadProcessing = new Runnable() { public void run() { backgroundThreadProcessing(); } }; private void backgroundThreadProcessing() { handler.post(new Runnable() { public void run() { int count = 0; try{ while (count < 10) { toast.show(); Thread.sleep(1850); count++; } } catch(Exception e){ Log.e("LongToast", "", e); } } }); } 
+4
source share
3 answers

You need a BroadcastReceiver:

 public class IncomingBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { MyLog.d("IncomingBroadcastReceiver: onReceive: "); String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); MyLog.d("IncomingBroadcastReceiver: onReceive: " + state); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { Intent i = new Intent(context, IncomingCallActivity.class); i.putExtras(intent); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } } } 

And register it in the manifest on <action android:name="android.intent.action.PHONE_STATE"></action> .

Then create an action like this:

 public class IncomingCallActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { MyLog.d("IncomingCallActivity: onCreate: "); // TODO Auto-generated method stub super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); setContentView(R.layout.main); String number = getIntent().getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); TextView text = (TextView)findViewById(R.id.text); text.setText("Incoming call from " + number); } } 

which has this layout:

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:text="text" android:windowBackground="@android:color/transparent" android:windowIsTranslucent="true" android:windowAnimationStyle="@android:style/Animation.Translucent"></TextView> 

This will lead to a translucent dialogical action on top of the incoming call screen, which allows the user to answer the call (does not interfere with touch events).

+7
source

Prior to Android 2.3, you cannot override the calling screen, but you can show a Toast message on it. The longest toast period is 3 seconds, after which it will disappear. However, you can create a thread that calls the show () method of the toast every 2 seconds. Something like that:

 Thread t = new Thread() { public void run() { try { while (true) { if( isInCall ){ toast.cancel(); break; } toast.show(); sleep(1850); } } catch (Exception e) { Log.d("Exception: " + e.toString()); } } }; t.start(); 

You must declare a toast:

 private Toast toast; 

You need to run the toast object before showing it.

 toast = new Toast(getBaseContext()); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.toast, null, false); toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); 
+4
source
0
source

Source: https://habr.com/ru/post/1345173/


All Articles