android中的广播接收器弹出消息

时间:2014-04-28 07:49:33

标签: android android-intent

在我的应用程序中,我使用广播接收器接收来电并向用户显示一些细节。它工作正常,但当我打开应用程序并收到来电时,它显示在应用程序的顶部,而不是来电屏幕的顶部。如何解决这个问题?

提前致谢

广播接收器

      public void onReceive(final Context context, final Intent intent) {                                         // 1
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);                         // 2
            if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {                                   // 3
                String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);  // 4
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent i = new Intent(context, Test1.class); 
                        i.putExtras(intent);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        context.startActivity(i);
                    }
                }, 2000);
            }
        }

讯息显示活动

    public class Test1 extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.popup);

        WindowManager.LayoutParams wmlp = getWindow().getAttributes();
        wmlp.gravity = Gravity.BOTTOM | Gravity.LEFT;
        String number = getIntent().getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

        TextView tv1 = (TextView)findViewById(R.id.tv1);
        tv1.setText(number);

        TextView tv2 = (TextView)findViewById(R.id.tv2);
        tv2.setText(MyApp.mDbh.check(number));

        new Handler().postDelayed(new Runnable(){
            public void run() {
                finish();                    
            }                   
        }, 10 *1000);
    }
    }

0 个答案:

没有答案