您的位置:首页 > 其它

利用友盟在自己项目中添加反馈功能

2016-07-08 10:18 417 查看
友盟上的官方=的sdk文档地址:http://dev.umeng.com/feedback/android/integration

是利用的友盟自己的反馈界面之类的,有些没必要,自己综合了网上的自定义反馈界面,而且比较简单易复用。

忘了说,反馈是反馈到自己的友盟APP中,登录账号可以看到。

studio中添加:

compile 'com.umeng.analytics:analytics:latest.integration'


并在libs中添加:com.umeng.fb.v5.0.0.jar

一会会将demo贴出来  资源里面取。

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.umengfeedbackdemo">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<meta-data
android:name="UMENG_APPKEY"
android:value="你的AppKey" />
<meta-data
android:name="UMENG_CHANNEL"
android:value="Channel ID" />

<activity android:name=".CustomActivity"/>
</application>

</manifest>


去友盟给应用申请个Appkey,填入AndroidManifest中。http://mobile.umeng.com/apps/new

反馈界面代码,CustomActivity:

package com.example.administrator.umengfeedbackdemo;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.umeng.fb.FeedbackAgent;
import com.umeng.fb.SyncListener;
import com.umeng.fb.model.Conversation;
import com.umeng.fb.model.Reply;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CustomActivity extends AppCompatActivity {

private ListView mListView;
private Conversation mComversation;
private Context mContext;
private ReplyAdapter adapter;
private Button sendBtn;
private EditText inputEdit;
private SwipeRefreshLayout mSwipeRefreshLayout;
private final int VIEW_TYPE_COUNT = 2;
private final int VIEW_TYPE_USER = 0;
private final int VIEW_TYPE_DEV = 1;
private final String TAG = CustomActivity.class.getName();

private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
adapter.notifyDataSetChanged();
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
setContentView(R.layout.activity_custom);
mContext = this;

initView();
mComversation = new FeedbackAgent(this).getDefaultConversation();
adapter = new ReplyAdapter();
mListView.setAdapter(adapter);
sync();

}

private void initView() {
mListView = (ListView) findViewById(R.id.fb_reply_list);
sendBtn = (Button) findViewById(R.id.fb_send_btn);
inputEdit = (EditText) findViewById(R.id.fb_send_content);

// 下拉刷新组件
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.fb_reply_refresh);
sendBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
String content = inputEdit.getText().toString();
inputEdit.getEditableText().clear();
if (!TextUtils.isEmpty(content)) {
// 将内容添加到会话列表
mComversation.addUserReply(content);
// 刷新新ListView
mHandler.sendMessage(new Message());
// 数据同步
sync();
}
}
});

// 下拉刷新
mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
sync();
}
});
}

// 数据同步
private void sync() {

mComversation.sync(new SyncListener() {

@Override
public void onSendUserReply(List<Reply> replyList) {
}

@Override
public void onReceiveDevReply(List<Reply> replyList) {
// SwipeRefreshLayout停止刷新
mSwipeRefreshLayout.setRefreshing(false);
// 发送消息,刷新ListView
mHandler.sendMessage(new Message());
// 如果开发者没有新的回复数据,则返回
if (replyList == null || replyList.size() < 1) {
return;
}
}
});
// 更新adapter,刷新ListView
adapter.notifyDataSetChanged();
}

// adapter
class ReplyAdapter extends BaseAdapter {

@Override
public int getCount() {
return mComversation.getReplyList().size();
}

@Override
public Object getItem(int arg0) {
return mComversation.getReplyList().get(arg0);
}

@Override
public long getItemId(int arg0) {
return arg0;
}

@Override
public int getViewTypeCount() {
// 两种不同的Tiem布局
return VIEW_TYPE_COUNT;
}

@Override
public int getItemViewType(int position) {
// 获取单条回复
Reply reply = mComversation.getReplyList().get(position);
if (Reply.TYPE_DEV_REPLY.equals(reply.type)) {
// 开发者回复Item布局
return VIEW_TYPE_DEV;
} else {
// 用户反馈、回复Item布局
return VIEW_TYPE_USER;
}
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// 获取单条回复
Reply reply = mComversation.getReplyList().get(position);
if (convertView == null) {
// 根据Type的类型来加载不同的Item布局
if (Reply.TYPE_DEV_REPLY.equals(reply.type)) {
// 开发者的回复
convertView = LayoutInflater.from(mContext).inflate(
R.layout.custom_fb_dev_reply, null);
} else {
// 用户的反馈、回复
convertView = LayoutInflater.from(mContext).inflate(
R.layout.custom_fb_user_reply, null);
}

// 创建ViewHolder并获取各种View
holder = new ViewHolder();
holder.replyContent = (TextView) convertView
.findViewById(R.id.fb_reply_content);
holder.replyProgressBar = (ProgressBar) convertView
.findViewById(R.id.fb_reply_progressBar);
holder.replyStateFailed = (ImageView) convertView
.findViewById(R.id.fb_reply_state_failed);
holder.replyData = (TextView) convertView
.findViewById(R.id.fb_reply_date);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

// 以下是填充数据
// 设置Reply的内容
holder.replyContent.setText(reply.content);
// 在App应用界面,对于开发者的Reply来讲status没有意义
if (!Reply.TYPE_DEV_REPLY.equals(reply.type)) {
// 根据Reply的状态来设置replyStateFailed的状态
if (Reply.STATUS_NOT_SENT.equals(reply.status)) {
holder.replyStateFailed.setVisibility(View.VISIBLE);
} else {
holder.replyStateFailed.setVisibility(View.GONE);
}

// 根据Reply的状态来设置replyProgressBar的状态
if (Reply.STATUS_SENDING.equals(reply.status)) {
holder.replyProgressBar.setVisibility(View.VISIBLE);
} else {
holder.replyProgressBar.setVisibility(View.GONE);
}
}

// 回复的时间数据,这里仿照QQ两条Reply之间相差100000ms则展示时间
if ((position + 1) < mComversation.getReplyList().size()) {
Reply nextReply = mComversation.getReplyList()
.get(position + 1);
if (nextReply.created_at - reply.created_at > 100000) {
Date replyTime = new Date(reply.created_at);
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
holder.replyData.setText(sdf.format(replyTime));
holder.replyData.setVisibility(View.VISIBLE);
}
}
return convertView;
}

class ViewHolder {
TextView replyContent;
ProgressBar replyProgressBar;
ImageView replyStateFailed;
TextView replyData;
}
}

}


layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/fb_input_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >

<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:background="#DCDCDC" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:paddingBottom="8dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="8dp" >

<Button
android:id="@+id/fb_send_btn"
android:layout_width="70dp"
android:layout_height="33dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/fb_send_btn_bg"
android:gravity="center"
android:text="发送"
android:textColor="@android:color/white"
android:textSize="16sp" />

<EditText
android:id="@+id/fb_send_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/fb_send_btn"
android:layout_toStartOf="@id/fb_send_btn"
android:background="@drawable/fb_input_bg"
android:hint="反馈意见或建议"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout>

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/fb_reply_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/fb_input_layout" >

<ListView
android:id="@+id/fb_reply_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:listSelector="#00000000" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>


demo截图和手机友盟收到的反馈,这样就可以进行实时的反馈通信了:





demo放到资源里:http://download.csdn.net/detail/zhangli_/9570543
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: