从代码上看鸿蒙APP与Android APP的关系

发布于 2021-1-4 11:36
浏览
3收藏

昨天跑通了鸿蒙的helloWord,蛮高兴的,正准备继续深入的时候,在网上看到一篇文章,我按照文章的说法复盘了一下,贴出代码,供大家交流。

1.当helloWord跑起来后,在build\intermediates\shell\debug\src\main目录下确实有Android 代码

从代码上看鸿蒙APP与Android APP的关系-开源基础软件社区

2、打开AndroidManifest.xml文件,可以看到Android配置信息

从代码上看鸿蒙APP与Android APP的关系-开源基础软件社区

3、打开配置文件指向的ShellMyApplication.java文件,发现ShellMyApplication继承了HarmonyApplication

从代码上看鸿蒙APP与Android APP的关系-开源基础软件社区

4、按住Ctrl点击HarmonyApplication,进入HarmonyApplication类,并定位,发现依赖包,可以看到在依赖包里有对Android Application和三大组件的代理类

从代码上看鸿蒙APP与Android APP的关系-开源基础软件社区

5、进入AbilityShellActivity类,可以看到对Android Activity的代理

从代码上看鸿蒙APP与Android APP的关系-开源基础软件社区

6、根据这些情况,实际上,我们可以在鸿蒙的项目里写Android代码,首先引入Android依赖包

 implementation 'com.google.android:android:4.1.1.4'

从代码上看鸿蒙APP与Android APP的关系-开源基础软件社区
7、在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;
    }
}


 

8、尝试输出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();
 
    }
}

从代码上看鸿蒙APP与Android APP的关系-开源基础软件社区

9、监听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());
            }
        });

从代码上看鸿蒙APP与Android APP的关系-开源基础软件社区10、用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);

从代码上看鸿蒙APP与Android APP的关系-开源基础软件社区
11、用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);

从代码上看鸿蒙APP与Android APP的关系-开源基础软件社区
12、最后,在鸿蒙的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());
            }
        });

 

本文授权转载自:87年的90后

3
收藏 3
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐