开发者

Titanium Appcelerator: How to set focus to the application window?

开发者 https://www.devze.com 2023-03-20 00:05 出处:网络
What 开发者_如何学编程is the Titanium method that shifts application focus to the Titanium app when an event occurs? For example, my application is constantly running in the background, and I want a w

What 开发者_如何学编程is the Titanium method that shifts application focus to the Titanium app when an event occurs? For example, my application is constantly running in the background, and I want a window to open when email arrives.


You would like the Ti App to open when you receive an email on the device?

As far as I know, this won't happen automatically (as I could see many developers abusing this).

However, what I suggest is, add a URI Scheme (a url link or button) in the Email something like: yourApp://view?id=abc which the user can click on, and it will open your app, and open the window/view/controller within your App. To do so, you will need to add URI schemes to your App, and handle the url, parse it, and so something useful with it in your App... here's how:

In the App's tiapp.xml, add this for iOS:

<dict>
    <key>CFBundleURLName</key>
    <!-- same as ti:app/id -->
    <string>your.app.id</string>
    <key>CFBundleURLSchemes</key>
       <array>
          <!-- your custom scheme -->
          <string>yourApp</string>
       </array>
 </dict> 

On Android in tiapp.xml in manifest node:

<application>
                <activity android:configChanges="keyboardHidden|orientation" android:label="Your App"
                          android:name=".MyAppActivity" android:theme="@style/Theme.Titanium"
                          android:launchMode="singleTask" >
                          <!-- add the above launchMode attribute -->
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN"/>
                        <category android:name="android.intent.category.LAUNCHER"/>
                    </intent-filter>
                    <!-- add the below additional intent-filter -->
                    <intent-filter>
                        <action android:name="android.intent.action.VIEW"/>
                        <category android:name="android.intent.category.DEFAULT"/>
                        <category android:name="android.intent.category.BROWSABLE"/>
                        <data android:scheme="yourApp" />
                    </intent-filter>
                </activity>
            </application>

In Alloy.js:

if (OS_ANDROID) {
    // Somehow, only in alloy.js we can get the data (URL) that opened the app
    Alloy.Globals.url = Ti.Android.currentActivity.intent.data;
}

In your Main app.js or index.js:

// We don't want our URL to do anything before our main window is open
$.index.addEventListener('open', function (e) {

    if (OS_IOS) {

        // Handle the URL in case it opened the app
        handleURL(Ti.App.getArguments().url);

        // Handle the URL in case it resumed the app
        Ti.App.addEventListener('resumed', function () {
            handleURL(Ti.App.getArguments().url);
        });

    } else if (OS_ANDROID) {

        // On Android, somehow the app always opens as new
        handleURL(Alloy.globals.url);
    }
});

var XCallbackURL = require('XCallbackURL');

function handleUrl(url) {
    var URL = XCallbackURL.parse(url),
        controller = URL.action(),
        args = URL.params();

    // Add some better logic here ;)
    Alloy.createController(controller, args || {}).getView().open();
}

You can also learn more in much more details here: http://fokkezb.nl/2013/08/26/url-schemes-for-ios-and-android-1/


win.addEventListener('focus',function() {
    // your code here
}

you can use setTimeOut() of javascript which will start another activity once your email popup or view or window does show up.


There are some issues with your example code:

  • You cannot handle incoming emails with your application (at least not in iOS). It is restricted by the iOS in order to ensure user privacy
  • The URL-schemes are used to handle links to your app (e.g. myapp:// from the browser or other apps)
  • In order to handle URL-schemes, use the handleurl event that is available in Titanium SDK 5.5.0.GA and later
  • Ensure that you remove event-listeners after leaving the current context, especially when you add event listeners in the open / focus event

If you follow that rules, you should be able to receive and handle URL's regarding your app, thanks!

0

精彩评论

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