<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          從代碼上看鴻蒙 APP 與安卓 APP 的關(guān)系

          共 6943字,需瀏覽 14分鐘

           ·

          2021-01-16 14:50

          點(diǎn)擊上方“逆鋒起筆”,公眾號(hào)回復(fù)?pdf
          領(lǐng)取大佬們推薦的學(xué)習(xí)資料

          昨天跑通了鴻蒙的 Hello Word,蠻高興的,正準(zhǔn)備繼續(xù)深入的時(shí)候,在網(wǎng)上看到一篇文章,我按照文章的說法復(fù)盤了一下,貼出代碼,供大家交流。



          當(dāng) Hello Word 跑起來后,在 build\intermediates\shell\debug\src\main 目錄下確實(shí)有 Android 代碼。



          如下圖:


          打開 AndroidManifest.xml 文件,可以看到 Android 配置信息。



          如下圖:


          ③打開配置文件指向的 ShellMyApplication.java 文件,發(fā)現(xiàn) ShellMyApplication 繼承了 HarmonyApplication。



          如下圖:


          按住 Ctrl 點(diǎn)擊 HarmonyApplication,進(jìn)入 HarmonyApplication 類,并定位,發(fā)現(xiàn)依賴包。



          可以看到在依賴包里有對(duì) Android Application 和三大組件的代理類:


          進(jìn)入 AbilityShellActivity 類,可以看到對(duì) Android Activity 的代理。



          如下圖:


          根據(jù)這些情況,實(shí)際上,我們可以在鴻蒙的項(xiàng)目里寫 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;
          ? ?}
          }


          關(guān)注公眾號(hào) 逆鋒起筆,回復(fù) pdf,下載你需要的各種學(xué)習(xí)資料。


          嘗試輸出 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();

          ? ?}
          }



          ⑨監(jiān)聽 Android Activity 的生命周期,日志現(xiàn)實(shí)當(dāng)前 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 代碼調(diào)用 Android 系統(tǒng) APP 設(shè)置頁面:
           ? ? ? ? ? ? ? ? ? ?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 代碼調(diào)用 Android 系統(tǒng)撥號(hào)頁面:


           ? ? ? ? ? ? ? ? ? ?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());
          ? ? ? ? ? ?}
          ? ? ? ?});

          來源:本文轉(zhuǎn)載自 87 年的 90 后。




          點(diǎn)“閱讀原文”了解更多

          瀏覽 23
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  中文字幕在线和永久在线的区别 | 手机免费看操逼视频 | 狂野欧美性交 | 中国久久一级片 | 国产寡妇淫乱高清视频 |