昨天跑通了鴻蒙的helloWord,蠻高興的,正準(zhǔn)備繼續(xù)深入的時(shí)候,在網(wǎng)上看到這樣一篇文章
我按照文章的說法復(fù)盤了一下,貼出代碼,供大家交流。
1.當(dāng)helloWord跑起來后,在build\intermediates\shell\debug\src\main目錄下確實(shí)有Android 代碼
2、打開AndroidManifest.xml文件,可以看到Android配置信息
3、打開配置文件指向的ShellMyApplication.java文件,發(fā)現(xiàn)ShellMyApplication繼承了HarmonyApplication
4、按住Ctrl點(diǎn)擊HarmonyApplication,進(jìn)入HarmonyApplication類,并定位,發(fā)現(xiàn)依賴包,可以看到在依賴包里有對(duì)Android Application和三大組件的代理類
5、進(jìn)入AbilityShellActivity類,可以看到對(duì)Android Activity的代理
6、根據(jù)這些情況,實(shí)際上,我們可以在鴻蒙的項(xiàng)目里寫Android代碼,首先引入Android依賴包
implementation 'com.google.android:android:4.1.1.4'
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();
}
}
9、監(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());
}
});
10、用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);
11、用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);
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());
}
});
編輯:hfy
-
Android
+關(guān)注
關(guān)注
12文章
3943瀏覽量
127788
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論