电子说
本文介绍将ArkUI框架扩展到Android平台所需要的必要的类及其使用说明,开发者基于OpenHarmony,可复用大部分的应用代码(生命周期等)并可以部署到Android平台,降低跨平台应用开发成本。
使用AndroidStudio所创建Android工程的PackageName需要与OpenHarmony工程的BundleName一致。
注: AndroidStudio:Android应用的开发工具。
开发前请熟悉鸿蒙开发指导文档 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
应用需要继承arkui_android_adapter.jar包所提供的StageApplication。StageApplication用于初始化资源路径以及加载配置信息,例如:
package com.example.myapplication;
import ohos.stage.ability.adapter.StageApplication;
public class HiStageApplication extends StageApplication {
}
Activity需要继承arkui_android_adapter.jar包所提供的StageActivity,StageActivity主要功能是将Android中Activity的生命周期与OpenHarmony中Ability的生命周期进行映射,例如:
package com.example.myapplication;
import ohos.stage.ability.adapter.StageActivity;
public class EntryMainAbilityActivity extends StageActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.setInstanceName("com.example.myapplication:entry:MainAbility:");
super.onCreate(savedInstanceState);
}
}
为了将Activity和ability进行关联,需要重写StageActivity中的onCreate事件,在super.onCreate(savedInstanceState)之前设置instanceName,规则如下:
bundleName:moduleName:abilityName:
其中bundleName的值来自于OpenHarmony应用中app.json5配置文件,moduleName、abilityName的值来自于OpenHarmony应用中的module.json5配置文件。
Android端应用内的Activity的packageName需要与Ability的bundleName一致。
Android端应用内的Activity的activityName组成规则:Ability的moduleName + Ability的abilityName + “Activity”。
示例如图:
import ohos.stage.ability.adapter.StageApplication;
public class HiStageApplication extends StageApplication {
@Override
public void onCreate() {
super.onCreate();
}
}
import android.app.Application;
import ohos.stage.ability.adapter.StageApplicationDelegate;
public class HiStageApplication extends Application {
private StageApplicationDelegate appDelegate_ = null;
@Override
public void onCreate() {
super.onCreate();
appDelegate_ = new StageApplicationDelegate();
appDelegate_.initApplication(this);
}
}
import android.app.Activity;
import ohos.stage.ability.adapter.StageApplicationDelegate;
public class EntryEntryAbilityActivity extends Activity {
private StageApplicationDelegate appDelegate_ = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
appDelegate_ = new StageApplicationDelegate();
appDelegate_.initApplication(this.getApplication());
super.onCreate(savedInstanceState);
}
}
使用原生Activity拉起Ability时,需使用原生应用的startActivity方法,参数的传递需要通过Intent中的putExtra()进行设置,规则如下:
key值为params
value为json格式
{
"params":[
{
"key":键,
"type":参数类型值,
"value":值
},
{
...
}
]
}
参数类型HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿 | 参数类型值 |
---|---|
boolean | 1 |
int | 5 |
double | 9 |
string | 10 |
public class EntryEntryAbilityActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
intent.setClass(this, EntryEntryAbilityTwoActivity.class);
intent.putExtra("params",
"{"params":[{"key":"bool","type":1,"value":"true"}," +
"{"key":"double","type":9,"value":"2.3"}," +
"{"key":"int","type":5,"value":"2"}," +
"{"key":"string","type":10,"value":"test"}]}");
startActivity(intent);
}
}
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !