如何在Gmail中获取在线好友列表?

时间:2012-05-23 06:58:12

标签: android

我想在gmail中获取在线好友列表, 我登录,也可以与朋友的电子邮件ID聊天。但没有获得列表。 感谢

import android.app.ListActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.indianic.frdinfo.FriendInfo;
import com.indianic.frdinfo.STATUS;
import com.indianic.interfac.IAppManager;
import com.indianic.service.IMService;

public class FriendList extends ListActivity 
{
    private static final int ADD_NEW_FRIEND_ID = Menu.FIRST;
    private static final int EXIT_APP_ID = Menu.FIRST + 1;
    private IAppManager imService = null;
    private FriendListAdapter friendAdapter;

    private class FriendListAdapter extends BaseAdapter 
    {       
        class ViewHolder {
            TextView text;
            ImageView icon;
        }
        private LayoutInflater mInflater;
        private Bitmap mOnlineIcon;
        private Bitmap mOfflineIcon;        

        private FriendInfo[] friends = null;


        public FriendListAdapter(Context context) {
            super();            

            mInflater = LayoutInflater.from(context);

            mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
            mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);

        }

        public void setFriendList(FriendInfo[] friends)
        {
            this.friends = friends;
        }


        public int getCount() {     

            return friends.length;
        }

        public FriendInfo getItem(int position) {           

            return friends[position];
        }

        public long getItemId(int position) {

            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) 
            {
                convertView = mInflater.inflate(R.layout.friend_list_screen, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);                                       

                convertView.setTag(holder);
            }   
            else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            holder.text.setText(friends[position].userName);
            holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);

            return convertView;
        }

    }

    public class MessageReceiver extends  BroadcastReceiver  {

        @Override
        public void onReceive(Context context, Intent intent) {

            Log.i("Broadcast receiver ", "received a message");
            Bundle extra = intent.getExtras();
            if (extra != null)
            {
                String action = intent.getAction();
                if (action.equals(IMService.FRIEND_LIST_UPDATED))
                {
                    // taking friend List from broadcast
                    //String rawFriendList = extra.getString(FriendInfo.FRIEND_LIST);
                    //FriendList.this.parseFriendInfo(rawFriendList);
                    FriendList.this.updateData(FriendController.getFriendsInfo(), 
                                                FriendController.getUnapprovedFriendsInfo());

                }
            }
        }

    };
    public MessageReceiver messageReceiver = new MessageReceiver();

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {          
            imService = ((IMService.IMBinder)service).getService();      

            FriendInfo[] friends = FriendController.getFriendsInfo(); //imService.getLastRawFriendList();
            if (friends != null) {              
                FriendList.this.updateData(friends, null); // parseFriendInfo(friendList);
            }    

            setTitle(imService.getUsername() + "'s friend list");
        }
        public void onServiceDisconnected(ComponentName className) {          
            imService = null;
            Toast.makeText(FriendList.this, R.string.local_service_stopped,
                    Toast.LENGTH_SHORT).show();
        }
    };



    protected void onCreate(Bundle savedInstanceState) 
    {       
        super.onCreate(savedInstanceState);

        setContentView(R.layout.list_screen);

        friendAdapter = new FriendListAdapter(this);




    }
    public void updateData(FriendInfo[] friends, FriendInfo[] unApprovedFriends)
    {
        if (friends != null) {
            friendAdapter.setFriendList(friends);   
            setListAdapter(friendAdapter);              
        }               

        if (unApprovedFriends != null) 
        {
            NotificationManager NM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            if (unApprovedFriends.length > 0)
            {                   
                String tmp = new String();
                for (int j = 0; j < unApprovedFriends.length; j++) {
                    tmp = tmp.concat(unApprovedFriends[j].userName).concat(",");            
                }
                Notification notification = new Notification(R.drawable.stat_sample, 
                        getText(R.string.new_friend_request_exist),
                        System.currentTimeMillis());

                //Intent i = new Intent(this, UnApprovedFriendList.class);
            //  i.putExtra(FriendInfo.FRIEND_LIST, tmp);                

            //  PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            //          i, 0);

            //  notification.setLatestEventInfo(this, getText(R.string.new_friend_request_exist),
            //                                  "You have new friend request(s)", 
            //                                  contentIntent);


                NM.notify(R.string.new_friend_request_exist, notification);         
            }
            else
            {
                // if any request exists, then cancel it
                NM.cancel(R.string.new_friend_request_exist);           
            }
        }

    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);      

        Intent i = new Intent(this, Messaging.class);
        FriendInfo friend = friendAdapter.getItem(position);
        if (friend.status == STATUS.ONLINE)     
        {
            i.putExtra(FriendInfo.USERNAME, friend.userName);
            i.putExtra(FriendInfo.PORT, friend.port);
            i.putExtra(FriendInfo.IP, friend.ip);       
            startActivity(i);
        }
        else 
        {           
            Toast.makeText(FriendList.this, R.string.user_offline, Toast.LENGTH_SHORT).show();
        }
    }




    @Override
    protected void onPause() 
    {
        unregisterReceiver(messageReceiver);        
        unbindService(mConnection);
        super.onPause();
    }

    @Override
    protected void onResume() 
    {

        super.onResume();
        bindService(new Intent(FriendList.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);

        IntentFilter i = new IntentFilter();
        //i.addAction(IMService.TAKE_MESSAGE);  
        i.addAction(IMService.FRIEND_LIST_UPDATED);

        registerReceiver(messageReceiver, i);           


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {     
        boolean result = super.onCreateOptionsMenu(menu);       

        menu.add(0, ADD_NEW_FRIEND_ID, 0, R.string.add_new_friend);

        menu.add(0, EXIT_APP_ID, 0, R.string.exit_application);     

        return result;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) 
    {       

        switch(item.getItemId()) 
        {     
            case ADD_NEW_FRIEND_ID:
            {
            //  Intent i = new Intent(FriendList.this, AddFriend.class);
            //  startActivity(i);
                return true;
            }       
            case EXIT_APP_ID:
            {
                imService.exit();
                finish();
                return true;
            }           
        }

        return super.onMenuItemSelected(featureId, item);       
    }   

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);




    }
}

2 个答案:

答案 0 :(得分:1)

使用Smack(XMPP)协议。

快速教程here 在android中实现XMPP的好tutorial

答案 1 :(得分:1)

  Roster roster = XMPPConnection.getRoster();
        Collection<RosterEntry> entries= roster.getEntries();
        ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp",
                new VCardProvider());
        VCard card = null;
        for (RosterEntry entry : entries) {
            card = new VCard();
            Presence presencek= roster.getPresence(entry.getUser());
            try {
                card.load(Main.conn, entry.getUser());
            } catch (Exception e) {
                e.printStackTrace();
            }

            String jid = entry.getUser();
             String name = card.getField("FN");
            String status = presencek.getType().name();
            Log.d("Prescence", "" + presencek.getType().name());// //num one log

            byte[] imgs = card.getAvatar();
            if (imgs != null) {
                int len = imgs.length;
                Bitmap img = BitmapFactory.decodeByteArray(imgs, 0, len);
            }