我有一種方法,從手機獲取聯繫人,並將其發送到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();
}
}
}
我已經試過定時器&處理程序,這是工作正常,但它使應用程序緩慢,沒有響應。線程不工作。請建議一些更好的方法在後臺運行此方法,而不會對主線程造成任何干擾。
但方法getNumber(serv.this.getContentResolver());只運行一次,我無法啓動intent服務中的nonstop()處理程序。 – user2269164