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

时间:2011-07-14 03:26:41

标签: titanium appcelerator

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

3 个答案:

答案 0 :(得分:1)

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

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

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

在App的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> 

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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

如果您遵守该规则,您应该能够接收和处理有关您的应用的网址,谢谢!