App-Android(使用App+htnl5框架,解决消息推送兼容SignalR问题)
loulijun2021
2022-10-09 2b8231f5da82c56c3315d1cf5b126688ea3d501e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
        <meta name="HandheldFriendly" content="true"/>
        <meta name="MobileOptimized" content="320"/>
        <title>Hello H5+</title>
        <script type="text/javascript" src="../js/common.js"></script>
        <script type="text/javascript">
/*
 *         首先是供参考对比的Android源代码,用Android的java代码创建手机桌面快捷方式
 * 
    import android.content.Intent;
    import android.graphics.BitmapFactory;
    import android.graphics.Bitmap;
    import android.app.Activity;
 
    // 获取主Activity
    Activity main = this;
    // 创建快捷方式意图
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    // 设置快捷方式的名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloH5+");
    // 设置不可重复创建
    shortcut.putExtra("duplicate",false);
    // 设置快捷方式图标
    Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/icon.png");
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
    // 设置快捷方式启动执行动作
    Intent action = new Intent(Intent.ACTION_MAIN);
    action.setComponent( main.getComponentName() );
    shortcut.putExtra( Intent.EXTRA_SHORTCUT_INTENT, action );
    // 广播创建快捷方式
    context.sendBroadcast(shortcut);
*/
/*
 * 如下开始是HTML5plus的js代码,演示如何通过js桥技术调用Android的原生api,从而完成手机桌面快捷方式创建。
 */
var Intent=null,BitmapFactory=null;
var main=null;
// H5 plus事件处理
function plusReady(){
    if ( plus.os.name == "Android" ) {
        // 导入要用到的类对象
        Intent = plus.android.importClass("android.content.Intent");
        BitmapFactory = plus.android.importClass("android.graphics.BitmapFactory");
        // 获取主Activity
        main = plus.android.runtimeMainActivity();
    }
}
if(window.plus){
    plusReady();
}else{
    document.addEventListener("plusready",plusReady,false);
}
// DOMContentLoaded事件处理
var _domReady=false;
document.addEventListener("DOMContentLoaded",function(){
    eStart = document.getElementById( "start" );
},false);
/**
 * 创建桌面快捷方式
 */
function createShortcut(){
    // 创建快捷方式意图
    var shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    // 设置快捷方式的名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloH5+");
    // 设置不可重复创建
    shortcut.putExtra("duplicate",false);
    // 设置快捷方式图标
    var iconPath = plus.io.convertLocalFileSystemURL("_www/icon.png"); // 将相对路径资源转换成系统绝对路径
    var bitmap = BitmapFactory.decodeFile(iconPath);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,bitmap);
    // 设置快捷方式启动执行动作
    var action = new Intent(Intent.ACTION_MAIN);
    //action.setComponent(main.getComponentName());
    action.setClassName(main.getPackageName(), 'io.dcloud.PandoraEntry');
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,action);
    // 广播创建快捷方式
    main.sendBroadcast(shortcut);
    outSet( "桌面快捷方式已创建完成!" );
}
 
/*
 *         供参考对比的Android源代码,用Android的java代码删除手机桌面快捷方式
 * 
 *         // 获取主Activity
 *         Activity main = this;
 *         // 创建快捷方式意图
 *         Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
 *         // 设置快捷方式的名称
 *         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"HelloH5+");
 *         // 设置快捷方式启动执行动作
 *         Intent action = new Intent(Intent.ACTION_MAIN);
 *         action.setComponent(main.getComponentName());
 *         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,action);
 *         // 广播删除快捷方式
 *         main.sendBroadcast(shortcut);
 */
/*
 * 删除桌面快捷方式
 */
function deleteShortcut(){
    // 创建快捷方式意图
    var shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
    // 设置快捷方式的名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"HelloH5+");
    // 设置快捷方式启动执行动作
    var action = new Intent(Intent.ACTION_MAIN);
    //action.setComponent(main.getComponentName());
    action.setClassName(main.getPackageName(), 'io.dcloud.PandoraEntry');
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,action);
    // 广播创建快捷方式
    main.sendBroadcast(shortcut);
    outSet( "桌面快捷方式已删除!" );
}
        </script>
        <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
        <style type="text/css">
        
        </style>
    </head>
    <body>
        <br/>
        <div class="button" onclick="createShortcut()">创建桌面快捷方式</div>
        <div class="button" onclick="deleteShortcut()">删除桌面快捷方式</div>
        <br/>
        <br/>
        <p class="des">
            快捷方式操作是通过向Android系统发送广播的方式来实现,可能在一些定制的ROM上没有实现此类广播的处理,导致无法创建桌面快捷方式。
        </p>
        <br/>
        <div id="outpos"/>
        <div id="output">
Native.JS for Android,可通过plus.android.*调用几乎所有的系统API。
        </div>
    </body>
</html>