为什么android线程会自行启动

时间:2014-07-04 18:53:48

标签: android android-asynctask

我有一个NEW_OUTGOING_CALL意图(静态接收器)的接收器。为了不占用系统,我在AsyncTask中完成了整个过程的冗长部分。

根据拨打的号码,我可能会也可能不会启动AsyncTask(并继续进行常规处理)。然而,任务开始自己,正确的param通过,我无法弄清楚如何! 我已经对项目进行了深入研究,除了CallOneShot函数中的LongOperation之外,没有其他调用LongOperation - 但是围绕着' new'声明没有出现。

这怎么可能发生?

请找到附上的代码,抱歉长度,我试图将其删除一点

感谢您的帮助 学家

package com.iper.phoneeco;



public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "XXBroadcastReceiver";

FileWriter fDevLog;
MyPrefs myprefs=null;
public final static String EXTRA_MESSAGE = "com.iper.phoneeco.msg1";

   @Override
   public void onReceive(Context context, Intent intent) {

       if(intent.getAction().equalsIgnoreCase("android.intent.action.NEW_OUTGOING_CALL"))
       {
        Log.d(TAG,"OUTGOING CALL RECEIVED");
        String phoneNumber = getResultData();
           if (phoneNumber == null) {
             // No reformatted number, use the original
             phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
           }
           Log.d(TAG,"phone number:"+phoneNumber);


           if (IsToProcess(phoneNumber)) {

               Log.d (TAG,"Trapping the call");

              // Lets Roll
              CallOneShot(phoneNumber);

              // and prevent other apps from calling as well
              setResultData(null);
              // abortBroadcast();

          }
          else { 
              Log.d (TAG,"Standard processing");
              Toast.makeText(context, "standard processing"     , Toast.LENGTH_LONG).show();
          }

  Log.d (TAG,"Finished processing intent");

   }

    // 
    // check is number against a list of exceptions, that we dont handle
    //
    private boolean IsToProcess(String num){
        String[] excluded = {"15","17","18","112","911","991","08.*","^\\*.*","^#.*"};
        for (String ex : excluded){
             Log.d(TAG,"Exclusion test: "+ex + "versus: "+num);
            if (num.matches(ex)) {
                Log.d(TAG,"Exclusion FOUND: "+ex);
                return false;
            }
        }
        if (num.length() < myprefs.minLen) {
            Log.d(TAG,"Exclusion FOUND: Numero trop court");
                return false;
        }
        Log.d(TAG,"Exclusion not found: ");

        return true;
    }

    //
    // Displays a toast
    //
    void MyToast(String s, int col, int dur ) {
        Toast toast=Toast.makeText(myprefs.ctx, s, dur);
        toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.getView().setBackgroundColor(col );
        LinearLayout toastLayout = (LinearLayout) toast.getView();
        TextView toastTV = (TextView) toastLayout.getChildAt(0);
        toastTV.setTextSize(20);
        toast.show();

    }
    void MyToast(String s, int col) {
        MyToast(s,col,Toast.LENGTH_LONG);
    }

    public void CallOneShot(String phoneNumber) {
      Log.d (TAG,"CallOneShot");
      MyToast (myprefs.ctx.getResources().getString(R.string.callbackipg)+" "+phoneNumber,Color.BLUE);
      new LongOperation().execute(phoneNumber);

    }
    // 
    // the meat....
    //
    public class LongOperation extends AsyncTask<String, Void, String> {
        String numToCall;

        @Override
        protected String doInBackground(String... params) {         


            int bytesRead;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
            byte[] buffer = new byte[1024];
            String msgres;
            String response;

            Log.d(TAG, "Clientthread started");
            numToCall=params[0];
            Log.d(TAG, "numTocall"+numToCall);

            // and add to the call log

            ContentValues values = new ContentValues();
            values.put(CallLog.Calls.NUMBER, numToCall);
            values.put(CallLog.Calls.DATE, System.currentTimeMillis());
            values.put(CallLog.Calls.DURATION, 0);
            values.put(CallLog.Calls.TYPE, CallLog.Calls.OUTGOING_TYPE);
            values.put(CallLog.Calls.NEW, 1);
            values.put(CallLog.Calls.CACHED_NAME, ""); 
            values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
            values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
            Log.d(TAG, "Inserting call log placeholder for " + numToCall);
            ContentResolver resolver = myprefs.ctx.getContentResolver();
            resolver.insert(CallLog.Calls.CONTENT_URI, values);

            response=myprefs.ctx.getResources().getString(R.string.errundef);
            return response;
        }

        protected void onPostExecute (String s) {
            if (!s.equals("ok")) {
                Log.d(TAG,"OnPostExecute - failed: "+s);
                MyToast (myprefs.ctx.getResources().getString(R.string.errcallback)+"\n"+s,Color.RED);

            }

        }
    }


}

2 个答案:

答案 0 :(得分:0)

您是否已为myprefs分配值。您似乎已将其初始化为null并且从未将其分配给任何值

答案 1 :(得分:0)

好吧 - 愚蠢的我就是答案 - 我已经更改了包的名称,包的旧版本仍然在模拟器上,捕获了意图!一旦我将其取下,它就恢复了正常......

非常感谢你的帮助