android activity test steps
- create a new nativescript project
tns create nativescript_activity --template tns-template-blank-ts
cd nativescript_activity
- update package.json and install
npm update
npm install
- add android platform
tns platform add android
- add the extended activity.
activity.android.ts
import {setActivityCallbacks, AndroidActivityCallbacks} from "tns-core-modules/ui/frame";
@JavaProxy("org.myApp.MainActivity")
class Activity extends android.support.v7.app.AppCompatActivity {
private _callbacks: AndroidActivityCallbacks;
public onCreate(savedInstanceState: android.os.Bundle): void {
if (!this._callbacks) {
setActivityCallbacks(this);
}
this._callbacks.onCreate(this, savedInstanceState, super.onCreate);
}
public onSaveInstanceState(outState: android.os.Bundle): void {
this._callbacks.onSaveInstanceState(this, outState, super.onSaveInstanceState);
}
public onStart(): void {
this._callbacks.onStart(this, super.onStart);
}
public onStop(): void {
this._callbacks.onStop(this, super.onStop);
}
public onDestroy(): void {
this._callbacks.onDestroy(this, super.onDestroy);
}
public onBackPressed(): void {
this._callbacks.onBackPressed(this, super.onBackPressed);
}
public onRequestPermissionsResult(requestCode: number, permissions: Array<string>, grantResults: Array<number>): void {
this._callbacks.onRequestPermissionsResult(this, requestCode, permissions, grantResults, undefined /*TODO: Enable if needed*/);
}
public onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void {
this._callbacks.onActivityResult(this, requestCode, resultCode, data, super.onActivityResult);
}
}
update webpack.config.js
webpack.config.js
@@ -14,6 +14,7 @@ module.exports = env => {
const appComponents = [
"tns-core-modules/ui/frame",
"tns-core-modules/ui/frame/activity",
+ resolve(__dirname, "app/activity.android.ts"),
];
const platform = env && (env.android && "android" || env.ios && "ios");
- add android activity functions.
public readonly MY_PERMISSION_REQUEST = 100;
public onCreate(savedInstanceState: android.os.Bundle): void {
if (!this._callbacks) {
setActivityCallbacks(this);
}
this._callbacks.onCreate(this, savedInstanceState, super.onCreate);
let intent: android.content.Intent = new android.content.Intent( ( <any> android.provider.Settings ).ACTION_HOME_SETTINGS );
let activity = app.android.foregroundActivity || app.android.startActivity;
// TODO: test android activity functions.
// activity.startActivityForResult( intent, this.MY_PERMISSION_REQUEST );
}
- run nativescript android app
tns run android
error when adding this line.
activity.startActivityForResult( intent, this.MY_PERMISSION_REQUEST );
Please login to continue.