WEB 前端

ionic capacitor 自定义插件类(安卓)

0 条评论 WEB 前端 ionic 无标签 行走之间

ionic capacitor 自定义插件类(安卓)

  • 方法集合

    exitApp 延迟退出App,也可直接退出
    getAppList 获取已安装App列表
    checkPackExits 检查App是否存在
    openUrl 打开App
  • 保存核心方法

     public Activity getStarActive(){
        return getBridge().getActivity();
    }
  • 类方法

    package com.mytest.plugins.demo;
    
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.ApplicationInfo;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.graphics.drawable.Drawable;
    import android.widget.SimpleAdapter;
    
    import com.getcapacitor.Bridge;
    import com.getcapacitor.JSObject;
    import com.getcapacitor.NativePlugin;
    import com.getcapacitor.Plugin;
    import com.getcapacitor.PluginCall;
    import com.getcapacitor.PluginMethod;
    import com.getcapacitor.ui.Toast;
    
    import org.json.JSONArray;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;
    
    @NativePlugin
    public class PluginsDemo extends Plugin {
    //    public final startActive =
        // 特别重要
        public Activity getStarActive(){
            return getBridge().getActivity();
        }
    
        @PluginMethod
        public void echo(PluginCall call) {
            String value = call.getString("value");
    
            JSObject ret = new JSObject();
            ret.put("value", value);
            call.success(ret);
        }
        private Context getContent(){
            return getContext();
        }
        //  延迟退出App代码
        @PluginMethod
        public void exitApp(PluginCall call) {
            final PluginCall calls = call;
            try{
                final Timer timer = new Timer();
                timer.scheduleAtFixedRate(new TimerTask(){
                    public int times = 10;
                    @Override
                    public void run(){
                        Toast.show(getContent(), "还有" + times-- + "S退出App");
                        if(times  == 0) {
                            timer.cancel();
                            getBridge().getActivity().finish();
                        }
                    }
                },1000,1000);
            }catch (Exception e) {
                call.reject(e.toString());
            }
    
        }
        // 打开其他App
        @PluginMethod
        public void openUrl(PluginCall call) {
           try {
               String packnName = call.getString("url");
               Intent intent = getStarActive().getPackageManager().getLaunchIntentForPackage(packnName);
               JSObject status = new JSObject();
               if (intent != null) {
                   intent.putExtra("type", "110");
                   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                   getStarActive().startActivity(intent);
                   status.put("complete",true);
                   call.resolve(status);
               }else {
                   status.put("complete","packageName error, please check you packageName");
                   call.reject(status.toString());
               }
           }catch (Exception e) {
               call.reject(e.toString());
           }
        }
        // 检查app是否存在
        @PluginMethod
        public void checkPackExits(PluginCall call) {
            JSObject reslut = new JSObject();
            try {
                String packName = call.getString("packName");
                PackageInfo packageInfo = null;
                try {
                    packageInfo = getStarActive().getPackageManager().getPackageInfo(packName, 0);
                    reslut.put("reslut",packageInfo != null);
                    call.resolve(reslut);
                } catch (PackageManager.NameNotFoundException e) {
                    call.reject(e.getMessage());
                }
            } catch (Exception e) {
               call.reject(e.getMessage());
            }
        }
        // 获取App已经安装列表
        @PluginMethod
        public void getAppList(PluginCall call){
            PackageManager pm = getStarActive().getPackageManager();
            List<PackageInfo> installedPackages = pm.getInstalledPackages(0);  // 获取所以已安装的包
            // 创建数组选项
            ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
            ArrayList<AppInfo> list = new ArrayList<AppInfo>();
            // 循环包
            for (PackageInfo packageInfo : installedPackages) {
                AppInfo info = new AppInfo();
                String packageName = packageInfo.packageName;  // 包名
                ApplicationInfo applicationInfo = packageInfo.applicationInfo;  // 应用信息
                String name = applicationInfo.loadLabel(pm).toString();  // 应用名称
                Drawable icon = applicationInfo.loadIcon(pm);  // 应用图标
                System.out.println("name=========="+name);
                System.out.println("packageName=========="+packageName);
                info.name = name;
                info.packageName = packageName;
                info.icon = icon;
                // 状态机,通过01状态来表示是否具备某些属性和功能
                int flags = applicationInfo.flags;  // 获取应用标记
                if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo
                        .FLAG_EXTERNAL_STORAGE) {
                    //安装在sdcard
                    info.isRom = false;
    
                } else {
                    //安装在手机
                    info.isRom = true;
                }
    
                if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo
                        .FLAG_SYSTEM) {
                    //系统应用
                    info.isUser = false;
    
                } else {
                    //用户应用
                    info.isUser = true;
                }
                // push已经安装的包
                if (info.isUser) {
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("ItemTitle", name);    //文字
                    map.put("ItemImage", icon.toString());   //图片
                    map.put("ItempackName",packageName); // 包名
                    map.put("ItemApplicationInfo",applicationInfo.toString()); // 应用信息
                    listItems.add(map);
                }
            }
            // 转换数据格式
            JSONArray jsonArray = new JSONArray(listItems);;
            JSObject ret = new JSObject();
            ret.put("result", jsonArray);
            // 返回数据给前端
            call.success(ret);
        }
    }
    
    class AppInfo {
        public String name;
        public String packageName;
        public Drawable icon;
        public boolean isUser;
        public boolean isRom;
    }

ionic配合capacitor使用

1 条评论 WEB 前端 ionic 无标签 行走之间

ionic配合capacitor使用

  • capacitorcordova有所区别,但是capacitor是ionic官方团队自己开发的,性能和适配性更好
  • 创建capacitor应用

    ionic start photo-gallery tabs --type =(vue or angular or react) --capacitor
    
    
    ionic start photo-gallery tabs --type=angular --capacitor
  • 安装可以在web端访问一些原生插件,如摄像头

    npm install @ionic/pwa-elements
    
    // main.ts
    
    import { defineCustomElements } from '@ionic/pwa-elements/loader';
    
    // Call the element loader after the platform has been bootstrapped
    defineCustomElements(window);
  • 启动项目

    cd photo-gallery
    
    ionic serve
  • 打包

    ionic build
    
  • 创建Android和iOS应用

    ionic cap add ios
    ionic cap add android
  • 将应用拷贝到Android和iOS中,代码改变则需要进行这步骤

    ionic cap copy
  • 将插件同步,添加了插件需要该步骤

    ionic cap sync
  • 打开Xcode 或者 Android studio

    ionic cap open (ios or android )
  • 使用权限,在Cordova中会有config.xml目录进行权限的声明,在capacitor需要在各自项目中配置

    // Android权限配置目录
    android/app/src/main/AndroidManifest.xml
    
    // ios权限配置目录
    ios/app/app/info.plist
  • 代码真机热更新,写完代码在真机上实时可见

    ionic cap run ios -l --external
    ionic cap run android -l --external
  • 添加插件后需要执行的命令,否则可能会出现插件无法找到的报错

    ionic build 
    npx cap copy ios
    npx cap sync
    // 卸载之前的软件
    ionic cap run ios -l --external
  • 简洁的方法,在package.json文件加入以下命令,ios同理

    "scripts": {
        "android": "npm run build && npx cap sync && npx cap open android"
    }