如何在Blackberry上推送带有计划时间的通知

时间:2011-09-09 11:49:15

标签: blackberry notifications

我想在每周的第一天推送诸如“Hello new week”之类的通知。

它每周都会自动运行。

我通过从Blackberry JDE插件导入此项目来引用NotificationsDemo。

public final class NotificationsDemo extends UiApplication
{    
    // com.rim.samples.device.notificationsdemo.NOTIFICATIONS_ID_1
    static final long NOTIFICATIONS_ID_1 = 0xdc5bf2f81374095L; 

    /**
     * Entry point for application.
     * @param args Command-line arguments
     */
    public static void main( String[] args )
    {
        if( args.length > 0 && args[ 0 ].equals( "autostartup" ) )
        {
            NotificationsDemo nd = new NotificationsDemo();
            nd.registerNotificationObjects();

            // Keep this instance around for rendering
            // Notification dialogs.
            nd.enterEventDispatcher(); 
        }
        else
        {
            // Start a new app instance for GUI operations.
            new NotificationsDemo().showGui();
        }
    }

    /**
     * Displays the NotificationDemoScreen.
     */
    private void showGui()
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        pushScreen( new NotificationsDemoScreen() );
        enterEventDispatcher();
    }

    /**
     * Registers this application as the notification manager.
     */
    private void registerNotificationObjects()
    {
        // A source is registered to tell the system that our application will
        // be sending notification events.  This will will cause a new user
        // editable configuration to be added to the Profiles application. 
        NotificationsManager.registerSource( NOTIFICATIONS_ID_1, new Object()
        {
            public String toString()
            {
                return "Notifications Demo";
            }
        }, NotificationsConstants.IMPORTANT );

        // Our NotificationsEngineListener implementation will display a dialog
        // to the user when a deferred event is triggered.
        NotificationsManager.registerNotificationsEngineListener( NOTIFICATIONS_ID_1,
                new NotificationsEngineListenerImpl( this ) );        

        // Our Consequence implementation will invoked whenever an immediate
        // event occurs.        
        NotificationsManager.registerConsequence(ConsequenceImpl.ID, new ConsequenceImpl());
    }

    /**
     * The MainScreen class for our UiApplication. 
     */
    private static class NotificationsDemoScreen extends MainScreen
    {
        private long _eventId;

        // Constructor
        private  NotificationsDemoScreen()
        {
            // Initialize UI components.            
            setTitle( "Notifications Demo" );
            add( new RichTextField( "Trigger notification from menu." ) );

             //A menu item to generate immediate and deferred events.
            MenuItem notifyItem = new MenuItem( new StringProvider( "Notify (ID1)" ), 0x230010, 0 );
            notifyItem.setCommand(new Command(new CommandHandler() 
            {
                /**
                 * @see net.rim.device.api.command.CommandHandler#execute(ReadOnlyCommandMetadata, Object)
                 */
                public void execute(ReadOnlyCommandMetadata metadata, Object context) 
                {
                    int trigger = NotificationsConstants.MANUAL_TRIGGER;

                    // The timeout parameter is IGNORED unless the TRIGGER
                    // is OUT_OF_HOLSTER_TRIGGER.
                    long timeout = -1;

                    Event e = new Event( NotificationsDemo.NOTIFICATIONS_ID_1, _eventId, 500, timeout,
                            trigger );
                    _eventId++;
                    e.fire();
                }
            }));
            addMenuItem( notifyItem );


        }
    }
}

按照我们使用菜单的代码打开LED,但我希望在没有菜单的情况下运行。我会自动跑。

如何正确设置时间以推送我的通知?

1 个答案:

答案 0 :(得分:0)

运行一个备用入口点,它将运行一个线程,该线程将检查每周的开头,然后使用此方法显示一个对话框:

private void showMessage(String data) {
  UiEngine ui = Ui.getUiEngine();
  Screen screen = new Dialog(Dialog.D_OK, data, Dialog.OK,
    Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION),
    Manager.VERTICAL_SCROLL);
  ui.queueStatus(screen, 1, true);
}