开发者

Android - How to make an activity to load faster?

开发者 https://www.devze.com 2023-03-08 07:13 出处:网络
I am currently working on an SMS app which will be quite similar to stock sms app. But the problem is my app is loading much slower than the stock(inbuilt) app.

I am currently working on an SMS app which will be quite similar to stock sms app.

But the problem is my app is loading much slower than the stock(inbuilt) app.

Here is my app's code :-

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);
开发者_JAVA百科            }
            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;
}

}

My custom adapter :-

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;
}
}

In my class i tried loading List with both the options(custom adapter as well as ArrayAdapter) But in both case my app is loading very slow. How to load it faster? I may prefer solution for my custom adapter as in future i may need few things to add in my layout like photos and other stuff.

Thanks. Please Help!!


You can use http://developer.android.com/guide/developing/tools/traceview.html profiler to localize performance issues. My guess the problem is you are doing disk operations (Cursor cursor = getContentResolver().query(...)) synchronously. Try to make this assync using AssyncTask i.e.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号