赞
踩
code小生 一个专注大前端领域的技术平台
公众号回复Android
加入安卓技术群
来源:本文转载自 87 年的 90 后
昨天跑通了鸿蒙的 Hello Word,蛮高兴的,正准备继续深入的时候,在网上看到一篇文章,我按照文章的说法复盘了一下,贴出代码,供大家交流。
①当 Hello Word 跑起来后,在 build\intermediates\shell\debug\src\main 目录下确实有 Android 代码。
如下图:
②打开 AndroidManifest.xml 文件,可以看到 Android 配置信息。
如下图:
③打开配置文件指向的 ShellMyApplication.java 文件,发现 ShellMyApplication 继承了 HarmonyApplication。
如下图:
④按住 Ctrl 点击 HarmonyApplication,进入 HarmonyApplication 类,并定位,发现依赖包。
可以看到在依赖包里有对 Android Application 和三大组件的代理类:
⑤进入 AbilityShellActivity 类,可以看到对 Android Activity 的代理。
如下图:
⑥根据这些情况,实际上,我们可以在鸿蒙的项目里写 Android 代码。
首先引入 Android 依赖包:
- implementation 'com.google.android:android:4.1.1.4'
-
⑦在 MyApplication 类中获取 Android 的 Application:
- public class MyApplication extends AbilityPackage {
- @Override
- public void onInitialize() {
- super.onInitialize();
- }
- public static Application getApp(){
- try {
- return (Application)Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null,(Object[])null);
- }catch (Exception e){
- e.printStackTrace();
- }
- return null;
- }
- }
-
⑧尝试输出 Android Log 和 Toast 并成功:
- public class MainAbility extends Ability {
- private Application application;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setMainRoute(MainAbilitySlice.class.getName());
- application=MyApplication.getApp();
- Log.e("来自Android的Log",application.getClass().getName());
- Toast.makeText(application,"Android吐司",Toast.LENGTH_LONG).show();
-
- }
- }
-
⑨监听 Android Activity 的生命周期,日志现实当前 Activity 是 MainAbilityShellActivity,就是 build 目录里的 Activity:
- MyApplication.getApp().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
- @Override
- public void onActivityCreated(Activity activity, Bundle bundle) {
- Log.e("来自Android的Log","onActivityCreated------>"+activity.getClass().getName());
- }
-
- @Override
- public void onActivityStarted(Activity activity) {
- Log.e("来自Android的Log","onActivityStarted------>"+activity.getClass().getName());
-
- }
-
- @Override
- public void onActivityResumed(Activity activity) {
- Log.e("来自Android的Log","onActivityResumed------>"+activity.getClass().getName());
- }
-
- @Override
- public void onActivityPaused(Activity activity) {
- Log.e("来自Android的Log","onActivityPaused------>"+activity.getClass().getName());
- }
-
- @Override
- public void onActivityStopped(Activity activity) {
- Log.e("来自Android的Log","onActivityStopped------>"+activity.getClass().getName());
- }
-
- @Override
- public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
- Log.e("来自Android的Log","onActivitySaveInstanceState------>"+activity.getClass().getName());
- }
-
- @Override
- public void onActivityDestroyed(Activity activity) {
- Log.e("来自Android的Log","onActivityDestroyed------>"+activity.getClass().getName());
- }
- });
-
⑩用 Android 代码调用 Android 系统 APP 设置页面:
- android.content.Intent intent1=new android.content.Intent();
- intent1.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
- intent1.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
- intent1.setData(Uri.fromParts("package", MyApplication.getApp().getPackageName(), null));
- MyApplication.getApp().startActivity(intent1);
-
⑪用 Android 代码调用 Android 系统拨号页面:
- android.content.Intent intent1=new android.content.Intent(android.content.Intent.ACTION_DIAL,Uri.parse("tel:123456789"));
- intent1.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
- MyApplication.getApp().startActivity(intent1);
-
⑫最后,在鸿蒙的 Ability 上添加 Android 控件:
- MyApplication.getApp().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
- @Override
- public void onActivityCreated(Activity activity, Bundle bundle) {
- Log.e("来自Android的Log","onActivityCreated------>"+activity.getClass().getName());
- }
-
- @Override
- public void onActivityStarted(Activity activity) {
- Log.e("来自Android的Log","onActivityStarted------>"+activity.getClass().getName());
-
- }
-
- @Override
- public void onActivityResumed(Activity activity) {
- Log.e("来自Android的Log","onActivityResumed------>"+activity.getClass().getName());
- FrameLayout frameLayout=(FrameLayout)activity.getWindow().getDecorView().findViewById(android.R.id.content);
- View view=frameLayout.findViewById(1);
- LinearLayout linearLayout;
- if (view instanceof LinearLayout){
- linearLayout=(LinearLayout)view;
- }
- else {
- linearLayout=new LinearLayout(activity);
- linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
- linearLayout.setOrientation(LinearLayout.VERTICAL);
- frameLayout.addView(linearLayout);
- }
- TextView textView=new TextView(activity);
- textView.setText("来自Android的TextView");
- textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
- linearLayout.addView(textView);
- android.widget.Button button=new android.widget.Button(activity);
- button.setText("来自Android的button");
- button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
- linearLayout.addView(button);
- }
-
- @Override
- public void onActivityPaused(Activity activity) {
- Log.e("来自Android的Log","onActivityPaused------>"+activity.getClass().getName());
- }
-
- @Override
- public void onActivityStopped(Activity activity) {
- Log.e("来自Android的Log","onActivityStopped------>"+activity.getClass().getName());
- }
-
- @Override
- public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
- Log.e("来自Android的Log","onActivitySaveInstanceState------>"+activity.getClass().getName());
- }
-
- @Override
- public void onActivityDestroyed(Activity activity) {
- Log.e("来自Android的Log","onActivityDestroyed------>"+activity.getClass().getName());
- }
- });
-
-
- 如果你有写博客的好习惯
- 欢迎投稿
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。