安卓:$ P $在通话过程中psenting的通知? [英] Android: Presenting a notification during a call?

查看:154
本文介绍了安卓:$ P $在通话过程中psenting的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个广播接收器监听来电。我想调整的来电屏幕。现在,我可以present敬酒,并添加通知,通知栏(顺便说一句,用户可以拔不下来,因为在屏幕锁定,接受呼叫,它还挺吸收之前)。我试图以显示警报,但它崩溃 - 这是不允许的?有没有办法为code在广播接收机做其他的事情,比如修改调用者的头像,或给它一个名称(即使它没有在联系人存在)。远的不说,我的广播接收机截获一个电话 - 可以将它添加电话号码和一个自定义头像的联系人,这样他们将立即psented在呼叫屏幕$ P $?

你怎么看?


修改

我已经测试厂商的code,和它的工作,但它是不安全的改变从后台线程的用户界面,所以我试图调整自己的codeA位,使其线程安全的,但敬酒没有按'T出现的某些原因。你怎么看?

 专用处理器处理器=新的处理程序();

    私人无效showToast(){
        线程线程=新主题(空,doBackgroundThreadProcessing,背景);
        thread.start();
    }

    私人可运行doBackgroundThreadProcessing =新的Runnable(){
        公共无效的run(){
            backgroundThreadProcessing();
        }
    };

    私人无效backgroundThreadProcessing(){
        handler.post(新的Runnable(){
            公共无效的run(){
                诠释计数= 0;
                尝试{
                    而(计数小于10){
                        toast.show();
                        视频下载(1850);
                        算上++;

                    }
                }
                赶上(例外五){

                    Log.e(LongToast,,E);
                }
            }
        });
    }
 

解决方案

您需要这样一个BroadcastReceiver:

 公共类IncomingBroadcastReceiver扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){

        MyLog.d(IncomingBroadcastReceiver:的onReceive:);

        字符串状态= intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        MyLog.d(IncomingBroadcastReceiver:的onReceive:+状态);
        如果(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
        {
            意图I =新的意图(背景下,IncomingCallActivity.class);
            i.putExtras(意向);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(ⅰ);
        }

    }

}
 

和在清单中注册到<作用机器人:名称=android.intent.action.PHONE_STATE>< /作用>

然后创建一个这样的活动:

 公共类IncomingCallActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){

        MyLog.d(IncomingCallActivity:的onCreate:);

        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);

        。getWindow()addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        。getWindow()addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

        的setContentView(R.layout.main);

        串号= getIntent()getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)。
        TextView的文字=(TextView的)findViewById(R.id.text);
        text.setText(+数字的来电);
    }
}
 

有这样的布局:

 < XML版本=1.0编码=UTF-8&GT?;

< TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:ID =@ + ID /文
    机器人:layout_width =match_parent机器人:layout_height =match_parent
    机器人:重力=center_vertical | center_horizo​​ntal
    机器人:文本=文本
    机器人:windowBackground =@机器人:彩色/透明
    机器人:windowIsTranslucent =真
    机器人:windowAnimationStyle =@安卓风格/ Animation.Translucent>< / TextView的>
 

这将产生在来电屏幕的顶部上的半透明对话框样活性,其允许用户应答该呼叫(不与触摸事件干扰)。

I have a broadcast receiver that listens to incoming calls. And I want to tweak the incoming call screen. Right now I can present toasts and add notifications to the notification bar (BTW the user can't pull it down because the screen is locked, before accepting the call, which kinda sucks). I tried to show an alert but it crashed - is it not allowed? Is there a way for the code in the broadcast receiver to do other things, like change the avatar of the caller or give it a name (even if it doesn't exist in the contacts). Let's just say my broadcast receiver intercepts a call - can it add the phone number and a custom avatar to the contacts, so that they will immediately be presented in the call screen?

What do you think?


Edit

I have tested vendor's code, and it worked, but it is not safe to change the UI from a background thread, so I tried to tweak his code a bit to make it thread safe but the toast doesn't 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);
                }
            } 
        });
    }

解决方案

You need a BroadcastReceiver like that:

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 to <action android:name="android.intent.action.PHONE_STATE"></action>.

Then create an Activity like that:

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 produce a translucent dialog-like activity on top of the incoming call screen, that allows the user to answer the call (doesn't interfere with touch events).

这篇关于安卓:$ P $在通话过程中psenting的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆