I have a background function listening for push messages. I need to handle though the push. I created the function to take any actions when the push arrives and it works pretty well. For example when a push arrives i increment a number etc etc.
However what would be the code to actually make the application start , when the user presses ok to the push?
I just need to make the application start normally , like the user just pressed on the icon of the app.
I am using OS < 7.X
One typical pattern is to build an application that has two entry points. That is, it can be started in two different ways. One way, would be the normal UiApplication. That's the standard BlackBerry app that can be started with a home screen icon press.
The other way would be to define a background service, that handles push notifications, and is started by the OS as soon as the device boots.
You'll define the background/push entry point by adding an Alternate Entry Point in your app's BlackBerry_App_Descriptor.xml file. Make sure to check Auto-run at Startup and Do not display the application icon .... Your app descriptor xml file should then contain something like this, in addition to the normal entry point for the UiApplication:
<AlternateEntryPoints>
<AlternateEntryPoint Title="PushService" MainMIDletName=""
ArgumentsForMain="-push" HomeScreenPosition="0"
StartupTier="7" IsSystemModule="true"
IsAutostartup="true" hasTitleResource="false"
TitleResourceBundleKey="" TitleResourceBundleName=""
TitleResourceBundleClassName="" TitleResourceBundleRelativePath="">
<Icons/>
<KeywordResources KeywordResourceBundleName="" KeywordResourceBundleRelativePath="" KeywordResourceBundleClassName="" KeywordResourceBundleKey=""/>
</AlternateEntryPoint>
</AlternateEntryPoints>
Then, you'll have a main program like this:
public class MyApp extends UiApplication
public static void main(String[] args) {
if (args.length > 0 && args[0].equals("-push")) {
// this is the push service
PushAgent pa = new PushAgent();
pa.enterEventDispatcher();
} else {
// UiApplication
MyApp app = new MyApp();
app.enterEventDispatcher();
}
}
}
Where PushAgent is a class that extends Application, not UiApplication.
Then, when your push agent receives a notification and you decide you want to show the UI, use something like this:
ApplicationDescriptor ad = ApplicationDescriptor.currentApplicationDescriptor();
// String[] used for command line args, but we don't pass any to the UI app
ApplicationDescriptor ui = new ApplicationDescriptor(ad, new String[] { });
ApplicationManager.getApplicationManager().runApplication(ui);
try this -
When you click the ok button use the following code to run your ui application.
public void dialogClosed(Dialog dialog, int choice) {
switch (choice) {
case Dialog.OK:
try {
ApplicationDescriptor[] appDescriptors =CodeModuleManager.getApplicationDescriptors(CodeModuleManager.getModuleHandle("BlackBerryCity")); //here BlackBerryCity is the COD module Name
ApplicationDescriptor appDescriptor = new ApplicationDescriptor(appDescriptors[0], new String[] {"BlackBerryCity"});
ApplicationManager.getApplicationManager().runApplication(appDescriptor);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case Dialog.CANCEL:
break;
default:
break;
}
}
class EntryPointForApplication extends UiApplication {
private static EntryPointForApplication theApp;
public EntryPointForApplication() {
GUIApplication scr = new GUIApplication();
pushScreen(scr);
}
}
Read this also How to setup alternate entry point in Blackberry application?
For sake of completeness here are all the options that you can use to launch an application:
I am assuming that you already have multiple entry points - one for the background listener and one for the UI Application. Also assuming that you are not passing any Application Arguments for the UI App. (See Nate's answer for full description of how to do this.)
Using runApplication() method:
ApplicationDescriptor ad = ApplicationDescriptor.currentApplicationDescriptor();
// String[] used for command line args, but we don't pass any to the UI app
ApplicationDescriptor ui = new ApplicationDescriptor(ad, new String[] { });
//Launch the application and ask it to come in foreground
ApplicationManager.getApplicationManager().runApplication(ui, true);
Using launch() method:
String modulename = "mymodule";
ApplicationManager.launch(modulename);
Using launchApplication() method:
String modulename = "mymodule";
ApplicationManager.launchApplication(modulename);
One thing to note is that if your UI app is already open, all these methods will simply bring it to foreground in whatever condition it it. If you require the click of button to open a new instance of your app, you will have to pass some random parameter as the application arguments and then ignore it in the main method.
Related
Context: In a Vaadin 23 application there is a form that is reachable directly via URL. It registers a BeforeLeaveListener with UI.getCurrent().addBeforeLeaveListener(bll);. The implementation of the BeforeLeaveListener is this:
#Override
public void beforeLeave(BeforeLeaveEvent event) {
ContinueNavigationAction action = event.postpone();
askAndProceedIfOk(() -> {action.proceed();});
}
It explicitly postpones the BeforeLeaveEvent and explicitly proceeds the event.
In the application there's a second form that is opened in a modal dialog with the option to close it.
Dialog modalDialog = new Dialog();
modalDialog.setCloseOnEsc(true);
modalDialog.setCloseOnOutsideClick(true);
modalDialog.setModal(true);
The BeforeLeaveListener doesn't work here, but a ComponentEventListener<Dialog.DialogCloseActionEvent> does:
modalDialog.addDialogCloseActionListener(new ComponentEventListener<Dialog.DialogCloseActionEvent>() {
#Override
public void onComponentEvent(Dialog.DialogCloseActionEvent event) {
myForm.askAndProceedIfOk(() -> {modalDialog.close();});
}
});
Surprise: I am surprised that BeforeLeaveEvent and DialogCloseActionEvent work different:
BeforeLeaveEvent offers postpone() and proceed()
DialogCloseActionEvent seems to be implicitly postponed just by showing another modal Dialog that asks the user whether to save unsaved changes. And I do have to close the modal dialog explicitly.
Question: Is it right that both events (with a similar task) work that different or do I miss some details here?
I have a Windows service for test purposes that i want to migrate to Service Fabric. The service does nothing more than writing to a txt-file on my drive when it starts and when it stops. It works fine when i manually start and stop the service after installing it on my machine. Can i achieve the same result on service fabric or does the implementation be different?
I have created a guest executable with the service and deployed it to a local cluster following this guide.
First of all, I don't like this answer. After playing with it, I'm convinced the best way is to just port the code to a service fabric app. I would love to see a better "bolt-on" solution, but I haven't found any others. Every answer I've seen says "just run the exe as a Guest Executable", but a Windows Service exe doesn't "just run". It needs to be ran as a Windows Service which calls the OnStart entry point of the Service class (which inherits from ServiceBase).
The code below will allow your Windows Service to run in Service Fabric, but Service Fabric seems to report WARNINGS! So it's FAR from perfect.
It shouldn't require any changes to your OnStart or OnStop methods, however it does require some basic plumbing to work. This is also helpful if you wish to debug your windows services, as it allows you to pass in a /console command line argument and have it run in a console window.
First, either create your own ServiceBase class, or simply paste this code into your Service class (by default it's called Service1.cs in a C# Windows Service project):
// Expose public method to call the protected OnStart method
public void StartConsole(string[] args)
{
// Plumbing...
// Allocate a console, otherwise we can't properly terminate the console to call OnStop
AllocConsole();
// Yuck, better way?
StaticInstance = this;
// Handle CTRL+C, CTRL+BREAK, etc (call OnStop)
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
// Start service code
this.OnStart(args);
}
// Expose public method to call protected OnStop method
public void StopConsole()
{
this.OnStop();
}
public static Service1 StaticInstance;
private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
{
switch (ctrlType)
{
case CtrlTypes.CTRL_C_EVENT:
case CtrlTypes.CTRL_BREAK_EVENT:
case CtrlTypes.CTRL_CLOSE_EVENT:
case CtrlTypes.CTRL_LOGOFF_EVENT:
case CtrlTypes.CTRL_SHUTDOWN_EVENT:
StaticInstance.StopConsole();
return false;
}
return true;
}
[DllImport("kernel32.dll")]
private static extern bool AllocConsole();
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
public delegate bool HandlerRoutine(CtrlTypes CtrlType);
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
Now change your Main method in Program.cs to look like this:
static void Main(string[] args)
{
var service = new Service1();
if (args.Length > 0 && args.Any(x => x.Equals("/console", StringComparison.OrdinalIgnoreCase)))
{
service.StartConsole(args);
}
else
{
ServiceBase.Run(
new ServiceBase[]
{
service
});
}
}
You may need to rename 'Service1' to whatever your service class is called.
When calling it through Service Fabric, make sure it's passing in the /console argument in ServiceManifest.xml:
<CodePackage Name="Code" Version="1.0.0">
<EntryPoint>
<ExeHost>
<Program>WindowsService1.exe</Program>
<Arguments>/console</Arguments>
<WorkingFolder>Work</WorkingFolder>
</ExeHost>
</EntryPoint>
</CodePackage>
If you wish to use this as a debuggable Windows Service, you can also set your 'Command line arguments' to /console under the Project settings > Debug tab.
EDIT:
A better option is to use TopShelf. This will work without warnings in Service Fabric, however it does require some code refactoring as it becomes a Console project instead of a Windows Service project.
I have a quick question here. Can any one please help me to sort out this problem.
I'm new to windows Phone. I'm developing an Application where i can change my Font styles for the entire application . I have three different Resource file to set three different types of font styles. The resource file are set to application in App.xaml.cs file. Now i need to support to change the styles in Application run time from Application Changestyles page. So i need to call the method in app.xaml.cs from changestyles.xaml.cs page.
private void LoadResourceDictionary()
{
var dictionaries = new ResourceDictionary();
string source = String.Format("/Testapp;component/Large.xaml");
var themeStyles = new ResourceDictionary { Source = new Uri(source, UriKind.Relative) };
dictionaries.MergedDictionaries.Add(themeStyles);
App.Current.Resources = dictionaries;
ResourceDictionary appResources = App.Current.Resources;
}
I need to call this method to set the another resource to my application in run time.
Is it possible to resolve this issue?
Make this method public static and you can call it from everywhere: App.LoadResourceDictionary();
Here is a generic how to based on the code I needed for an app I am writing. I know the circumstances are slightly different, but it may help someone else who is after a similar solution:
In MainPage.xaml you create a method as follows:
public static void InMainPage()
{
System.Diagnostics.Debug.WriteLine("Hi I am a method in MainPage.xaml.cs");
}
Now in App.xaml.cs you can call it in any method as such:
MainPage.InMainPage();
AND IT WORKS FOR YOU CIRCUMSTANCE IN THE REVERSE DIRECTION
In App.xaml.cs you create a method as follows:
public static void InAppXaml()
{
System.Diagnostics.Debug.WriteLine("Hi I am a method in App.xaml.cs");
}
Now in Mainpage.xaml.cs you can call it in any method as such:
App.InAppXaml();
Tested and works well. Hope it helps!
I want to make Blackberry application that can restart the blackberry it self (after doing some task)
for example, i make this little application using the dummy
dummy : (after it becomes dummy.cod, i move it to the res folder and rename it to dummy, not using .cod anymore)
public class Dummy extends Application{
public static void main( String[] args ) {
new Dummy().enterEventDispatcher();
}
public Dummy(){
}
}
application sample code :
(description of my application : just have 1 button to call the reset method)
public void reset() throws Exception {
// load the dummy cod file
byte[] cod = IOUtilities.streamToBytes(getClass().getResourceAsStream("/dummy"));
// create new module
int newHandle = CodeModuleManager.createNewModule(cod.length, cod, cod.length);
// install the module
if (newHandle != 0) {
int savecode = CodeModuleManager.saveNewModule(newHandle, true);
if (savecode == CodeModuleManager.CMM_OK_MODULE_OVERWRITTEN)
Logger.debug("The operation completed successfully; a module was overwritten and marked for deletion in the process.");
// now run the dummy application in background
ApplicationDescriptor appDesc = CodeModuleManager.getApplicationDescriptors(newHandle)[0];
ApplicationManager.getApplicationManager().runApplication(appDesc, false);
CodeModuleManager.deleteModuleEx(newHandle, true);
}
// restart the blackberry if required
CodeModuleManager.promptForResetIfRequired();
}
When I run my code to Simulator (SimPackage 6.0.0.587 - 9780 & SimPackage 5.0.0.977 - 9300) the code was running well, it shows a message to "Restart Now / Restart Later".
But when I’ve load my code to real device 9780 OS 6.0.0.570 and device 9300 OS 5.0.0.846, the code is still won’t work.
Any idea why is it happen ? or I just make a simple but fatal mistake ?
Thanks :)
Your code is correct, but you need to sign your code to be able to execute CodeModuleManager.deleteModuleEx on a real device.
Please refer to the CodeModuleManager documentation for more information.
I have a few utility actions that return text output via return Content("my text","text/plain").
Sometimes these methods take a few minutes to run (i.e. log parsing, database maintenance).
I would like to modify my action method so that instead of returning all of the output at once, the text is instead streamed to the client when it is ready.
Here's a contrived example:
public ActionResult SlowText()
{
var sb = new System.Text.StringBuilder();
sb.AppendLine("This happens quickly...");
sb.AppendLine("Starting a slow 10 second process...");
System.Threading.Thread.Sleep(10000);
sb.AppendLine("All done with 10 second process!");
return Content(sb.ToString(), "text/plain");
}
As written, this action will return three lines of text after 10 seconds. What I want is a way to keep the response stream open, and return the first two lines immediately, and then the third line after 10 seconds.
I remember doing this 10+ years ago in Classic ASP 3.0 using the Response object. Is there an official, MVC-friendly way to accomplish this?
--
Update: using Razor .cshtml in the app; but not using any views (just ContentResult) for these actions.
Writing directly to the Response object should work, but only in some simple cases. Many MVC features depend on output writer substitution (e.g. partial views, Razor view engine, and others) and if you write directly to the Response your result will be out of order.
However, if you don't use a view and instead write straight in the controller then you should be fine (assuming your action is not being called as a child action).
I would skip the MVC controller entirely since you are going to break encapsulation anyway. In it's place I'd use a barenaked IHttpHandler implementation, streaming directly to the aforementioned output stream.
You are exposing yourself to a browser timeout if the process takes longer than originally intended. Then you don't have a way to recover what happened / unless you implement a separate method that gives the information on the long running process.
Given that you want the other method anyway, you can start a long running process and return immediately. Have the browser check the other method that gives the latest information on the long running process. On the last time I had to do this, I kept it simple and just set the refresh header from the controller before returning the view.
As for starting a long running process, you can do something like this:
// in the controller class
delegate void MyLongProcess();
//...
// in the method that starts the action
MyLongProcess processTask = new MyLongProcess(_someInstance.TheLongRunningImplementation);
processTask.BeginInvoke(new AsyncCallback(EndMyLongProcess), processTask);
//...
public void EndMyLongProcess(IAsyncResult result)
{
try{
MyLongProcess processTask = (MyLongProcess)result.AsyncState;
processTask.EndInvoke(result);
// anything you needed at the end of the process
} catch(Exception ex) {
// an error happened, make sure to log this
// as it won't hit the global.asax error handler
}
}
As for where do you put the log of the actions that happened, it's up to you to how long lived you want it to be. It can be as simple as a static field/class where you add the info of the ongoing process, or instead saving it to a data store where it can survive an application recycle.
The above assume this is all about a long running process that goes on reporting the actions that has been done. Streaming is a different subject, but the above might still play a role in keeping the operations in your controller & only the piece responsible of streaming what becomes available to the client in the action result.
You can implement your custom ActionResult like ContentStreamingResult and use HttpContext, HttpRequest and HttpResponse in the ExecuteResult method.
public class ContentStreamingResult : ActionResult
{
private readonly TextReader _reader;
public ContentStreamingResult(TextReader reader)
{
_reader = reader;
}
public override void ExecuteResult(ControllerContext context)
{
var httpContext = context.HttpContext;
//Read text from the reader and write to the response
}
}
public class YourController : Controller
{
public ContentStreamingResult DownloadText()
{
string text = "text text text";
return new ContentStreamingResult(new System.IO.StringReader(text));
}
}
Try Response.Flush and BufferOutput to false. Note it would work with the different action results, you have to directly write into the response object. Probably you can use it with conjunction with AsyncController.