Titanium Appcelerator:如何将焦点设置到应用程序窗口?

发布于 2024-11-23 18:43:23 字数 89 浏览 0 评论 0原文

当事件发生时将应用程序焦点转移到 Titanium 应用程序的 Titanium 方法是什么?例如,我的应用程序不断在后台运行,我希望在电子邮件到达时打开一个窗口。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍暮颜 2024-11-30 18:43:23

您希望在设备上收到电子邮件时打开 Ti 应用程序吗?

据我所知,这不会自动发生(因为我可以看到许多开发人员滥用这一点)。

然而,我的建议是,在电子邮件中添加一个 URI 方案(url 链接或按钮),如下所示:
yourApp://view?id=abc 用户可以点击它,它将打开您的应用程序,并打开您应用程序中的窗口/视图/控制器。为此,您需要将 URI 方案添加到您的应用程序,并处理 url、解析它,以及在您的应用程序中使用它的一些有用的东西...具体方法如下:

在应用程序的 tiapp.xml 中,为 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> 

在 Android 上,清单节点中的 tiapp.xml 中:

<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>

在 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;
}

在主 app.js 或 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();
}

您还可以在此处了解更多详细信息: http://fokkezb.nl/2013/08/ 26/url-schemes-for-ios-and-android-1/

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/

勿挽旧人 2024-11-30 18:43:23
win.addEventListener('focus',function() {
    // your code here
}

您可以使用 javascript 的 setTimeOut() ,一旦您的电子邮件弹出窗口、视图或窗口出现,它将启动另一个活动。

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.

辞慾 2024-11-30 18:43:23

您的示例代码存在一些问题:

  • 您无法使用应用程序处理传入电子邮件(至少在 iOS 中不能)。它受到 iOS 的限制,以确保用户隐私
  • URL 方案用于处理指向您的应用程序的链接(例如来自浏览器或其他应用程序的 myapp://
  • 为了处理 URL-方案,使用 handleurl 事件在 Titanium SDK 5.5.0.GA 及更高版本中提供
  • 确保在离开当前上下文后删除事件侦听器,尤其是在添加事件侦听器时open / focus 事件

如果您遵循该规则,您应该能够接收和处理有关您的应用程序的 URL,谢谢!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文