我有一个方法从电话中取出联系人并将其发送到php服务器进行处理然后它将返回数据,以便它将在sql lite DB中更新。我需要在后台持续运行此方法。我正在使用Volley进行网络操作。我正在使用Handler内部服务来运行此方法。问题是我看到太多的跳帧和应用程序非常缓慢和卡住。我想在不干扰主线程的情况下运行此方法。服务代码如下所述。
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();
}
}
}
我试过Timer&amp;处理程序,这些工作正常,但它使应用程序缓慢,没有响应。线程无效。请建议一些更好的方法在后台运行此方法,不会对主线程造成任何干扰。
答案 0 :(得分:0)
在清单文件中为您的服务创建单独的流程可能会对您有所帮助
<manifest>
....
<application>
.....
<service android:name=".serv" android:process=":myprocess" >
</application>
</manifest>
答案 1 :(得分:0)
你试过IntentService吗?这是运行耗时的后台任务的简便方法。
答案 2 :(得分:0)