1.XiriISceneListener.java
package com.mychtech.himalayajs.service;
import android.content.Intent;
public interface XiriISceneListener {
String onQuery();
void onExecute(Intent var1);
}
2.XiriManager.java
package com.mychtech.himalayajs.service;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.iflytek.xiri.Feedback;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class XiriManager implements XiriISceneListener {
private static XiriManager mInstance = null;
private XiriScene mScene;
private Feedback mFeedback;
private String mSceneJson;
private Context mContext = null;
private IXiriManager mIXiriManager = null;
public static XiriManager getInstance() {
synchronized (XiriManager.class) {
if (mInstance == null) {
mInstance = new XiriManager();
}
}
return mInstance;
}
public void onCreate(Context context, IXiriManager iXiriManager) {
mIXiriManager = iXiriManager;
mContext = context;
mScene = new XiriScene(context);
mFeedback = new Feedback(context);
mScene.init(this);
Log.i(“XiriManager”, “XiriManager add XiriManager oncreate…”);
}
public void onResume() {
//初始化Scene,在界面显示的时候才可调用
mScene.init(this);
Log.i(“XiriManager”, “XiriManager add XiriManager onResume…”);
}
public void onPause() {
//释放Scene,在界面消失的时候调用
mScene.release();
Log.i(“XiriManager”, “XiriManager add XiriManager onPause…”);
}
@Override
public String onQuery() {
return regiestScene();
}
@Override
public void onExecute(Intent intent) {
mFeedback.begin(intent);
//判断是否有注册的场景
Log.i(“XiriManager”, “XiriManager add _scene = " + intent.hasExtra(”_scene"));
if (intent.hasExtra(“_scene”) && intent.getStringExtra(“_scene”)
.equals(“com.mychtech.himalayajs.activity.AudioDetailActivity”)) {
if (intent.hasExtra(“_command”)) { //需注意,此处是_command,不是场景注册时候的_commands,千万别搞错了!
String commands = intent.getStringExtra(“_command”);
Log.i(“XiriManager”, “XiriManager add commands:” + commands);
JSONObject params = new JSONObject();
if (“key1”.equals(commands)) {
//key1–> 固定命令(打开,退出)
mFeedback.feedback(“打开”, Feedback.SILENCE);
if(mIXiriManager != null) {
mIXiriManager.setXiriToJs(“打开”, “”, params.toString());
}
} else if (“key2”.equals(commands)) {
//key2–> 预定于语义$P(_PLAY)
String action = intent.getStringExtra(“_action”);
Log.i(“XiriManager”, "onExecute___key2__action: "+action);
if(“SEEK”.equals(action)) {
int position = intent.getIntExtra(“position”, -1);
try {
params.put(“position”, position);
} catch (JSONException e) {
e.printStackTrace();
}
} else if(“FORWARD”.equals(action) || “BACKWARD”.equals(action)) {
int position = intent.getIntExtra(“offset”, -1);
try {
params.put(“offset”, position);
} catch (JSONException e) {
e.printStackTrace();
}
}
mFeedback.feedback(action, Feedback.SILENCE);
if(mIXiriManager != null) {
mIXiriManager.setXiriToJs(“$P(_PLAY)”, action, params.toString());
}
} else if (“key3”.equals(commands)) {
//ke3–>预定义槽 $P(_PAGE)
String action = intent.getStringExtra(“_action”);
if (“PREV”.equals(action))
{
mFeedback.feedback(“上一页”, Feedback.SILENCE);
}
else if (“NEXT”.equals(action))
{
mFeedback.feedback(“下一页”, Feedback.SILENCE);
}
else if (“INDEX”.equals(action))
{
String index = intent.getStringExtra(“index”);
mFeedback.feedback(“第” + index + “页”, Feedback.SILENCE);
try {
params.put(“index”, index);
} catch (JSONException e) {
e.printStackTrace();
}
}
if(mIXiriManager != null) {
mIXiriManager.setXiriToJs(“$W(video)”, action, params.toString());
}
} else if (“key4”.equals(commands)) {
//key3–>自定义槽$W(video)
String video = intent.getStringExtra(“video”);
mFeedback.feedback(video, Feedback.SILENCE);
if(mIXiriManager != null) {
mIXiriManager.setXiriToJs(“$W(video)”, “”, params.toString());
}
} else if(“key5”.equals(commands)) {
//$P(_EPISODE)
String action = intent.getStringExtra(“_action”);
if (“PREV”.equals(action))
{
//mFeedback.feedback(“上一集”, Feedback.SILENCE);
}
else if (“NEXT”.equals(action))
{
//mFeedback.feedback(“下一集”, Feedback.SILENCE);
}
else if (“INDEX”.equals(action))
{
int index = intent.getIntExtra(“index”,0);
///mFeedback.feedback(“第” + index + “集”, Feedback.SILENCE);
try {
params.put(“index”, index);
} catch (JSONException e) {
e.printStackTrace();
}
}
if(mIXiriManager != null) {
mIXiriManager.setXiriToJs(“$P(_EPISODE)”, action, params.toString());
}
} else if(“key6”.equals(commands)) {
//$P(_SELECT)
try {
params.put(“row”, intent.getIntExtra(“row”, -1));
params.put(“index”, intent.getIntExtra(“index”, -1));
} catch (JSONException e) {
e.printStackTrace();
}
if(mIXiriManager != null) {
mIXiriManager.setXiriToJs(“$P(_SELECT)”, “”, params.toString());
}
} else if(“key7”.equals(commands)) {
//退出应用
if(mIXiriManager != null) {
mIXiriManager.setXiriToJs(“退出”, “”, params.toString());
}
}
}
}
}
/**
-
读取注册了相关场景的方法
-
@param is
-
@return
*/
public String getSceneText(InputStream is) {
StringBuilder builder = new StringBuilder();
if (is != null) {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
builder.append(line);
}
br.close();
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return builder.toString();
}
/**
-
场景命令词注册:
-
_scene: 场景标识,命名规则为前面是应用包名,后面是场景名。
-
_commands:是命令描述,包含发回给应用的命令和说法,以键值对的形式存储。
-
其中键是命令id,值为该功能的各种可能的说法,说法分为三种:
-
1、 命令词短语(如:打开、退出);
-
2、 预定义的语义,以“ P “开头(如: P“开头(如: P“开头(如:P(_PLAY));
-
3、 模糊槽,以“ W “开头后跟槽的名字(如: W“开头后跟槽的名字(如: W“开头后跟槽的名字(如:W(video)其中的“video”对应的是_fuzzy_words 节点中的槽的名字);
-
_fuzzy_words:该内容和“_commands”节点中的自定义槽对应。如果“commands”节点中没有自定义槽,那么“_fuzzy_words“节点则不存在。
*/
public String regiestScene() {
try {
mSceneJson = getSceneText(mContext.getAssets().open(“XiriSceneText.json”));
} catch (IOException e) {
e.printStackTrace();
}
return mSceneJson;
}
public interface IXiriManager {
void setXiriToJs(String key, String action, String params);
}
}
3. XiriScene.java
package com.mychtech.himalayajs.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.util.Log;
import org.json.JSONObject;
public class XiriScene {
private Context mContext;
IntentFilter mIntentFilter;
private XiriISceneListener mISceneListenner;
public static final String ADDSCENECOMMAND_ACTION = “com.iflytek.xiri2.topActivity.QUERY”;
public static final String FUZZY_SCENE_SERVICE_ACTION = “com.iflytek.xiri2.scenes.EXECUTE”;
public static final String COMMIT_ACTION = “com.iflytek.xiri2.topActivity.COMMIT”;
public static final String NEW_COMMIT_ACTION = “tv.yuyin.topActivity.COMMIT”;
private int token;
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String sceneId;
Log.i(“XiriScene”, "onReceive__intent.getAction(): "+intent.getAction());
if (“com.iflytek.xiri2.topActivity.QUERY”.equals(intent.getAction())) {
Log.d(“XiriScene”, "ADDSCENECOMMAND_ACTION 1 onReceive " + Uri.decode(intent.toURI()));
sceneId = intent.getStringExtra(“pkgname”) == null ? “com.iflytek.xiri” : intent.getStringExtra(“pkgname”);
//intent.putExtra(“topActivity”, “com.mychtech.himalayajs.ximalaya.”);
if (“com.iflytek.xiri”.equals(sceneId)) {
intent.setAction(“com.iflytek.xiri2.topActivity.COMMIT”);
intent.putExtra(“_scene”, mISceneListenner.onQuery());
//intent.putExtra(“_package”, XiriScene.this.mContext.getPackageName());
//intent.putExtra(“sw_caller_packagename”, “com.mychtech.himalayajs.ximalaya”);
intent.putExtra(“_package”, “com.mychtech.himalayajs.ximalaya”);
intent.putExtra(“_objhash”, XiriScene.this.token);
intent.setPackage(sceneId);
XiriScene.this.mContext.startService(intent);
} else {
intent.setAction(“tv.yuyin.topActivity.COMMIT”);
intent.putExtra(“_scene”, XiriScene.this.mISceneListenner.onQuery());
//intent.putExtra(“_package”, XiriScene.this.mContext.getPackageName());
//intent.putExtra(“sw_caller_packagename”, “com.mychtech.himalayajs.ximalaya”);
intent.putExtra(“_package”, “com.mychtech.himalayajs.ximalaya”);
intent.putExtra(“_objhash”, XiriScene.this.token);
intent.setPackage(sceneId);
XiriScene.this.mContext.startService(intent);
}
Log.d(“XiriScene”, "ADDSCENECOMMAND_ACTION startService " + Uri.decode(intent.toURI()));
} else if (“com.iflytek.xiri2.scenes.EXECUTE”.equals(intent.getAction())) {
Log.d(“XiriScene”, "FUZZY_SCENE_SERVICE_ACTION " + Uri.decode(intent.toURI()));
Log.d(“SCENE_TIME”, "StartTime " + System.currentTimeMillis());
Log.d(“XiriScene”, "mContext getPackagename " + XiriScene.this.mContext.getPackageName());
if (intent.hasExtra(“_objhash”) && intent.getStringExtra(“_objhash”).equals(XiriScene.this.token + “”)) {
if (intent.hasExtra(“_scene”)) {
sceneId = intent.getStringExtra(“_scene”);
Log.d(“SCENE_TIME”, "fromIntent sceneId " + sceneId);
try {
JSONObject mJsonObject = new JSONObject(XiriScene.this.mISceneListenner.onQuery());
String userSceneId = mJsonObject.getString(“_scene”);
Log.d(“SCENE_TIME”, "userSceneId " + sceneId);
if (userSceneId.equals(sceneId)) {
文末
当你打算跳槽的时候,应该把“跳槽成功后,我能学到什么东西?对我的未来发展有什么好处”放在第一位。这些东西才是真正引导你的关键。在跳槽之前尽量“物尽其用”,把手头上的工作做好,最好是完成了某个项目或是得到提升之后再走。跳槽不是目的,而是为了达到最终职业目标的手段
最后祝大家工作升职加薪,面试拿到心仪Offer
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
ring userSceneId = mJsonObject.getString(“_scene”);
Log.d(“SCENE_TIME”, "userSceneId " + sceneId);
if (userSceneId.equals(sceneId)) {
文末
当你打算跳槽的时候,应该把“跳槽成功后,我能学到什么东西?对我的未来发展有什么好处”放在第一位。这些东西才是真正引导你的关键。在跳槽之前尽量“物尽其用”,把手头上的工作做好,最好是完成了某个项目或是得到提升之后再走。跳槽不是目的,而是为了达到最终职业目标的手段
最后祝大家工作升职加薪,面试拿到心仪Offer
[外链图片转存中…(img-DVtRiQGs-1714269933887)]
[外链图片转存中…(img-3PzyfzNT-1714269933888)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!