现在的位置: 首页 > 综合 > 正文

晒个昨天上午在家写的小程序吧 — 给黑莓内置程序”Task”添加菜单

2014年04月03日 ⁄ 综合 ⁄ 共 7413字 ⁄ 字号 评论关闭

目标:

1.程序随开机自动启动,给Task添加一个菜单Assign To

2.点击Assign To,从地址簿里面选人

3.对话框提示是否Assign给这个人

4.下一步:通过Web Service访问公司内外的Domino服务器,把这个Task assign给同事。(正在做)

 

 

package lab;
/*
.创建报告
点击assign按钮,如果发现没有保存(只有uid属性),就提示需要保存
   选人进行assign
  提示这个项目已经assgin给谁了,失败;或者是否继续assgin
   提示assign成功
  提示assign失败
           */
import java.util.Date;

import javax.microedition.pim.Contact;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;
import javax.microedition.pim.PIMItem;

import net.rim.blackberry.api.menuitem.ApplicationMenuItem;
import net.rim.blackberry.api.menuitem.ApplicationMenuItemRepository;
import net.rim.blackberry.api.pdap.BlackBerryContact;
import net.rim.blackberry.api.pdap.BlackBerryContactList;
import net.rim.blackberry.api.pdap.BlackBerryToDo;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class BBIntegration extends UiApplication
{
    public static void main(String args[])
    {
     if ((args.length > 0) && (args[0].equals("autostartup"))) {
      new BBIntegration();         
        } else
      new BBIntegration();
    }   
   
    public BBIntegration()
    {
        //Create a new instance of MyMenuItem passing in 0 as the order.
        MyMenuItem myMenuitem = new MyMenuItem(0);
        //Add MyMenuITem to the message list.
        ApplicationMenuItemRepository.getInstance().addMenuItem(ApplicationMenuItemRepository.MENUITEM_TASK_EDIT, myMenuitem);
    }
   
   
    static class MyMenuItem extends ApplicationMenuItem
    {
        //The toString method should return the string we want to use as the label
        //of the MenuItem.  Let's use "Run Browser".
        public String toString()
        {
            return "Assign to";
        }
   
        //Use the default constructor here.
        MyMenuItem(int order)
        {
            super(order);
        }
   
        //Methods we must implement.
        //The run method is called when the menuItem is invoked.
        public Object run(Object context)
        {
         String className = context.getClass().toString();
         if (className.equalsIgnoreCase("class net.rim.blackberry.api.pdap.ToDoImpl")) {  
          //alert(className);
          //test(context);
          //if task not saved, do nothing
          if (getTaskSummary(context) == null) {
           Dialog.alert("Please save task before assigning task.");
           return context;
          }
           
          try {
                 //choose from contact list
           BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST,PIM.READ_WRITE);
           BlackBerryContact contact = (BlackBerryContact) contactList.choose();
           
           //do you want to assign
           if(contact != null) {
            String question = "Do you want to assign this task to " + getDisplayName(contact) + "(" + getEmail(contact) + ")?";
            int answer = Dialog.ask(Dialog.D_YES_NO, question, Dialog.YES);
             if(answer == Dialog.YES){
              Dialog.inform("Task /"" + getTaskSummary(context) + "/" assigned to " + getDisplayName(contact) + " by device " + getPIN());;
             }else {
              Dialog.alert("Task /"" + getTaskSummary(context) + "/" not assigned.");
             }
            //alert("Username is :" + getDisplayName(contact));
            //alert("email is :" + getEmail(contact));
           } else {
            Dialog.alert("Contact not choosen, did not assign this task to anyone.");
           }
          } catch (PIMException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
          }
               
         }
           
            //Return context.
            return context;
        }
    }
   
    public static void alert(final String msg) {
        UiApplication.getUiApplication().invokeLater(new Runnable()
        {
            public void run()
            {
                Dialog.alert(msg);
            }
        });
    }
   
    public static String getDisplayName(Contact contact)
    {
        if(contact == null)
        {
            return null;
        }
   
        String displayName = null;
   
        // See if there is a meaningful name set for the contact
        if(contact.countValues(Contact.NAME) > 0)
        {
            final String[] name = contact.getStringArray(Contact.NAME, 0);
            final String firstName = name[Contact.NAME_GIVEN];
            final String lastName = name[Contact.NAME_FAMILY];
           
            if(firstName != null && lastName != null)
            {
                //displayName = firstName + " " + lastName;
             displayName = lastName + " " + firstName;
            }
            else if(firstName != null)
            {
                displayName = firstName;
            }
            else if(lastName != null)
            {
                displayName = lastName;
            }
   
            //加上Title
            if(displayName != null)
            {
                final String namePrefix = name[Contact.NAME_PREFIX];
                if (namePrefix != null)
                {
                    displayName = namePrefix + "(" + displayName + ")";
                }
                return displayName;
            }
        }
   
        // If no meaningful name is set, use the company name
        if(contact.countValues(Contact.ORG) > 0)
        {
            final String companyName = contact.getString(Contact.ORG, 0);
            if(companyName != null)
            {
                return companyName;
            }
        }
        return displayName;   
    }
   
    public static String getEmail(Contact contact)
    {
        if(contact == null)
        {
            return null;
        }
   
        String email = null;
   
        if(contact.countValues(Contact.EMAIL) > 0)
        {
            email = contact.getString(Contact.EMAIL, 0);
            if(email != null)
            {
                return email;
            }
        }
        return email;   
    }
   
    public static String getPIN() {
  String pin = Integer.toHexString(DeviceInfo.getDeviceId()).toUpperCase();
  //alert("Device PIN number is " + pin);
  return pin;
    }
   
    public static String getTaskSummary(Object context) {
  BlackBerryToDo task = (BlackBerryToDo)context;

  int[] fieldIds = task.getFields();
  int id;
  for(int index = 0; index < fieldIds.length; ++index)
  {
       id = fieldIds[index];
       if(task.getPIMList().getFieldDataType(id) == PIMItem.STRING)
       {
          for(int j=0; j < task.countValues(id); ++j)
          {
             String value = task.getString(id, j);
             try {
              if (task.getPIMList().getFieldLabel(id).equals("Summary")) return value;
              if (task.getPIMList().getFieldLabel(id).equals("摘要")) return value;
             } catch(Exception e) {
             }
          }
       }
  }
  return null;
    }

    public static void test(Object context) {
  BlackBerryToDo task = (BlackBerryToDo)context;
  //http://docs.blackberry.com/en/developers/deliverables/15665/Retrieve_task_information_565527_11.jsp
  //http://docs.blackberry.com/en/developers/deliverables/11935/Retrieve_task_information_565527_11.jsp
  //测试结果发现,如果不保存task,是读不到数据的,只能读一个UID属性(唯一的键值) UID,Summary, note

  int[] fieldIds = task.getFields();
     int id;
     for(int index = 0; index < fieldIds.length; ++index)
     {
        id = fieldIds[index];
        if(task.getPIMList().getFieldDataType(id) == PIMItem.STRING)
        {
           for(int j=0; j < task.countValues(id); ++j)
           {
              String value = task.getString(id, j);
              try {
               alert(task.getPIMList().getFieldLabel(id) + "=" + value);
              } catch(Exception e) {
               
              }
           }
        }
        if(task.getPIMList().getFieldDataType(id) == PIMItem.DATE)
        {
           for(int j=0; j < task.countValues(id); ++j)
           {
              long value = task.getDate(id, j);
              Date date = new Date(value);
              try {
               alert(task.getPIMList().getFieldLabel(id) + "=" + date);
              } catch(Exception e) {
               
              }
           }
        }
     }
    }
}

抱歉!评论已关闭.