在传入的call-android上启动活动

时间:2014-12-20 06:27:24

标签: java android

我想在来电时启动活动我的代码是这样的:

    package com.suraj.test;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.content.BroadcastReceiver;
import android.util.Log;
import android.widget.Toast;

public class IncomingCallReciever extends BroadcastReceiver {

    private Context myContext;
    private Intent myIntent;

    public void onReceive(Context context, Intent intent) {
        myIntent = new Intent(context, CallerId.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

        myContext = context;
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        int events = PhoneStateListener.LISTEN_CALL_STATE;
        tm.listen(phoneStateListener, events);
    }

    private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:    
                    break;

                case TelephonyManager.CALL_STATE_RINGING:   
                    Thread thread = new Thread() {
                        private int sleepTime = 400;

                        @Override
                        public void run() {
                            super.run();
                            try {
                                int wait_Time = 0;

                                while (wait_Time < sleepTime) {
                                sleep(400);
                                  wait_Time += 100;
                                }
                            } catch (Exception e) {
                            } finally {
                            }
                            IncomingCallReciever.this.myContext.startActivity(IncomingCallReciever.this.myIntent);

                        }
                    };
                    thread.setPriority(Thread.MAX_PRIORITY);
                    thread.run();
            }

            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    try {
                        CallerId.fa.finish();

                    } catch (NullPointerException e) {
                    }

                    break;
            }

            super.
                    onCallStateChanged(state, incomingNumber);
        }
    };
}

问题是在显示呼叫者屏幕后1或2秒后显示活动。我想在调用者屏幕后立即启动它。我该怎么办

1 个答案:

答案 0 :(得分:0)

尝试删除此块

 try {
   int wait_Time = 0;
   while (wait_Time < sleepTime) {
       sleep(400);
       wait_Time += 100;
   }
 } catch (Exception e) {
 } finally {}