【发布时间】:2011-07-14 03:26:41
【问题描述】:
当事件发生时,将应用程序焦点转移到 Titanium 应用程序的 Titanium 方法是什么?例如,我的应用程序一直在后台运行,我希望在电子邮件到达时打开一个窗口。
【问题讨论】:
-
Android。但是,很明显,对于下一个读者来说,两者都更好。
标签: titanium appcelerator
当事件发生时,将应用程序焦点转移到 Titanium 应用程序的 Titanium 方法是什么?例如,我的应用程序一直在后台运行,我希望在电子邮件到达时打开一个窗口。
【问题讨论】:
标签: titanium appcelerator
您希望在设备上收到电子邮件时打开 Ti App?
据我所知,这不会自动发生(我可以看到许多开发人员滥用它)。
但是,我建议在电子邮件中添加一个 URI 方案(一个 url 链接或按钮),例如:
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/
【讨论】:
win.addEventListener('focus',function() {
// your code here
}
您可以使用 setTimeOut() 的 javascript,一旦您的电子邮件弹出窗口或视图或窗口确实出现,它将启动另一个活动。
【讨论】:
您的示例代码存在一些问题:
myapp://)handleurl 事件open / focus 事件中添加事件侦听器时如果您遵守该规则,您应该能够接收和处理有关您的应用的 URL,谢谢!
【讨论】: