Android:在通话过程中提出通知?

Ale*_*987 4 alert android phone-call broadcastreceiver android-notifications

我有一个广播接收器,可以收听来电.我想调整来电屏幕.现在我可以提供祝酒词并向通知栏添加通知(BTW用户无法将其拉下来,因为屏幕被锁定,在接受呼叫之前,这有点糟糕).我试图显示警报,但它崩溃了 - 是不允许的?是否有一种方法可以让广播接收器中的代码执行其他操作,例如更改调用者的头像或为其命名(即使联系人中不存在).我们只是说我的广播接收器拦截了一个电话 - 它可以将电话号码和自定义头像添加到联系人中,以便它们立即显示在呼叫屏幕中吗?

你怎么看?


编辑

我已经测试了供应商的代码,并且它有效,但是从后台线程更改UI是不安全的,所以我尝试稍微调整他的代码以使其线程安全,但吐司不会出于某种原因.你怎么看?

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);
                }
            } 
        });
    }
Run Code Online (Sandbox Code Playgroud)

Sir*_*get 7

你需要一个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);
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

并在清单中注册它<action android:name="android.intent.action.PHONE_STATE"></action>.

然后像这样创建一个Activity:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

有这种布局:

<?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>
Run Code Online (Sandbox Code Playgroud)

这将在来电屏幕上产生半透明的类似对话框的活动,允许用户接听电话(不会干扰触摸事件).