kotlin格式编写云信

今天学了一个新知识云信发送文件

   // 发送文件
                        String account="12345678";
                        SessionTypeEnum sessionTypeEnum= SessionTypeEnum.P2P;
                        File file = new File("/sdcard/hh.txt");
                        IMMessage fileMessage = MessageBuilder.createFileMessage(account, sessionTypeEnum, file, file.getName());
                        NIMClient.getService(MsgService.class).sendMessage(fileMessage,false);

KotLin(主角)

最近断断续续地把项目的界面部分的代码由JAva改成了Kotlin编写,并且如果应用了kotlin-android-extensions插件,一个显而易见的好处是再也不用写 findViewById()来实例化你的控件对象了,直接操作你在布局文件里的id即可,这一点我感觉比butterknife做的还简洁友好 再也不用担心id什么的了拿来就用。

首先导入uikit包
在这里插入图片描述
初始化MyApplication 写一个类继承Application

class MyApp : Application() {

    override fun onCreate() {
        super.onCreate()
        NIMClient.init(this, loginInfo(), options());
        // ... your codes
        if (NIMUtil.isMainProcess(this)) {
            // 注意:以下操作必须在主进程中进行
            // 1、UI相关初始化操作
            // 2、相关Service调用
            NimUIKit.init(this)
        }
    }

    // 如果返回值为 null,则全部使用默认参数。
    private fun options(): SDKOptions {
        val options = SDKOptions()

        // 如果将新消息通知提醒托管给 SDK 完成,需要添加以下配置。否则无需设置。
        val config = StatusBarNotificationConfig()
        config.notificationEntrance = MainActivity::class.java // 点击通知栏跳转到该Activity
        config.notificationSmallIconId = R.drawable.down_icon
        // 呼吸灯配置
        config.ledARGB = Color.GREEN
        config.ledOnMs = 1000
        config.ledOffMs = 1500
        // 通知铃声的uri字符串
        config.notificationSound = "android.resource://com.netease.nim.demo/raw/msg"
        options.statusBarNotificationConfig = config


        // 用户资料提供者, 目前主要用于提供用户资料,用于新消息通知栏中显示消息来源的头像和昵称
        options.userInfoProvider = object : UserInfoProvider {
            override fun getUserInfo(account: String): UserInfo? {
                return null
            }

            override fun getDisplayNameForMessageNotifier(s: String, s1: String, sessionTypeEnum: SessionTypeEnum): String? {
                return null
            }

            override fun getAvatarForMessageNotifier(sessionTypeEnum: SessionTypeEnum, s: String): Bitmap? {
                return null
            }


        }
        return options
    }

    // 如果已经存在用户登录信息,返回LoginInfo,否则返回null即可
    private fun loginInfo(): LoginInfo? {
        return null
    }


登录界面
布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="账号"
        android:id="@+id/met1"
        android:layout_margin="50dp"/>
    <EditText
        android:id="@+id/met2"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:layout_below="@id/met1"
        android:layout_marginLeft="50dp"/>

    <Button
        android:id="@+id/mbtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陆"
        android:layout_below="@+id/met2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"/>

</RelativeLayout>

代码

package com.example.myapplication

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.netease.nim.uikit.api.NimUIKit
import com.netease.nim.uikit.api.model.location.LocationProvider
import com.netease.nimlib.sdk.NIMClient
import com.netease.nimlib.sdk.RequestCallback
import com.netease.nimlib.sdk.auth.LoginInfo
import com.netease.nimlib.sdk.msg.MessageBuilder
import com.netease.nimlib.sdk.msg.MsgService
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

         mbtn1.setOnClickListener {
             NimUIKit.getAccount()
             NimUIKit.login(LoginInfo(met1.text.toString(), met2.text.toString()), object : RequestCallback<LoginInfo> {
                 override fun onSuccess(info: LoginInfo) {
                     val intent = Intent(this@MainActivity, Main2Activity::class.java)
                     startActivity(intent)

                     val account = "12345678"
                     //登陆的代码
                     val sessionTypeEnum = SessionTypeEnum.P2P

                     //发送文件
                     val file = File("/sdcard/hh.txt")
                     val fileMessage = MessageBuilder.createFileMessage(account, sessionTypeEnum, file, file.name)
                     NIMClient.getService(MsgService::class.java).sendMessage(fileMessage, false)


                     //云信点击加号的定位功能
                     NimUIKit.setLocationProvider(object : LocationProvider {
                         override fun requestLocation(context: Context, callback: LocationProvider.Callback) {
                             callback.onSuccess(361.0, 136.0, "沈阳")
                         }

                         override fun openMap(context: Context, longitude: Double, latitude: Double, address: String) {

                         }
                     })
                 }

                 override fun onFailed(i: Int) {

                 }

                 override fun onException(throwable: Throwable) {

                 }
             })
         }
    }
}

消息列表 通讯录列表
这个是静态集成的所以我们需要用viewparger 与 TabLayout 去承载消息列表跟通讯录列表
代码

package com.example.myapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import kotlinx.android.synthetic.main.activity_main2.*
import java.util.ArrayList

class Main2Activity : AppCompatActivity() {

    internal var arrayList = ArrayList<Fragment>()
    internal var arrayList1 = ArrayList<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)


        val mf1 = MF1()
        val mf2 = MF2()

        arrayList.add(mf1)
        arrayList.add(mf2)

        arrayList1.add("1")
        arrayList1.add("2")

        val m2Adapter = M2Adapter(supportFragmentManager, arrayList, arrayList1)
        m2vp1.setAdapter(m2Adapter)
        m2tb1.setupWithViewPager(m2vp1)
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity"
    android:orientation="vertical">

    <!--<ListView-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="match_parent"-->
    <!--android:id="@+id/m2lv1">-->

    <!--</ListView>-->


    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/m2tb1">

    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/m2vp1"
        android:layout_weight="9">

    </android.support.v4.view.ViewPager>

</LinearLayout>

写两个fragment里面的代码
通讯录列表

  <fragment
    android:id="@+id/contact_list_fragment"
    android:name="com.netease.nim.uikit.business.contact.ContactsFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</fragment>

消息列表

 <fragment
    android:id="@+id/recent_contacts_fragment"
    android:name="com.netease.nim.uikit.business.recent.RecentContactsFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</fragment>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值