首页 > 移动平台 > 详细

安卓智能聊天机器人的实现及源码分享

时间:2015-09-22 02:13:54      阅读:301      评论:0      收藏:0      [点我收藏+]

这是一个安卓智能聊天机器人的源码,采用了仿微信的风格设计,调用的是图灵机器人的API,能够实现智能聊天、讲故事、讲笑话、查天气、查公交等丰富的功能。
下面是代码片段,想要源码的小伙伴可在下面链接下载:

001

http://www.itlanbao.com/preview.aspx#1,0

?

package com.example.android_robot_01;

002 ??
003 import java.util.ArrayList;
004

i

mport java.util.Date;

005 import java.util.List;
006 ??
007 import android.app.Activity;
008 import android.content.Context;
009 import android.os.Bundle;
010 import android.os.Handler;
011 import android.os.Message;
012 import android.text.TextUtils;
013 import android.view.View;
014 import android.view.Window;
015 import android.view.inputmethod.InputMethodManager;
016 import android.widget.EditText;
017 import android.widget.ListView;
018 import android.widget.Toast;
019 ??
020 import com.example.android_robot_01.bean.ChatMessage;
021 import com.example.android_robot_01.bean.ChatMessage.Type;
022 import com.zhy.utils.HttpUtils;
023 ??
024 public class MainActivity extends Activity
025 {
026 ???? ???/**
027 ???? ?? ?* 展示消息的listview
028 ???? ?? ?*/
029 ???? ???private ListView mChatView;
030 ???? ???/**
031 ???? ?? ?* 文本域
032 ???? ?? ?*/
033 ???? ???private EditText mMsg;
034 ???? ???/**
035 ???? ?? ?* 存储聊天消息
036 ???? ?? ?*/
037 ???? ???private List mDatas = new ArrayList();
038 ???? ???/**
039 ???? ?? ?* 适配器
040 ???? ?? ?*/
041 ???? ???private ChatMessageAdapter mAdapter;
042 ??
043 ???? ???private Handler mHandler = new Handler()
044 ???? ???{
045 ???? ?? ?? ?? ? public void handleMessage(android.os.Message msg)
046 ???? ?? ?? ?? ? {
047 ???? ?? ?? ?? ?? ?? ?? ?ChatMessage from = (ChatMessage) msg.obj;
048 ???? ?? ?? ?? ?? ?? ?? ?mDatas.add(from);
049 ???? ?? ?? ?? ?? ?? ?? ?mAdapter.notifyDataSetChanged();
050 ???? ?? ?? ?? ?? ?? ?? ?mChatView.setSelection(mDatas.size() - 1);
051 ???? ?? ?? ?? ? };
052 ???? ???};
053 ??
054 ???? ???<a href="http://home.51cto.com/index.php?s=/space/5017954" target="_blank">@Override</a>
055 ???? ???protected void onCreate(Bundle savedInstanceState)
056 ???? ???{
057 ???? ?? ?? ?? ? super.onCreate(savedInstanceState);
058 ???? ?? ?? ?? ? requestWindowFeature(Window.FEATURE_NO_TITLE);
059 ???? ?? ?? ?? ? setContentView(R.layout.main_chatting);
060 ???? ?? ?? ?? ????
061 ???? ?? ?? ?? ? initView();
062 ???? ?? ?? ?? ????
063 ???? ?? ?? ?? ? mAdapter = new ChatMessageAdapter(this, mDatas);
064 ???? ?? ?? ?? ? mChatView.setAdapter(mAdapter);
065 ??
066 ???? ???}
067 ??
068 ???? ???private void initView()
069 ???? ???{
070 ???? ?? ?? ?? ? mChatView = (ListView) findViewById(R.id.id_chat_listView);
071 ???? ?? ?? ?? ? mMsg = (EditText) findViewById(R.id.id_chat_msg);
072 ???? ?? ?? ?? ? mDatas.add(new ChatMessage(Type.INPUT, "我是小貅貅,很高兴为您服务"));
073 ???? ???}
074 ??
075 ???? ???public void sendMessage(View view)
076 ???? ???{
077 ???? ?? ?? ?? ? final String msg = mMsg.getText().toString();
078 ???? ?? ?? ?? ? if (TextUtils.isEmpty(msg))
079 ???? ?? ?? ?? ? {
080 ???? ?? ?? ?? ?? ?? ?? ?Toast.makeText(this, "您还没有填写信息呢...", Toast.LENGTH_SHORT).show();
081 ???? ?? ?? ?? ?? ?? ?? ?return;
082 ???? ?? ?? ?? ? }
083 ??
084 ???? ?? ?? ?? ? ChatMessage to = new ChatMessage(Type.OUTPUT, msg);
085 ???? ?? ?? ?? ? to.setDate(new Date());
086 ???? ?? ?? ?? ? mDatas.add(to);
087 ??
088 ???? ?? ?? ?? ? mAdapter.notifyDataSetChanged();
089 ???? ?? ?? ?? ? mChatView.setSelection(mDatas.size() - 1);
090 ??
091 ???? ?? ?? ?? ? mMsg.setText("");
092 ??
093 ???? ?? ?? ?? ? // 关闭软键盘
094 ???? ?? ?? ?? ? InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
095 ???? ?? ?? ?? ? // 得到InputMethodManager的实例
096 ???? ?? ?? ?? ? if (imm.isActive())
097 ???? ?? ?? ?? ? {
098 ???? ?? ?? ?? ?? ?? ?? ?// 如果开启
099 ???? ?? ?? ?? ?? ?? ?? ?imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
100 ???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? InputMethodManager.HIDE_NOT_ALWAYS);
101 ???? ?? ?? ?? ?? ?? ?? ?// 关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的
102 ???? ?? ?? ?? ? }
103 ??
104 ???? ?? ?? ?? ? new Thread()
105 ???? ?? ?? ?? ? {
106 ???? ?? ?? ?? ?? ?? ?? ?public void run()
107 ???? ?? ?? ?? ?? ?? ?? ?{
108 ???? ?? ?? ?? ?? ?? ?? ?? ?? ???ChatMessage from = null;
109 ???? ?? ?? ?? ?? ?? ?? ?? ?? ???try
110 ???? ?? ?? ?? ?? ?? ?? ?? ?? ???{
111 ???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? from = HttpUtils.sendMsg(msg);
112 ???? ?? ?? ?? ?? ?? ?? ?? ?? ???} catch (Exception e)
113 ???? ?? ?? ?? ?? ?? ?? ?? ?? ???{
114 ???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? from = new ChatMessage(Type.INPUT, "服务器挂了呢...");
115 ???? ?? ?? ?? ?? ?? ?? ?? ?? ???}
116 ???? ?? ?? ?? ?? ?? ?? ?? ?? ???Message message = Message.obtain();
117 ???? ?? ?? ?? ?? ?? ?? ?? ?? ???message.obj = from;
118 ???? ?? ?? ?? ?? ?? ?? ?? ?? ???mHandler.sendMessage(message);
119 ???? ?? ?? ?? ?? ?? ?? ?};
120 ???? ?? ?? ?? ? }.start();
121 ??
122 ???? ???}
123 ??
124 }

?

??????? 了解更多请点击:http://www.itlanbao.com/codes.aspx#1,0

?

?

?

安卓智能聊天机器人的实现及源码分享

原文:http://yichen9867.iteye.com/blog/2244911

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!