我正在编写一个应用程序,它必须在较长时间内以高cpu使用率运行
该 *** 作就像服务一样开始
但由于高cpu使用率,android系统会杀死应用程序
那么我该怎么做才能减少cpu的使用量呢?
并使系统不停止我的服务?
服务:
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(); } }}
解决方法:
您可以尝试将 *** 作分成更小的部分,并将Thread.sleep()放置一小段时间.如果这还不够,请尝试将一些代码更改为循环,如下所示:
旧代码:
object.move(1000);
新代码:
for (int i=0; i<100; i++) { object.move(10); Thread.sleep(10);}
如果示例move() *** 作在给定参数较高时花费更多时间,则旧代码可能导致程序无响应.新代码将允许androID在Thread.sleep()期间与您的程序通信,因此您的应用程序不会停止工作.
@编辑
正如我在你的代码中看到的那样 – 你确实有几个while循环.尝试将Thread.sleep(10)放在其中.
以上是内存溢出为你收集整理的Android App高CPU使用率全部内容,希望文章能够帮你解决Android App高CPU使用率所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)