Android语音聊天室源码开发,文本内容的分享发送与接收方法简述

作为开发者的我们如何让自己开发的语音聊天室源码实现文本内容的分享发送与接收呢,下面我们以实现文本的发送分享以及接收来梳理下两种功能的实现过程(其他类型的数据在博文末尾会给大家做简单介绍)。

第一种情况:语音聊天室源码实现发送分享文本功能

在实现语音聊天室源码发送与分享的功能时,根据是否指定选择器Intent.createChooser(),会有两种不同的实现效果。

(1)指定选择器的实现效果如下:
每次需要发送分享的时候,都会弹出所有具有分享功能的APP供选择。(个人认为很人性化)
在这里插入图片描述

(2)未指定选择器的实现效果如下:
图中演示测试使用的安卓原生系统,在未设置选择器的时候,每次会提醒用户使用当前语音聊天室源码提交发送分享所使用的APP仅使用一次还是始终都使用(经测试万一大家手滑,误点了“始终”,那么好吧,如果下次想换其他APP分享内容时,除非你卸载重装当前语音聊天室源码);但在其他一些安卓定制系统的品牌手机上测试时,发现仅第一次会跳出所有具有发送分享功能的APP供你选择(但是不会提示你仅使用一次还是始终),一旦选择后,后果与在原生系统上点击始终的效果相同。立马卸载语音聊天室源码的心都有了。
在这里插入图片描述

好了,实现效果大家都看到了,我们开始撸一把代码吧:

第一步:Layout中界面布局文件activity_main.xml文件(文本编辑框以及按钮):

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.sendshare.MainActivity">
    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="想给潘侯爷说点什么"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendtext"
        android:text="发送文本" />
</LinearLayout>

第二步:Java中实现代码MainActivity.java功能实现代码:
注意注意:指定选择器啊

public class MainActivity extends AppCompatActivity {
    EditText et;//声明文本编辑框
    String str;//声明字符串,用于获取文本编辑框内的内容
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取文本框
        et = (EditText) findViewById(R.id.et);
    }
    //创建方法将输入的内容发出去
    public void sendtext(View view){
        str=et.getText().toString();
        Intent intent = new Intent();
        /*设置action为发送分享,
        *并判断要发送分享的内容是否为空
         */
        intent.setAction(Intent.ACTION_SEND);
        if(str!=null){
            intent.putExtra(Intent.EXTRA_TEXT,str);
        }else{
            intent.putExtra(Intent.EXTRA_TEXT,"");
        }
        intent.setType("text/plain");//设置分享发送的数据类型
        //未指定选择器,部分定制系统首次选择后,后期将无法再次改变
//        startActivity(intent);
        //指定选择器选择使用有发送文本功能的App
        startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));
    }
}

第二种情况:语音聊天室源码实现接收分享文本功能

实现效果如下(将短信内容分享到我们的语音聊天室源码上):
在这里插入图片描述

layout布局界面为初始默认,仅一个默认helloworld的TextView界面,这里就省略不写了。

第一步:AndroidMainfest.xml配置文件(添加接收文本所需的action等intent属性)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.test" >
    <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" />
                //添加接收文本用的action,category,mimeType
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

第二步:Java中实现代码MainActivity.java功能实现代码

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        //获取intent
        Intent intent =getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        //设置接收类型为文本
        if (Intent.ACTION_SEND.equals(action) && type != null){
            if ("text/plain".equals(type)) {
                handlerText(intent);
            }
        }
}
//该方法用于获取intent所包含的文本信息,并显示到APP的Activity界面上
    private void handlerText(Intent intent) {
        String data = intent.getStringExtra(Intent.EXTRA_TEXT);
        tv.setText(data);
    }
}

额外补充:

设置更新语音聊天室源码桌面背景,核心代码如下:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
    public void select(View view){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SET_WALLPAPER);
        startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));
//        startActivity(intent);
    }
}

在语音聊天室源码以二进制的形式分享发送图片,核心代码如下:

public void sendimage(View view) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195228.jpg"));
    intent.setType("image/*");
//  startActivity(intent);
    startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));
}

在语音聊天室源码中发送分享多张图片,核心代码如下:

public void sendimages(View view) {
    ArrayList<Uri> uris = new ArrayList<>();
    //演示发送两张图片
    uris.add(Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195228.jpg"));
    uris.add(Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195155.jpg"));
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND_MULTIPLE);
    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
    intent.setType("image/*");
//        startActivity(intent);
    startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));
}

以上就是“Android语音聊天室源码开发,文本内容的分享发送与接收方法简述"的全部内容了,希望对大家有帮助。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值