公司使用apicloud做的项目,实现将开门钥匙 添加到桌面,点击快捷方式直接打开应用开门。
开门和快捷方式都在模块中实现,出现向h5传值的障碍。
实现过程:首先 肯定是创建快捷方式,有的直接就成功了,有的手机需要手动打开权限,有的手机我没找到 - -。
// 安装的Intent
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// 快捷图标是允许重复
shortcut.putExtra("duplicate", true);
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.putExtra("userId", userId);
shortcutIntent.putExtra("devSn", devSn);
String pkName = context.getPackageName();
shortcutIntent.setClassName(pkName,
"com.ht.demo.OpenActivity");
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 快捷图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
context, UZResourcesIDFinder.getResDrawableID("keylogo"));
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
// 发送广播
context.sendBroadcast(shortcut);
com.ht.demo.OpenActivity 为点击启动页面;清单文件注册为透明风格
<activity
android:name="com.ht.demo.OpenActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
快捷方式点击后启动这个activity,那么此时 有两种情况 应用后台,和应用杀死。创建一个单例
private static ShortChatOpenHelper helper;
private UZModuleContext moduleContext;
private Bundle bundle;
public static ShortChatOpenHelper getInstance() {
if (helper == null) {
helper = new ShortChatOpenHelper();
}
return helper;
}
public UZModuleContext getModuleContext() {
return moduleContext;
}
public void setModuleContext(UZModuleContext moduleContext) {
this.moduleContext = moduleContext;
}
public Bundle getBundle() {
return bundle;
}
public void setBundle(Bundle bundle) {
this.bundle = bundle;
}
public void sendExtra2H5() {
if (bundle != null) {
String userId = bundle.getString("userId");
String devSn = bundle.getString("devSn");
try {
JSONObject jso = new JSONObject();
jso.put("userId", userId);
jso.put("devSn", devSn);
JSONObject sendObj = new JSONObject();
sendObj.put("data", jso);
sendObj.put("success", true);
moduleContext.success(sendObj, false);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
bundle = null;
}
}
由于我们公司用apicloud写的 所以 我用moduleContext来判断的应用后台或杀死,如果原生应用可以用对应页面的context 然后在BaseActivity的onpuse中传入;
接下来 就是打开快捷方式的参数接收。在openActivity中
Bundle a = getIntent().getExtras();
if (a != null) {
ShortChatOpenHelper.getInstance().setBundle(a);
if (ShortChatOpenHelper.getInstance().getModuleContext() == null) {//应用杀死 进主页
Intent intent = new Intent(this, EntranceActivity.class);
startActivity(intent);
} else {
ShortChatOpenHelper.getInstance().sendExtra2H5();//应用后台
}
}
finish();
接收值之后,在单例保存起来参数值,做操作,直接关闭activity
1、应用杀死 进主页, oncreate中判断 单例的bundle是否为空,不为空 根据参数做对应操作;
2、应用后台,根据参数调用对应应该实现的方法;