使用Android服务时出现内存不足错误

时间:2018-12-28 16:01:46

标签: android

我有一个Android应用程序,该服务器通过Volley从服务器获取一些帖子 我想在这种情况下每当有新帖子添加到服务器时都播放音乐,我将旧计数存储在共享的prefrence变量中,每隔7秒从服务器获取列表大小,然后与旧计数进行比较 如果它们不同,音乐会播放

一切正常,但是几个小时后,我得到了内存不足的错误:

STACK_TRACE=java.lang.OutOfMemoryError: thread creation failed
    at java.lang.VMThread.create(Native Method)
    at java.lang.Thread.start(Thread.java:1050)
    at com.android.volley.RequestQueue.start(RequestQueue.java:152)
    atcom.android.volley.toolbox.Volley.newRequestQueue(Volley.java:79)
    atcom.android.volley.toolbox.Volley.newRequestQueue(Volley.java:105)
    at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:115)
    at dalvik.system.NativeStart.main(Native Method)

这是我的互联网服务代码:

public class InternetService7 extends Service {
    StringRequest request;
    public int notifNumber;
    public int notifNumberOriginal;
    Context context1;

    Context c2;

    SharedPreferences shp;

    int number = 0;
    int preNumber = 0;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        context1 = this;

        shp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor shpE = shp.edit();

        if (!shp.contains("countNumber")) {
            shpE.putInt("countNumber", 10);
            shpE.apply();
        } else {

        }
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    newCount(context1);
                } catch (Exception e) {

                }
                handler.postDelayed(this, 7000);
            }

        }, 7000);
        return START_STICKY;
    }

    private void newCount(final Context context) {
        RequestQueue queue = Volley.newRequestQueue(context);

        String url_notf_number = ..

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url_notf_number, 
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        String r = response.substring(0, 14);
                        String number = r.replaceAll("\\D+", "");

                        notifNumber = Integer.parseInt(number);
                        notifNumberOriginal = notifNumber;
                        c2 = context;
                        context1 = context;
                        subCount1();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        stringRequest.setShouldCache(false);
        queue.add(stringRequest);
    }

    private void subCount1() {
        try {
            int number1 = (shp.getInt("lastNumber", 0));

            notifNumber = notifNumber - number1;

            if ((notifNumber != preNumber) && notifNumber > 0) {
                preNumber = notifNumber;

                boolean b = false;

                MediaPlayer ourSong = MediaPlayer.create(context1, R.raw.iphone);
                ourSong.start();
                try {
                    MainActivity.setMyy("reload");
                } catch (Exception e) {
                }
            } else {
            }
        } catch (OutOfMemoryError error) {
        } catch (Exception e) {
    }
}

我不知道我是否正确地完成了操作,我将不胜感激

0 个答案:

没有答案