关闭后自动重启 nativescript 8 angular 应用程序 - 退出或完成 - 该应用程序更改应用程序语言
RESTART automatically a nativescript 8 angular app after close -exit or finish - that app to change the app language
我正在我的 nativescript 8 angular 应用程序中实现 EddyVerbruggen/nativescript-localize 插件,我想知道是否有办法在 [=19] 退出后自动重启应用程序=] 或 android 中的结尾用于更改应用程序语言。
更准确地说,在 NS 官方文档 (https://docs.nativescript.org/plugins/localize.html#how-to-change-the-language-dynamically-at-runtime ) 中,我找到了这段代码(请参阅下面的警告...)我希望完成 ?????? (请参阅下面的代码)使用一种方法(如果可能的话)在关闭后重新启动应用程序。
import { Application } from '@nativescript/core'
import { overrideLocale } from '@nativescript/localize'
alert({
title: 'Switch Language',
message: 'The application needs to be restarted to change language',
okButtonText: 'Quit!'
}).then(() => {
L.localize.overrideLocale(selectedLang)
if (isAndroid) {
;(
Application.android.foregroundActivity || Application.android.startActivity
).finish()
// Here I need a method to RESTART the app automatically after finsih
?????
} else {
exit(0)
// Here I need a method to RESTART the app automatically after exit
?????
}
})
要在 nativescript android 中重启应用程序,请使用:
if (isAndroid) {
const Intent = android.content.Intent;
type Intent = android.content.Intent;
const IntentCompat = androidx.core.content.IntentCompat;
type IntentCompat = androidx.core.content.IntentCompat;
const ctx: android.content.Context = application.android.context;
const mainIntent: Intent = IntentCompat.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_LAUNCHER);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.getApplicationContext().startActivity(mainIntent);
(
Application.android.foregroundActivity ||
Application.android.startActivity
).finish();
}
对于iOS,您似乎无法重新启动应用程序,Apple 的建议甚至是不要使用exit()
以编程方式退出应用程序,您可以阅读here:
Warning: Do not call the exit function. Applications calling exit will
appear to the user to have crashed, rather than performing a graceful
termination and animating back to the Home screen.
但是,如果您仍想调用退出函数,则有重新启动应用程序的解决方法,例如安排在应用程序退出后显示本地通知(如 here). To do this using nativescript you can use NativeScript Local Notifications Plugin 使用以下代码所示:
import {
LocalNotifications,
NotificationAction
} from 'nativescript-local-notifications';
// inside your class:
private scheduleNotification() {
var scheduledTime = new Date();
scheduledTime.setSeconds(scheduledTime.getSeconds() + 30);
const actionStartSession: NotificationAction = {
id: 'startSession',
type: 'button',
title: 'Restart App',
launch: true,
choices: ['Restart']
};
// Check if we have permission on iOS
LocalNotifications.hasPermission().then(granted => {
if (granted) {
console.log('We have permission to schedule notifications.');
} else {
LocalNotifications.requestPermission().then(granted => {
alert(`This app needs permission to schedule notifications. Please go to iOS settings > hmapp > Notifications, and allow notifications.`);
});
}
});
LocalNotifications.schedule([
{
id: new Date().getTime(),
title: 'App restart required',
body: 'Tap to reopen the application',
at: scheduledTime,
actions: [actionStartSession]
}
]).then(
() => {
console.log('Notification scheduled');
},
error => {
console.log('Scheduling error: ' + error);
}
);
}
我正在我的 nativescript 8 angular 应用程序中实现 EddyVerbruggen/nativescript-localize 插件,我想知道是否有办法在 [=19] 退出后自动重启应用程序=] 或 android 中的结尾用于更改应用程序语言。
更准确地说,在 NS 官方文档 (https://docs.nativescript.org/plugins/localize.html#how-to-change-the-language-dynamically-at-runtime ) 中,我找到了这段代码(请参阅下面的警告...)我希望完成 ?????? (请参阅下面的代码)使用一种方法(如果可能的话)在关闭后重新启动应用程序。
import { Application } from '@nativescript/core'
import { overrideLocale } from '@nativescript/localize'
alert({
title: 'Switch Language',
message: 'The application needs to be restarted to change language',
okButtonText: 'Quit!'
}).then(() => {
L.localize.overrideLocale(selectedLang)
if (isAndroid) {
;(
Application.android.foregroundActivity || Application.android.startActivity
).finish()
// Here I need a method to RESTART the app automatically after finsih
?????
} else {
exit(0)
// Here I need a method to RESTART the app automatically after exit
?????
}
})
要在 nativescript android 中重启应用程序,请使用:
if (isAndroid) {
const Intent = android.content.Intent;
type Intent = android.content.Intent;
const IntentCompat = androidx.core.content.IntentCompat;
type IntentCompat = androidx.core.content.IntentCompat;
const ctx: android.content.Context = application.android.context;
const mainIntent: Intent = IntentCompat.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_LAUNCHER);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.getApplicationContext().startActivity(mainIntent);
(
Application.android.foregroundActivity ||
Application.android.startActivity
).finish();
}
对于iOS,您似乎无法重新启动应用程序,Apple 的建议甚至是不要使用exit()
以编程方式退出应用程序,您可以阅读here:
Warning: Do not call the exit function. Applications calling exit will appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen.
但是,如果您仍想调用退出函数,则有重新启动应用程序的解决方法,例如安排在应用程序退出后显示本地通知(如 here). To do this using nativescript you can use NativeScript Local Notifications Plugin 使用以下代码所示:
import {
LocalNotifications,
NotificationAction
} from 'nativescript-local-notifications';
// inside your class:
private scheduleNotification() {
var scheduledTime = new Date();
scheduledTime.setSeconds(scheduledTime.getSeconds() + 30);
const actionStartSession: NotificationAction = {
id: 'startSession',
type: 'button',
title: 'Restart App',
launch: true,
choices: ['Restart']
};
// Check if we have permission on iOS
LocalNotifications.hasPermission().then(granted => {
if (granted) {
console.log('We have permission to schedule notifications.');
} else {
LocalNotifications.requestPermission().then(granted => {
alert(`This app needs permission to schedule notifications. Please go to iOS settings > hmapp > Notifications, and allow notifications.`);
});
}
});
LocalNotifications.schedule([
{
id: new Date().getTime(),
title: 'App restart required',
body: 'Tap to reopen the application',
at: scheduledTime,
actions: [actionStartSession]
}
]).then(
() => {
console.log('Notification scheduled');
},
error => {
console.log('Scheduling error: ' + error);
}
);
}