【问题标题】:Android - How to make an activity to load faster?Android - 如何使活动加载更快?
【发布时间】:2011-05-26 09:22:32
【问题描述】:

我目前正在开发一个短信应用程序,它与股票短信应用程序非常相似。

但问题是我的应用加载速度比 stock(inbuilt) 应用慢得多。

这是我的应用程序代码:-

public class ThreadData extends ListActivity
{
private static final Uri SMS_URI = Uri.parse("content://sms");
ArrayList<String> bodies = new ArrayList<String>();
MySMSAdapter adapter;
String name;

public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(R.layout.threadlist);
    Bundle extras = getIntent().getExtras();
    if(extras != null)
    {
        name = extras.getString("address");
    }
    Cursor cursor = getContentResolver().query(SMS_URI, null, null, null, "date ASC");
    startManagingCursor(cursor);
    while (cursor.moveToNext())
    {
        String Id = cursor.getString(cursor.getColumnIndex("_id"));
        String address = cursor.getString(cursor.getColumnIndex("address"));
        String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
        String personName = getPersonName(Id,  address);
        if(personName.equals(name))
        {
            bodies.add(body);
        }
    }
    //adapter = new MySMSAdapter(this, bodies);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.sms_inbox, R.id.row, bodies);
    ListView listView = getListView();
    listView.setAdapter(adapter);
}
public String getPersonName(String id, String address)
{
        if(id==null)
        {
            if(address != null)
            {
                return PhoneNumberUtils.formatNumber(address);
            }
            else
            {
                return null;
            }
        }

        Cursor c = this.getContentResolver().query(Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, address),
                   new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

        if(c != null)
        {
            try
            {
                if(c.getCount() > 0)
                {
                    c.moveToFirst();
                    return c.getString(0);
                }
            }
            finally
            {
                c.close();
            }
        }
        if(address != null)
        {
            return PhoneNumberUtils.formatNumber(address);
        }
        return null;
}

}

我的自定义适配器:-

public class MySMSAdapter extends BaseAdapter
{
private Activity activity;
private ArrayList<String> data;
LayoutInflater inflater;

public MySMSAdapter(Activity a, ArrayList<String> arrayList)
{
    activity = a;
    data = arrayList;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount()
{
    return data.size();  //To change body of implemented methods use File | Settings | File Templates.
}

public Object getItem(int i)
{
    return data.get(i);  //To change body of implemented methods use File | Settings | File Templates.
}

public long getItemId(int i)
{
    return i;
}

public static class ViewHolder
{
    public TextView textView;
}

public View getView(final int i, View view, ViewGroup viewGroup)
{
    View row = view;
    final ViewHolder holder;
    if(view==null)
    {
        row = inflater.inflate(R.layout.sms_inbox, viewGroup, false);
        holder = new ViewHolder();
        holder.textView = (TextView)row.findViewById(R.id.row);
        row.setTag(holder);
    }
    else
    {
        holder = (ViewHolder)row.getTag();
    }

    holder.textView.setText(data.get(i));
    return row;
}
}

在我的课堂上,我尝试使用两个选项(自定义适配器以及 ArrayAdapter)加载 List,但在这两种情况下,我的应用程序加载速度都很慢。如何更快地加载它?我可能更喜欢我的自定义适配器的解决方案,因为将来我可能需要在我的布局中添加一些东西,比如照片和其他东西。

谢谢。请帮忙!!

【问题讨论】:

    标签: android android-activity load sms performance


    【解决方案1】:

    您可以使用http://developer.android.com/guide/developing/tools/traceview.html 分析器来定位性能问题。我猜问题是您正在同步执行磁盘操作(Cursor cursor = getContentResolver().query(...))。尝试使用 AssyncTask 进行此异步操作,即

    【讨论】:

    • 你能解释一下我应该把哪一部分代码放在 AsyncTask 中,因为我试过了,但它花费了相同的时间,但唯一的区别是 msgs 已被一一加载而不是一次全部。请多解释一下。 :(
    • 这部分Cursor cursor = getContentResolver().query(SMS_URI, null, null, null, "date ASC");开始管理光标(光标); while (cursor.moveToNext()) { String Id = cursor.getString(cursor.getColumnIndex("_id"));字符串地址 = cursor.getString(cursor.getColumnIndex("address")); String body = cursor.getString(cursor.getColumnIndexOrThrow("body")); String personName = getPersonName(Id, address); if(personName.equals(name)) { body.add(body); } }
    • 我尝试了 asynctask 和 traceview。首先我要感谢你,因为我学到了两个新东西 - Asynctask 以及如何使用 traceview 和一些 adb 的东西。我发现 getPersonName() 消耗了我应用程序总性能的 37%。你知道我可以用来获取联系人姓名的任何替代方法吗????这是我的最后一个问题,在此之后我不会打扰你。非常感谢你的帮助。 :)
    • 您可以尝试获取所有联系人并将它们放入您自己的数据结构中,然后再遍历消息。在 while 循环中查询数据库肯定很慢。
    猜你喜欢
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多