不能在没有任何活动的情况下调用处理程序???

时间:2014-03-23 22:48:54

标签: android android-activity broadcastreceiver handler delay

我希望我的广播接收器在接听来电4秒后打开一个活动。为了延迟,我使用了Handler类,但是发生了一些非常奇怪的事情。首先,请参阅我的代码:

package com.example.nrsearch;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class CallReceiver extends BroadcastReceiver {
public CallReceiver() {
}

public Context context;

@Override
public void onReceive(Context context, Intent intent) {
    Log.i("CallReceiverBroadcast", "onReceive() is called.");
    this.context = context;
    TelephonyManager teleMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    PhoneStateListener psl = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            Log.i("CallReceiverBroadcast", "onCallStateChanged() is called.");
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                Log.i("CallReceiverBroadcast", "Incoming call caught. Caller's number is " + incomingNumber + ".");
                if (!checkContactExistance(incomingNumber)) {
                    Handler handler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            if (msg.what == 1) {
                                Log.i("CallReceiverBroadcast", "Handler works!");
                            }
                        }
                    };
                    startNumberDisplayActivity(incomingNumber);
                    Message message = Message.obtain(handler, 1);
                    handler.sendMessageDelayed(message, 1*4000);
                }
            }
            Log.i("CallReceiverBroadcast", "onCallStateChanged() is finished.");
        }
    };
    teleMgr.listen(psl, PhoneStateListener.LISTEN_CALL_STATE);
    teleMgr.listen(psl, PhoneStateListener.LISTEN_NONE);
    Log.i("CallReceiverBroadcast", "onReceive() is finished.");
}

public void startNumberDisplayActivity(String incomingNumber) {
    Intent i = new Intent(context, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putExtra("incomingNumber", incomingNumber);
    context.startActivity(i);
}

public boolean checkContactExistance(String number) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    ContentResolver contentResolver = context.getContentResolver();
    Cursor contactLookup = contentResolver.query(uri, new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (contactLookup != null && contactLookup.getCount() > 0) {
        Log.i("CallReceiverBroadcast", "The incoming number exists.");
        contactLookup.close();
        return true;
    } else {
        Log.i("CallReceiverBroadcast", "The incoming number doesn't exist.");
        contactLookup.close();
        return false;
    }
}
}

问题是,现在startNumberDisplayActivity(incomingNumber);在处理程序handleMessage()方法之外,因为如果我把这行放在那里,那么处理程序不工作,它永远不会被调用。但是我需要在处理程序中启动此活动,因为我需要延迟它。如果我从其中启动活动,为什么处理程序不起作用?怎么做到这一点?顺便说一句,这个广播接收器在清单中注册并且是过滤意图android.intent.action.PHONE_STATE。只要有来电,就会调用它,并且它是为了启动活动。

0 个答案:

没有答案