不同应用之间数据的分享

1.数据的分享(不使用系统给的菜单)

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    /**
     * 按钮单击事件
     */
    public void btnOnClick(View v){
        switch (v.getId()) {
        case R.id.btn_shareText://分享文本
            Intent sendIntent = new Intent();  
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT,"分享给你的文本,快接着");
            sendIntent.setType("text/plain");  
            startActivity(Intent.createChooser(sendIntent,"请选择应用"));  
            break;
        case R.id.btn_shareImage://分享图片
            String path=Environment.getExternalStorageDirectory().getAbsoluteFile()+"/myImg/a.jpg";
            File file=new File(path);
            Intent shareIntent = new Intent();
            //设置分享的intent的action   
            shareIntent.setAction(Intent.ACTION_SEND);
            //指定类型
            shareIntent.setType("image/*");
            //设置数据         需要传file的uri形式
            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            //设置分享需要的intent
            startActivity(shareIntent);//系统的样式
            break;
        }
    }
}

权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

分享数据2:用自己的应用接收上面分享的数据

注意,这里需要专门定义一个Activity来处理接收其他应用分享的数据,代码如下

//需要专门定义一个活动类接收其他应用分享的数据,毕竟总不能一打开这个应用就去接收数据嘛
public class GetDataActivity extends Activity {

    private ImageView imageView;
    private TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_data);
        text = (TextView) findViewById(R.id.textView);
        imageView = (ImageView) findViewById(R.id.imageView);

        // 获得分享功能传过来的intent
        Intent intent = getIntent();

        String action = intent.getAction();//获取action
        String type = intent.getType();//获取类型
        if (Intent.ACTION_SEND.equals(action)) {
            if ("text/plain".equals(type)) {
                handleSendText(intent);//接收文本
            } else if (type.startsWith("image/")) {
                handleSendImage(intent);//接收单个图片
            }
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
            if (type.startsWith("image/")) {
                handleSendMultipleImages(intent);// 处理多个image文件的发送
            }
        } else {
            // 其他类型的ACTION
        }
    }

    public void handleSendText(Intent intent) {
        String shareText = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (shareText != null) {
            // 处理获接收到的文本
            text.setText(shareText);
        }
    }
    /**
     * 接收单个图片
     */
    public void handleSendImage(Intent intent) {
        Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
        if (imageUri != null) {
            //获得uri中的file路径
            String path = imageUri.getPath();
            Bitmap bitmap = BitmapFactory.decodeFile(path);
            imageView.setImageBitmap(bitmap);
        }
    }
    /**
     * 接收多个图片
     */
    public void handleSendMultipleImages(Intent intent) {
        ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
        if (imageUris != null) {
            // 处理接收到的图片
        }
    }
}

布局文件就是一个TextView与ImageView

上面活动的注册:

 <activity
            android:name="com.ts.GetDataActivity"
            android:label="@string/title_activity_get_data" >
            <!-- 接收图片的过滤器 -->
                <intent-filter>  
                    <action android:name="android.intent.action.SEND" />  
                    <category android:name="android.intent.category.DEFAULT" />  
                    <data android:mimeType="image/*" />  
                </intent-filter>  
            <!-- 接收文本的过滤器 -->   
                <intent-filter>  
                    <action android:name="android.intent.action.SEND" />  
                    <category android:name="android.intent.category.DEFAULT" />  
                    <data android:mimeType="text/plain" />  
                </intent-filter>  
            <!-- 接收多个图片的过滤器 -->   
                <intent-filter>  
                    <action android:name="android.intent.action.SEND_MULTIPLE" />  
                    <category android:name="android.intent.category.DEFAULT" />  
                    <data android:mimeType="image/*" />  
                </intent-filter>  
        </activity>
    </application>

效果如下:

数据的分享-3:上面的分享方式只不过就是隐式启动能够响应这个注册信息的应用,并没有什么特别的,下面接收另一种分享的方法,注意:上面的分享使用的是按钮,那就看下面的是什么吧.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    }
    /**
     * 加载菜单布局文件
     * 用下面的方法分享后,被选择的分享应用的图标会显示到actionBatr中,不知道是不是模拟器问题,所以选用了下面的按钮单击事件来分享
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        MenuItem item = menu.findItem(R.id.action_shared);//找到分享菜单控件
        //找到分享菜单的支持类对象,这样可以显示系统的分享图标
        ShareActionProvider provider = (ShareActionProvider) item.getActionProvider();
        Intent shareIntent=new Intent();
        //设置分享intent的action
        shareIntent.setAction(Intent.ACTION_SEND);//="android.intent.action.SEND"
        //设置类型
        shareIntent.setType("text/plain");
        //设置数据
        shareIntent.putExtra(Intent.EXTRA_TEXT, "数据来了,接招!");
        provider.setShareIntent(shareIntent);//为这个分享按钮设置分享的意图,点击需要分享的应用就会执行
        return true;
    }
    /**
     * 菜单单击事件,这里不会响应分享图标的单击事件
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        System.out.println(item);
        return super.onOptionsItemSelected(item);
    }
}

上面使用到的菜单项:main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 在活动条的简单使用的文章里使用了下面的两个属性为活动条添加控件和布局
    android:actionLayout=""
    android:actionViewClass=""
       现在需要分享数据给另一个应用,就需要用到actionProviderClass -->
       <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="三个点里面的菜单项"/>
    <item 
        android:id="@+id/action_shared"
        android:title="分享"
        android:showAsAction="always" //会自动显示分享的图标,可以点击,但是没有单击事件,如果这里将showAsAction改为never就会显示title属性
        android:actionProviderClass="android.widget.ShareActionProvider"/>

</menu>

效果如下:

 

posted @ 2016-08-22 22:53  ts-android  阅读(243)  评论(0编辑  收藏  举报