安卓(Android)聊天機(jī)器人實(shí)現(xiàn)代碼分享
今天看到一個(gè)ios寫(xiě)的圖靈機(jī)器人,直接去官網(wǎng)(http://www.tuling123.com/openapi/)看了下API接入,太簡(jiǎn)單了,就一個(gè)get請(qǐng)求~于是乎,寫(xiě)了一個(gè)Android版本的機(jī)器人,沒(méi)什么技術(shù)含量,但是挺好玩的~剛好昨晚看了自己喜歡的秦時(shí)明月,嘿嘿,小貔貅,就是我的機(jī)器人寵物啦~
這是一個(gè)安卓智能聊天機(jī)器人的源碼,采用了仿微信的風(fēng)格設(shè)計(jì),調(diào)用的是圖靈機(jī)器人的API,能夠?qū)崿F(xiàn)智能聊天、講故事、講笑話、查天氣、查公交等豐富的功能。
先給大家展示效果圖:

下面是代碼片段,想要源碼的小伙伴可在下面留言留下你的郵箱地址
package com.example.android_robot_;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.example.android_robot_.bean.ChatMessage;
import com.example.android_robot_.bean.ChatMessage.Type;
import com.zhy.utils.HttpUtils;
public class MainActivity extends Activity
{
/**
* 展示消息的listview
*/
private ListView mChatView;
/**
* 文本域
*/
private EditText mMsg;
/**
* 存儲(chǔ)聊天消息
*/
private List mDatas = new ArrayList();
/**
* 適配器
*/
private ChatMessageAdapter mAdapter;
private Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
ChatMessage from = (ChatMessage) msg.obj;
mDatas.add(from);
mAdapter.notifyDataSetChanged();
mChatView.setSelection(mDatas.size() - );
};
};
<a target="_blank">@Override</a>
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_chatting);
initView();
mAdapter = new ChatMessageAdapter(this, mDatas);
mChatView.setAdapter(mAdapter);
}
private void initView()
{
mChatView = (ListView) findViewById(R.id.id_chat_listView);
mMsg = (EditText) findViewById(R.id.id_chat_msg);
mDatas.add(new ChatMessage(Type.INPUT, "我是小貅貅,很高興為您服務(wù)"));
}
public void sendMessage(View view)
{
final String msg = mMsg.getText().toString();
if (TextUtils.isEmpty(msg))
{
Toast.makeText(this, "您還沒(méi)有填寫(xiě)信息呢...", Toast.LENGTH_SHORT).show();
return;
}
ChatMessage to = new ChatMessage(Type.OUTPUT, msg);
to.setDate(new Date());
mDatas.add(to);
mAdapter.notifyDataSetChanged();
mChatView.setSelection(mDatas.size() - );
mMsg.setText("");
// 關(guān)閉軟鍵盤(pán)
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// 得到InputMethodManager的實(shí)例
if (imm.isActive())
{
// 如果開(kāi)啟
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
InputMethodManager.HIDE_NOT_ALWAYS);
// 關(guān)閉軟鍵盤(pán),開(kāi)啟方法相同,這個(gè)方法是切換開(kāi)啟與關(guān)閉狀態(tài)的
}
new Thread()
{
public void run()
{
ChatMessage from = null;
try
{
from = HttpUtils.sendMsg(msg);
} catch (Exception e)
{
from = new ChatMessage(Type.INPUT, "服務(wù)器掛了呢...");
}
Message message = Message.obtain();
message.obj = from;
mHandler.sendMessage(message);
};
}.start();
}
}
以上代碼就是實(shí)現(xiàn)安卓聊天機(jī)器人的全部代碼,喜歡的朋友直接拿去用,在使用過(guò)程中發(fā)現(xiàn)有問(wèn)題請(qǐng)隨時(shí)和我聯(lián)系。
相關(guān)文章
Android 中 TabHost與ViewPager結(jié)合實(shí)現(xiàn)首頁(yè)導(dǎo)航效果
這篇文章主要介紹了Android 中 TabHost與ViewPager結(jié)合實(shí)現(xiàn)首頁(yè)導(dǎo)航效果,代碼簡(jiǎn)單易懂,感興趣的朋友參考下吧2016-09-09
Android實(shí)現(xiàn)彈出鍵盤(pán)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)彈出鍵盤(pán)的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09
android PopupWindow 和 Activity彈出窗口實(shí)現(xiàn)方式
本人小菜一個(gè)。目前只見(jiàn)過(guò)兩種彈出框的實(shí)現(xiàn)方式,第一種是最常見(jiàn)的PopupWindow,第二種也就是Activity的方式是前幾天才見(jiàn)識(shí)過(guò),需要的朋友可以參考下2012-11-11
Android RecyclerView的焦點(diǎn)記憶封裝
這篇文章主要介紹了Android RecyclerView的焦點(diǎn)記憶封裝,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Android側(cè)滑導(dǎo)航欄的實(shí)例代碼
這篇文章主要介紹了Android側(cè)滑導(dǎo)航欄的實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android Fragment+FragmentTabHost組件實(shí)現(xiàn)常見(jiàn)主頁(yè)面(仿微信新浪)
本文主要介紹Fragment+FragmentTabHost組件實(shí)現(xiàn)常見(jiàn)主頁(yè)面,這里整理了詳細(xì)資料及簡(jiǎn)單示例代碼,有興趣的小伙伴可以參考下2016-09-09
Android自定義組件獲取本地圖片和相機(jī)拍照?qǐng)D片
這篇文章主要為大家詳細(xì)介紹了Android自定義組件獲取本地圖片和相機(jī)拍照?qǐng)D片的相關(guān)資料,非常炫酷的效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android設(shè)置Activity背景為透明style的簡(jiǎn)單方法(必看)
下面小編就為大家?guī)?lái)一篇Android設(shè)置Activity背景為透明style的簡(jiǎn)單方法(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
Android Studio的中文亂碼問(wèn)題解決方法
Android Studio安裝后發(fā)現(xiàn)所有的中文,不管是界面上的還是輸出的log中的中文都變成小框框,具體的解決方法如下,感興趣的朋友可以參考下哈2013-06-06

