【IM】极光简单的聊天测试

配置AndroidManifest.xml

    <permission
     android:name="在极光注册的包名.permission.JPUSH_MESSAGE"
        android:protectionLevel="signature" />

    
    <uses-permission android:name="在极光注册的包名.permission.JPUSH_MESSAGE" />
    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

    <application

        android:name="自己的Application"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>
        activity>
        <activity android:name="com.sunset.im.IMActivity" >
        activity>
        <activity android:name="在极光注册的包名.DengluActivity">activity>
        
        
        
        <service
            android:name="cn.jpush.android.service.PushService"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="cn.jpush.android.intent.REGISTER" />
                <action android:name="cn.jpush.android.intent.REPORT" />
                <action android:name="cn.jpush.android.intent.PushService" />
                <action android:name="cn.jpush.android.intent.PUSH_TIME" />
            intent-filter>
        service>

        
        
        <service
            android:name="cn.jpush.android.service.DaemonService"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="cn.jpush.android.intent.DaemonService" />

                <category android:name="在极光注册的包名" />
            intent-filter>
        service>

        
        <receiver
            android:name="cn.jpush.android.service.PushReceiver"
            android:enabled="true" >
            <intent-filter android:priority="1000" >
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />

                <category android:name="在极光注册的包名" />
            intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            intent-filter>
            
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />

                <data android:scheme="package" />
            intent-filter>
        receiver>

        
        <activity
            android:name="cn.jpush.android.ui.PushActivity"
            android:configChanges="orientation|keyboardHidden"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" >
            <intent-filter>
                <action android:name="cn.jpush.android.ui.PushActivity" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="在极光注册的包名" />
            intent-filter>
        activity>
        
        <service
            android:name="cn.jpush.android.service.DownloadService"
            android:enabled="true"
            android:exported="false" />
        
        <receiver android:name="cn.jpush.android.service.AlarmReceiver" />

        
        <receiver
            android:name="cn.jpush.im.android.helpers.IMReceiver"
            android:enabled="true"
            android:exported="false" >
            <intent-filter android:priority="1000" >
                <action android:name="cn.jpush.im.android.action.IM_RESPONSE" />
                <action android:name="cn.jpush.im.android.action.NOTIFICATION_CLICK_PROXY" />

                <category android:name="在极光注册的包名" />
            intent-filter>
        receiver>

        
        <meta-data
            android:name="JPUSH_CHANNEL"
            android:value="developer-default" />
        
        <meta-data
            android:name="JPUSH_APPKEY"
            android:value="在极光注册的的应用的APPKEY" />

先初始化Application

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("JMessageDemoApplication", "Application onCreate");

        JMessageClient.init(getApplicationContext());
        //是否打印LOG
        JPushInterface.setDebugMode(true);
    }

注册用户

    public void register() {
        JMessageClient.register("用户名", "用户密码", new BasicCallback() {

            @Override
            public void gotResult(int arg0, String arg1) {
                if (arg0==0) {
                    Toast.makeText(MainActivity.this, "注册成功", 0).show();
                }
            }
        });
    }

获取用户信息

    public void getUserInfo() {
        JMessageClient.getUserInfo(FriendName, new GetUserInfoCallback() {
            @Override
            public void gotResult(int arg0, String arg1, UserInfo arg2) {
                System.out.println("............." + arg0);
                System.out.println("............." + arg1);
                System.out.println(arg2.getUserName() + ">>>>>>>>>>>>>"+ arg2.getNickname());
            }
        });
    }

一定要在使用极光SDK的Activity重配置onResume()和onPause()

@Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

登录

public void login() {
        JMessageClient.login(dl_name.getText().toString(), dl_pwd.getText().toString(), new BasicCallback() {

            @Override
            public void gotResult(int arg0, String arg1) {
                if (arg0==0) {
                    Toast.makeText(DengluActivity.this, "登陆成功", 0).show();
                }
            }
        });
    }

发送消息要在开启一个子线程

//创建一个消息对象
Message m = JMessageClient.createSingleTextMessage("朋友的用户名", "要发送的消息");
//发送消息
JMessageClient.sendMessage(m);

接受消息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_im);
        //注册一个接受的广播
    JMessageClient.registerEventReceiver(this);
    }

//接受消息的事件
    public void onEventMainThread(MessageEvent event) {
        Message msg = event.getMessage();
        switch (msg.getContentType()) {
        case text:
            // 处理文字消息
            TextContent textContent = (TextContent) msg.getContent();
            textContent.getText();
            txt_sms.append(textContent.getText());
            break;
        }
    }

一定要在onDestory()中去掉广播

    @Override
    protected void onDestroy() {
        super.onDestroy();
        JMessageClient.unRegisterEventReceiver(this);
    }

自定义设置通知栏的跳转界面,默认的是跳转主界面

    public void onEvent(NotificationClickEvent event) {
        Intent notificationIntent = new Intent(this, IMActivity.class);
        this.startActivity(notificationIntent);// 自定义跳转到指定页面
    }

你可能感兴趣的:(Android,聊天,android,IM-聊天)