2017-02-14 43 views
0

我有一种方法,从手机获取联系人,并将其发送到PHP服务器进行处理,然后它会返回数据,以便它将在SQL Lite DB中更新。我需要在后台连续运行此方法。我正在使用Volley进行网络操作。我在服务中使用Handler来运行此方法。问题是我看到太多的跳帧和应用程序是非常缓慢和stucks。我想在不干扰主线程的情况下运行此方法。服务代码在下面提到。最好的方式来运行服务内部的方法,而不会干扰主UI线程

public class serv extends Service { 

ArrayList<String> aa= new ArrayList<>(); 
ArrayList<String> bb= new ArrayList<>(); 
JSONObject JSONimdb; 
JSONObject EverythingJSON; 
ArrayList<mobstat> musers = new ArrayList<mobstat>(); 
private RequestQueue requestQueue; 
String l; 
private static Timer timer = new Timer(); 

@Override 
public void onTaskRemoved(Intent rootIntent) { 
    Log.e("Shiva","Service Killed"); 
} 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
//nonstop1(); 
threadcheck(); 
    return Service.START_STICKY; 
} 

private void _startService() 
{ 
    long UPDATE_INTERVAL = 5 * 1000; 
    timer.scheduleAtFixedRate(
    new TimerTask() 
    { 
     public void run() 
     { 
     try 
      { 
      getNumber(serv.this.getContentResolver()); 
      } catch (JSONException e) 
      { 
      e.printStackTrace(); 
      } 
      } 
      }, 1000, UPDATE_INTERVAL); 
} 

private void nonstop1() 
{ 
    final Handler handlera = new Handler(); 
    Runnable updatea = new Runnable() 
    { 
     @Override 
     public void run() 
     { 

      try { 
       getNumber(serv.this.getContentResolver()); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      handlera.postDelayed(this , 1000); 
     } 
    }; 
    handlera.postDelayed(updatea, 10); 
} 

private void threadcheck() 
{ 
    new Thread() { 
     public void run() { 
      try { 
       Log.e("Shiva","Threadcheck"); 
       getNumber(serv.this.getContentResolver()); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }.start(); 
} 
@Override 
public void onDestroy() 
{ 
    super.onDestroy(); 
    // Intent broadcatIntent = new Intent("com.statmob.findnum"); 
    //sendBroadcast(broadcatIntent); 
    //stoptimertask(); 
    //nonstop1(); 
    threadcheck(); 
} 



public void getNumber(ContentResolver cr) throws JSONException 
{ 
    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); 
    while (phones.moveToNext()) 
    { 
     String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
     String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
     if (phoneNumber.length()>=10) 
     { 
      l = phoneNumber.substring(phoneNumber.length()-10); 
      aa.add(l); 
      bb.add(name);} 
    } 
    phones.close(); 

    JSONimdb = new JSONObject(); 
    for (int i = 0; i < aa.size(); i++) 
    { 
     try 
     { 
     JSONimdb.put(bb.get(i), aa.get(i)); 
     } catch (JSONException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    EverythingJSON = new JSONObject(); 
    try 
     { 
     EverythingJSON.put("imdblist", JSONimdb); 
     } catch (JSONException e) 
     { 
      e.printStackTrace(); 
     } 

    StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://xxxxxxxx/cont.php", 
      new Response.Listener<String>() 
      { 
       @Override 
       public void onResponse(String s) 
       { 
        if (s != null) 
        { 
        parseJSONresponse(s); 
        } 
       } 
      }, 
      new Response.ErrorListener() 
      { 
       @Override 
       public void onErrorResponse(VolleyError error) 
       { 
        Log.e("Shiva",""+error); 
       } 
      }) { 
       @Override 
       protected Map<String, String> getParams() throws AuthFailureError 
       { 
        Map<String, String> params = new Hashtable<String, String>(); 
        params.put("arr", EverythingJSON.toString()); 
        return params; 
       } 
      }; 

    int socketTimeout = 50000; 
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, 
    DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); 
    if(requestQueue==null) 
    { 
     requestQueue = Volley.newRequestQueue(serv.this);} 
     stringRequest.setRetryPolicy(policy); 
     requestQueue.add(stringRequest); 
    } 

private void parseJSONresponse(String s) 
{ 
    try 
     { 
     JSONArray json = new JSONArray(s); 

     for (int i = 0; i < json.length(); i++) 
     { 
      JSONObject e = json.getJSONObject(i); 
      musers.add(new mobstat(e.getString("name"), e.getString("status"),e.getLong("time"))); 
      SugarRecord.updateInTx(musers); 

     } 
    } catch (JSONException e) 
     { 
     e.printStackTrace(); 
     } 
    } 
} 

我已经试过定时器&处理程序,这是工作正常,但它使应用程序缓慢,没有响应。线程不工作。请建议一些更好的方法在后台运行此方法,而不会对主线程造成任何干扰。

回答

0

创建单独的进程清单文件的服务可能这帮助您

<manifest> 
.... 
<application> 
..... 
<service android:name=".serv" android:process=":myprocess" > 
</application> 
</manifest> 
0

IntentServices修改您当前的实施。

默认情况下IntentServices在后台线程中运行。如果使用Services,则默认使用主线程。然后你需要运行一个Thread里面的服务来使该执行块在后台。

经过thisthis link以获取更多信息

+0

但方法getNumber(serv.this.getContentResolver());只运行一次,我无法启动intent服务中的nonstop()处理程序。 – user2269164

相关问题