从代码上看鸿蒙 APP 与安卓 APP 的关系
点击上方“逆锋起笔”,公众号回复 pdf 领取大佬们推荐的学习资料
昨天跑通了鸿蒙的 Hello Word,蛮高兴的,正准备继续深入的时候,在网上看到一篇文章,我按照文章的说法复盘了一下,贴出代码,供大家交流。
①当 Hello Word 跑起来后,在 build\intermediates\shell\debug\src\main 目录下确实有 Android 代码。
②打开 AndroidManifest.xml 文件,可以看到 Android 配置信息。
③打开配置文件指向的 ShellMyApplication.java 文件,发现 ShellMyApplication 继承了 HarmonyApplication。
④按住 Ctrl 点击 HarmonyApplication,进入 HarmonyApplication 类,并定位,发现依赖包。
⑤进入 AbilityShellActivity 类,可以看到对 Android Activity 的代理。
⑥根据这些情况,实际上,我们可以在鸿蒙的项目里写 Android 代码。
implementation 'com.google.android:android:4.1.1.4'
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;
}
}
关注公众号 逆锋起笔,回复 pdf,下载你需要的各种学习资料。
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();
}
}
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.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);
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());
}
});
点“阅读原文”了解更多
评论