AIDE Android App基础教程_第二十一课答案-分享Intent

第二十一课,本课中你将学习如何利用Share Intents分享数据

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onShareButtonClick(View view)
    {
    	
    }
    
}

点击按钮“onShareButtonClick”方法被调用

现在,用代码“Intent intent = new Intent(Intent.ACTION_SEND);”新建一个“Intent”对象,“ACTION SEND”参数表明这是一个shareintent用来分享数据

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onShareButtonClick(View view)
    {
    	Intent intent=new Intent(Intent.ACTION_SEND);
    }
    
}

添加代码“intent.setType(“text/plain”);”指定此intent分享一些纯文本(你也可以分享图片或视频等)

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onShareButtonClick(View view)
    {
    	Intent intent=new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
    }
    
}

添加代码“intent.putExtra(Intent.EXTRA_TEXT, “This is my text to send”);”到添加的实际文本进行共享:“This is my text to send”

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onShareButtonClick(View view)
    {
    	Intent intent=new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT,"This is my text to send");
    }
    
}

调用“startActivity(intent);”这将使Android利用Share Intent启动和处理activity发送文本–例如电子邮件应用程序

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onShareButtonClick(View view)
    {
    	Intent intent=new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT,"This is my text to send");
		startActivity(intent);
    }
    
}

 

 版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明
本文链接: https://www.aidemx.cn/2135.html

梦雪

专注于AIDE教程分享

1 Comment

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐

AIDE教程网
AIDE教程网