Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:34:39+00:00 2026-05-31T02:34:39+00:00

I am programming a app that has to do a operation with high cpu

  • 0

I am programming a app that has to do a operation with high cpu usage on a longer time

the operation is startet like a service
but the android system kills the app because of the high cpu usage

so what can I do to reduce the cpu usage ?
and make the system not to stop my service ?

service :

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.IBinder;
import android.provider.ContactsContract;

public class backup_service  extends Service {
    int i;
    int i2;


    @Override
    public void onCreate() {
        try
        {
        FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/dialog.db");
        BufferedWriter out = new BufferedWriter(fstream);
        out.write("on");
        //Close the output stream
        out.close();
        backup();
        }
        catch (Exception x)
        {
            stopSelf();
        }
    }

    @Override
    public void onDestroy() {
        util.deleteDir(new File("/data/data/contact.backup.alexander.fuchs/dialog.db"));
        stopSelf();
    }



    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public void backup()
    {
        util.deleteDir(new File("/data/data/contact.backup.alexander.fuchs/backup/"));
        new File("/data/data/contact.backup.alexander.fuchs/backup/").mkdirs(); 



        // get it 
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);


        if (cur.getCount() > 0) {
            i = 0;
            i2 = 0;
        while (cur.moveToNext()) {
             String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


        // write
        try
        {
            new File("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)).mkdirs(); 

            FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/id.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(id);
            //Close the output stream
            out.close();


            fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/name.txt");
            out = new BufferedWriter(fstream);
            out.write(name);
            //Close the output stream
            out.close();





        }
        catch (Exception x)
        {

        }
        if (Integer.parseInt(cur.getString(
                cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
             Cursor pCur = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
            null, 
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
            new String[]{id}, null);





        while (pCur.moveToNext()) {

            String number = pCur.getString(
                    pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            try
            {
            new File("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)).mkdirs(); 

            FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/number.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(number);
            //Close the output stream
            out.close();


            }
            catch(Exception x)
            {

            }
            }




    pCur.close();

    Cursor emailCur = cr.query( 
            ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
            null,
            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
            new String[]{id}, null); 
        while (emailCur.moveToNext()) { 
            // This would allow you get several email addresses
                // if the email addresses were stored in an array
            String email = emailCur.getString(
                          emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            String emailType = emailCur.getString(
                          emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
           try
        {
        new File("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)).mkdirs(); 

            FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/email.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(email);
            //Close the output stream
            out.close();

            fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/email_type.txt");
            out = new BufferedWriter(fstream);
            out.write(emailType);
            //Close the output stream
            out.close();


        }
        catch(Exception x)
        {

        }

        } 
        emailCur.close();

        String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
        String[] noteWhereParams = new String[]{id, 
        ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; 
                Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null); 
    if (noteCur.moveToFirst()) { 
        String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));

    // write
            try
            {
                new File("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)).mkdirs(); 

                FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/note.txt");
                BufferedWriter out = new BufferedWriter(fstream);
                out.write(note);
                //Close the output stream
                out.close();



            }
            catch (Exception x)
            {

            }
    } 
    noteCur.close();



    String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] addrWhereParams = new String[]{id, 
        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE}; 
    Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, 
                null, null, null, null); 
    while(addrCur.moveToNext()) {
        String poBox = addrCur.getString(
                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
        String street = addrCur.getString(
                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
        String city = addrCur.getString(
                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
        String state = addrCur.getString(
                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
        String postalCode = addrCur.getString(
                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
        String country = addrCur.getString(
                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
        String type = addrCur.getString(
                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
    // write
            try
            {
                new File("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)).mkdirs(); 

                FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/pobox.txt");
                BufferedWriter out = new BufferedWriter(fstream);
                out.write(poBox);
                //Close the output stream
                out.close();

                fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/street.txt");
                out = new BufferedWriter(fstream);
                out.write(street);
                //Close the output stream
                out.close();

                fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/city.txt");
                out = new BufferedWriter(fstream);
                out.write(city);
                //Close the output stream
                out.close();

                fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/state.txt");
                out = new BufferedWriter(fstream);
                out.write(state);
                //Close the output stream
                out.close();

                fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/postalcode.txt");
                out = new BufferedWriter(fstream);
                out.write(postalCode);
                //Close the output stream
                out.close();

                fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/country.txt");
                out = new BufferedWriter(fstream);
                out.write(country);
                //Close the output stream
                out.close();

                fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"addres_type.txt");
                out = new BufferedWriter(fstream);
                out.write(type);
                //Close the output stream
                out.close();

            }
            catch (Exception x)
            {

            }

    } 
    addrCur.close();

    String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] imWhereParams = new String[]{id, 
        ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE}; 
    Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI, 
            null, imWhere, imWhereParams, null); 
    if (imCur.moveToFirst()) { 
        String imName = imCur.getString(
                 imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
        String imType;
        imType = imCur.getString(
                 imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));

        //write
       try
        {
        new File("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)).mkdirs(); 

            FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/im.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(imName);
            //Close the output stream
            out.close();

            fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/im_type.txt");
            out = new BufferedWriter(fstream);
            out.write(imType);
            //Close the output stream
            out.close();


        }
        catch(Exception x)
        {

        }

    } 
    imCur.close();


    String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] orgWhereParams = new String[]{id, 
        ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; 
    Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI, 
                null, orgWhere, orgWhereParams, null);
    if (orgCur.moveToFirst()) { 
        String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
        String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
        String company = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
        String department = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DEPARTMENT));

         //write
       try
        {
        new File("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)).mkdirs(); 

            FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/orgName.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(orgName);
            //Close the output stream
            out.close();

            fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/title.txt");
            out = new BufferedWriter(fstream);
            out.write(title);
            //Close the output stream
            out.close();

            fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/company.txt");
            out = new BufferedWriter(fstream);
            out.write(company);
            //Close the output stream
            out.close();

            fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/department.txt");
            out = new BufferedWriter(fstream);
            out.write(department);
            //Close the output stream
            out.close();

        }
        catch(Exception x)
        {

        }
    } 
    orgCur.close();
        }
        i++;
        }
        onDestroy();


    }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:34:40+00:00Added an answer on May 31, 2026 at 2:34 am

    You can try divide the operation into smaller pieces and place Thread.sleep() for a short time. If that’s not enough try changing some code into loop like this:

    Old code:

    object.move(1000);
    

    New code:

    for (int i=0; i<100; i++) {
        object.move(10);
        Thread.sleep(10);
    }
    

    If the example move() operation takes more time if the given parameter is higher, the old code can cause your program non-responding. The new code will allow android to comunicate with your program during Thread.sleep(), so your app shoud not stop working.

    @edit
    As I can see in your code – you do have several while loops. Try placing Thread.sleep(10) inside them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Android App high cpu-usage I am programming a app that has to
I'm developing an iPhone (and later Android) app that has real-time features, i.e. when
I am new to iPhone programming and I am programming a app that has
I'm practicing my programming skills on an app that has a pivot page with
I've got an app that has a table view that displays contact information in
I have a general old-fashioned phone that has no special app system and uses
This is slightly off topic of programming but still has to do with my
Created a WCF service that is being called by a Silverlight app. Both reside
So, I'm building an iOS4 app that has a login screen and has to
Now that Apple has changed their in-app subscription model, there is a feature that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.